Sort the nearest locations using coordinates? - ios

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:.

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

Delivery radius for a restaurant in Apple Maps

I'm trying to develop a delivery system for a restaurant, but I'm not sure how to approach this problem. The restaurant has five locations; four of them are in one state and the other is in a different state.
They only do deliveries for each location depending on how far it is; they also have setup certain limitations for each location.
My idea will be:
Fetch user's location on iOS (Accomplished)
Check if user location is inside of any Restaurant delivery radius. If so, set that location as the store, if not, just show a message that we don't delivery in their area.
Where I'm stuck
How can I define in Apple Maps the limits of Location 1, 2, 3, etc. (meaning what area will they be doing delivery to)?
Try setting the deliverable radius around the location of the restaurant. You can even draw an MKCircle to be fancy.
CLLocation * _storeLocation = [[CLLocation alloc] initWithLatitude:30.270135 longitude:-97.731270];
double deliverableRadius = 3 * 1609.34; // 3 miles (or the area they will deliver to)
MKCircle * circle = [MKCircle circleWithCenterCoordinate:storeLocation.coordinate radius:deliverableRadius];
[_mapView addOverlay:circle];
Once you have established the deliverable area, you can check to see if the see if the users location is within this area.
So I think a simple approach might be to simply check if the user is within a certain distance of a restaurant (ie "If I am less than 25 miles from a restaurant, then they will deliver to me").
First convert the address of the restaurant locations into gps coordinates. See Converting Place Names Into Coordinates for more info.
Next, calculate the distance between the user's location and the location of each restaurant and check if that distance less than the maximum delivery distance.
A formula for calcualating the distance between two points can be found here.

Find places within the boundaries of given locations

in my maps app i have to find the places (bank,ATM,cafe,bar..) inside bound of selected locations.Actually i have to get the places which are centre to the given locations.
for example i have 4 places a,b,c,d i have to get all banks inside bounds of these 4 places and have show them on map.
i can get nearest places for each location individually by using GooglePlaces API.but i need show places which are centre(approximately) to these 4 places.
please give me any suggestions how to do this or any example code or any other tutorials or links....
Thanks
Raki.
Your solution would be to take the average lat/lon of your points and then hit your Google API to get locations in that area. This average will be in the middle of however many coordinates you have. Example:
double totalLat = 0;
double totalLon = 0;
for(CLLocationCoordinate2D coordinate in coordinateArray)
{
totalLat += coordinate.latitude;
totalLon += coordinate.longitude;
}
// Use this average coordinate (which is the center of your points) to hit your Google API
CLLocationCoordinate2D averageCoordinate = CLLocationCoordinate2DMake(totalLat / coordinateArray.count, totalLon / coordinateArray.count);
Note: I haven't actually tested this, so don't just copy/paste, but it's the general idea.
You can always use the radius parameter defined in the Places API to get this information.
Please follow this Stack Overflow answer for more details.

iOS Compare GPS locations

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
}

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