iOS Compare GPS locations - ios

I'm creating an application which needs to determine if my current location (recd in didUpdateLocation:) is present among a set of geo-coordinates I have. Now I understand that comparing double/float values for equality can be error prone as the precisions involved in geo-coordinates is very high. Slight inaccuracy in the GPS can throw my algo off-track. Hence I need to compare it with some error margin.
How do I compare 2 CLLocations with a margin of error? Or determine the error in the location reading (I don't think this is possible since CLLocationManager would have rectified it).
Thanks in advance :)

In each CLLocation instance there is a property declared as #property(readonly, nonatomic) CLLocationAccuracy horizontalAccuracy; What you can do is to make a routine like:
-(BOOL)isLocation:(CLLocation*)location inRangeWith:(CLLocation*)otherLocation{
CLLocationDistance delta = [location distanceFromLocation:otherLocation];
return (delta < location.horizontalAccuracy);
}
You can make even more complex logic as both locations have that property...
Cheers.
【ツ】

You can use the CoreLocation method distanceFromLocation: to find the distance between the user's location and another point. If the distance is less than whatever threshold you decide, then you can assume the locations are the same.
CLLocationDistance threshold = 5.0; // threshold distance in meters
// note: userLocation and otherLocation are CLLocation objects
if ([userLocation distanceFromLocation:otherLocation] <= threshold) {
// same location
}

Related

Core plot : Updating labels using shouldUpdateAxisLabels and shouldUpdateMinorAxisLabels

