iOS find the average speed in core location [duplicate] - ios

This question already has answers here:
iOS find average speed
(2 answers)
Closed 8 years ago.
I want to be able to calculate the average speed with core location. I already have the current speed:
- (void)locationUpdate:(CLLocation *)location {
speedLabel.text = [NSString stringWithFormat:#"%.2f", [location speed]*2.236936284];
And also I want to be able to show it in a label and reset it with a button.
I want it to start averaging when a button is pressed and update the average about every 5 seconds and then stop averaging after another button is pressed.
Thank you!

As defined: Average refers to the sum of numbers divided by n. That is, when dealing with an average, you usually have at least 2 or more observations from which you wish to calculate an average. The average of your current speed is just, well, your current speed divided by one. I'm sure that's not what you are looking for.
You will need to ask yourself for how long time you wish to average. Five seconds? 10 seconds? The shorter the interval (fewer observations), the more sensitive to significant changes your average will be.
Basically what you would do is to store your observations in a mutable collection, eg NSMutableArray, and then just sum them up and divide the sum by the array's count property, corresponding to the number of observations. Or, as NSHipster has a great article on, you could take advantage of valueForKeyPath:
NSMutableArray *speedObservations = [[NSMutableArray alloc] initWithCapacity:n];
...
CGFloat average = [speedObservations valueForKeyPath:#"#avg.floatValue"];

Hmm. I would do this differently.
Average speed is distance traveled from a start time to an end time. I would record the user's location when they start asking for average speed, along with the current date. Then I would periodically get a new location reading and calculate distance traveled from the old location to the new location divided by the elapsed time between the current location reading and the first location reading.

Related

How to look back a certain number of days for an indicator and how to only calculate logic upto the last complete bar (i.e. not the current bar)

I'm relatively new to Pine and was having trouble with a couple of things.
So I've got a indicator that I want to look back for the last 5 full days of activity, however not each stock trades in the premarket/afterhours so I want to be able to look back say 5 days and then calculate the number of bars on the time frame (i.e. for a 5 min or hourly resolution). That way I can calculate the number of bars present on the current ticker for that time frame and adjust my indicator length (instead of just having a constant figure of 80 bars or whatever).
The second thing I want to do is only have the indicator update on the completion of a full bar (for example the hourly resolution). So I want it to only calculate up to x-1 bars. Right now I'm finding that it's updating on each new tick. ( I was able to figure this one out, just changed it from i= 0 to length -1 to i=1 to length).
Any help is greatly appreciated.
Thanks,
A

How can I write an mt4 indicator that draws a rectangle and horizontal lines over first 6 hours of hourly candles

I am a quite new to mql4 coding. I would like to know how I write an indicator that does the following, based on the image below:
Draw a rectangle over the current day hour chart that covers the highest and lowest price points of the first 6 hours (candles)
Draw two horizontal lines along the highest and lowest points found 1.
Please note that 1. and 2. should be based strictly on the hourly period and shouldn't vary with the selected period.
I believe I'm suppose to be using ObjectCreate() with OBJ_RECTANGLE and OBJ_HLINE but the whole concept is quite new to me. I would really appreciate some assistance.

How To Find Remaining Distance To Move UIView Between 2 Points

SCENARIO
I am working on an application that keeps track of blood glucose levels into a graph. On the graph there are "markings" (ex: -200mg) going in vertical order along the y axis on the right side of the screen and "hours" (ex: -12:00 PM) will be along the x axis on the bottom of the graph. I have to plot out little 'dots' to display what the blood glucose level was throughout the way.
ISSUE
I am trying to calculate how to position the 'dots' in the correct time and mg level and I'm having difficulty calculating the positions. I can access the "markings" and retrieve it's marking.center.x to indicate which 'Time Slot' (x axis) and the marking.center.y to indicate which 'MG Level' the 'dot' needs to go into. Problem is it isn't always exactly 12:00 PM or 200mg where it will need to be placed. In fact that would be very rare.
WHAT I NEED
Based on the following variables:
dot.mgLevel
The dot will already know where it needs to go based on the information retrieved from the medical device. It will know the time and mgLevel to assign itself.
marking.mgLevel
The markings will each have evenly distributed values that such as -100mg, -200mg, -300mg ect...
timemarking.timeslot
Each time marking on the bottom will each have evenly distributed times allocated every 30 min. Such as -12:00PM, -12:30PM, -1:00PM ect...
If the dot has a mg Level of 330mg and the closest marking on the mg Level is 300mg, then I need to be able to calculate how much further up the dot needs to move from 300 going towards the 400mg marker.
SO...
If the distance between the markings are 100pt and the dot's mgLevel is 330mg, then I know that I need to move the dot from the 300mg marking toward the 400mg marking by exactly 30pt. That's because it's simple math because the distance between the markings is 100. But in real life it isn't 100, so I need to be able to calculate this.
MY ULTIMATE QUESTION
Say distance between markings is 241 and each marking represents multiples of a hundred. Say my dot has a mgLevel of 412. How do I calculate how far I need to move the dot so that it will be in the correct place?
I THINK?
I think I need to make 241 equal 100%. But I need help.
Distance between markings is 241pt
Markings are multiples of 100mg
1mg will occupy 2.41pt. So 412mg will occupy (2.41 * 412) pt. To know how much to move for the next dot, take the difference in mg and multiply by 2.41.
In general, if distance between 2 markings in points is d, markings are multiples of m, and desired accuracy is k decimal places, 1mg will occupy g:
let divisor = pow(10.0, Double(k))
let g = round((d/m)*divisor) / divisor

Find closest point on path forwards

Given a path say 1000 meters long from start to goal consisting of a finite number of CLLocation points (guesstimate around 100). Assuming the user is somewhere on the path and a CLLocation coming from iOS location services, what would be the best way to calculate the nearest point forwards along the route?
Finding the nearest point is easy enough using -[CLLocation distanceFromLocation:], but as the user passes a point and moves towards the next, the previous point will still be the nearest until he passes the midway between the previous and next point.
You could keep a array consisting of the sorted (start to the end of the path) CLLocation points (hypothetical 100) and evaluate only those poins to find the nearest one that will be visited. To find a visited point you keep the starting position and mesure the distance from the start to the current location of the user(d1). If d1 > distance from start to the next point in the sorted array and the current location is nearby (10 - 100 m) the next point in sorted array then remove the point else user did not visit / passed by that location so you keep it in the sorted array.

geolocation calculation: calculate points if they are within a radius

ok here is the problem:
I want to show to my users the closest geopoints in his vicinity.
i have arround 55.000 fixed geopoints which could be shown. (I have for all of them long/lat)
dependent on the users location (long/lat) i want to show the closest 20 geopoints.
so of course i could just calculate all 55.000 geopoints and see if there are 20 within a certain radius of the users lang/lat and rank them afterwards but this would be somehow an overhead to calculate always 55tsd geopoints.
is there a posibility to "add" 5-miles/kilometers range to a users long and lat and calculate only the distance for those which are within this radius?
i hope you get what i mean. :)
Merci
If the calculation of distance between all points take too much time, you could narrow down the possible points just by simple difference between lat/lon (source_lat - target_lat) (source_lon - target_lon) and take only the closest point to final calculation of distance. You could use formula to calculate distance.
D = 60* 1.1515 * acos (sin(pi*y/180) * sin(pi*Y/180) +
cos(pi*y/180) * cos(pi*Y/180) * cos((z-Z) *pi / 180)
) * 180 / pi)
But the efficiency of calculation depends on used database technology as well.

Resources