This is a follow up question to.
Core plot-Add OutOfRange to draw graph that changes dynamically with label "OL".
I am using Automatic labelling policy and using shouldUpdateAxisLabels and shouldUpdateMinorAxisLabels and trying to set the OL label for whichever location is the highest.
In that method,
//Check whether need to set labelling for minTickLocations or majorTickLocations
double maxMajorTickLocationValue = [[[axis.majorTickLocations allObjects] valueForKeyPath:#"#max.doubleValue"] doubleValue];
double minMajorTickLocationValue = [[[axis.majorTickLocations allObjects] valueForKeyPath:#"#min.doubleValue"] doubleValue];
double maxMinorTickLocationValue = [[[axis.minorTickLocations allObjects] valueForKeyPath:#"#max.doubleValue"] doubleValue];
double minMinorTickLocationValue = [[[axis.minorTickLocations allObjects] valueForKeyPath:#"#min.doubleValue"] doubleValue];
while setting labels, I check if location is highest location, for e.g., if location is in majorLocations array and if its greater than maxMinorLocation and set "OL". Similarly for min value with "-OL".
Issue I am facing here is, labelling will always happen on minor locations. So for eg., in case of UI if highest location is a major location and lowest is a minor location, setting of OL label to a major location and -OL to a minor location does not work and labels will be set to only minor locations. Attaching graph image as well.
So pls suggest.
/Users/shrikanth/Desktop/Screen Shot 2017-01-02 at 7.23.06 AM.png

How can I get the point's coordinate in iOS-charts?

I'm trying to get LineChart's plots' coordinate.
I've searched several hours, but I found only one solution, but it depends on xAxis only and always set CGPoint.y to zero.
I want to get the X and Y coordinates.
The reason is that the graph's xAxis is on hour base and showing year based, so it's really hard the tap the point to show a marker even I spread the graph. I tried many things, but I couldn't figure it and not succeeded to detect the tap.
Then I thought that if I can get the point's coordinate, I can build a bd-tree to store them and find the nearest coordinates from the tapped point.
Thank you for your help.
You have to implement the ChartViewDelegate and then implement the following method to get the touch point in your chartview
-(void)chartValueNothingSelected:(ChartViewBase *)chartView{
NSArray *gestureRecognizers = chartView.gestureRecognizers;
int count = 0;
for(UIGestureRecognizer *gesture in chartView.gestureRecognizers){
if([gesture isKindOfClass:[UITapGestureRecognizer class]] && gesture.state == UIGestureRecognizerStateEnded){
float xVal = [self.chartDataView getValueByTouchPointWithPt:[gesture locationInView:self.chartDataView] axis:AxisDependencyLeft].x;
}
}
}
It's too much to explain here.
There is ChartTransformer, which is used to convert data coordinate space into pixel coordinate space and vice versa.
So, in ChartTransformer, there are several methods like pointValueToPixel and pixelToValue to get the job done.
You can try figure out a clever way to write more methods or delegates to pass the coordinates you want.
You have to look at the code in detail to know how to use them. A good example is x axis renderer's drawGridLine.

Sort the nearest locations using coordinates?

I have list a of places with coordinates
If users select particular location I want to suggest list of locations nearby using coordinates
I can achieve that by putting loop and searching all the places within the distance from the following code
for(i=0;i<locations.count;i++)
CLLocation *locA = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
CLLocationDistance distance = [locA distanceFromLocation:locB];
// if(distance<10)
//{
//show the pin
// }
But I guess It not a efficient way if we have more locations in the database
What I can I do here
Is there any alternative way??
You should filter the content to reduce the amount you extract from the database. You need to define what 'nearby' means in your case, calculate the lat/long bounding box where everything outside that box is not 'nearby' and use the box min and max lat/long values to exclude things that don't match. This is done as part of the SQL query so it's very efficient.
Now, your question loop logic comes into play. The approach is fine, though using your own loop likely isn't best. Instead, look at using sortedArrayUsingComparator: or sortedArrayUsingFunction:context:.

Distance between two GPS locations and also trace a route line between them

i tried to find distance between two GPS locations and i got the way using MKReverseGeocoder... now,how can i trace a path between those two locations.. suggestions will be highly appreciated..
Thanks in advance..
and for distance from two location just use bellow line to get distance
CLLocationDistance distance = [yourCurrentLocation distanceFromLocation:yourDestinationLocation];
here yourCurrentLocation and yourDestinationLocation is CLLocation variable
and It Returns the distance (in meters) from the receiver’s location to the specified location.
i hope this helpful to you..
:)

Understanding MKCoordinateFromMapPoint behaviour

In a location based app we use MKMapPoints to store locations, for example the current user location.
When we try use this location on a MKMapView, to set the region that is initially displayed (zoomed in on the user) we convert this to a CLLocationCoordinate2D
There's a convernience method for that: namenly: MKCoordinateForMapPoint, but during testing this gives strange results.
MKMapPoint mapPoint = MKMapPointMake(51.96, 6.3); // My area ;)
CLLocationCoordinate2D automagicCoordinate = MKCoordinateForMapPoint(mapPoint);
CLLocationCoordinate2D manualCoordinate = CLLocationCoordinate2DMake(mapPoint.x, mapPoint.y);
I would expect both the automagicCoordinate and the manualCoordinate to be exactply the same.
but when I inspect it in the debugger I get the following result:
automagicCoordinate.latitude = (CLLocationDegrees) 85.05
automagicCoordinate.longitude = (CLLocationDegrees) -179.99
manualCoordinate.latitude = (CLLocationDegrees) 51.96
manualCoordinate.longitude = (CLLocationDegrees) 6.3
How come the coordinate created with the method is incorrect?
An MKMapPoint is not a latitude and longitude. If it was, you wouldn't need a function to "convert" it to coordinates.
As the Location Awareness Programming Guide explains in the Understanding Map Geometry section:
A map point is an x and y value on the Mercator map projection. Map points are used for many map-related calculations instead of map coordinates because they simplify the mathematics involved in the calculations.
The documentation for MKMapPoint is clearer:
If you project the curved surface of the globe onto a flat surface,
what you get is a two-dimensional version of a map where longitude
lines appear to be parallel. ...
The actual units of a map point are tied to the underlying units used
to draw the contents of an MKMapView, but you should never need to
worry about these units directly. ...
When saving map-related data to a file, you should always save
coordinate values (latitude and longitude) and not map points.
The map point 51.96, 6.3 corresponds to a coordinate at the top-left of the map projection. If you want to work with coordinates (latitude, longitude), use a CLLocationCoordinate2D to avoid confusion.
(You can technically use an MKMapPoint struct to store your coordinate values but then they don't need to be converted to coordinates and the wrong type usage will just lead to confusion.)

Resources