How To Detect A User Is Within Range Of An Annotation - ios

So I am working on a project that includes many uses placing annotations all around a map. The annotation, (which is a custom image with a much larger circular range) appears on the screen and, ideally, I would like for a user to be:
Notified if they are within the range of a annotation
and
Not be allowed to place another annotation within the range of another one if the circular pins overlap by, say, more than 25%
I think this is a pretty unique question and should be fun for somebody to help out with, so have fun! Thanks everybody!

You can check the distance from each annotation using
- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location
This method measures the distance between the two locations by tracing
a line between them that follows the curvature of the Earth. The
resulting arc is a smooth curve and does not take into account
specific altitude changes between the two locations.
For more details refer Link

Try this:
let location = CLLocation(latitude: 1, longitude: 1)//Or user's location
let distance = location.distance(from: anotherLocation)
Edit:
As mentioned in the comments, you wanted to create an equidistant point. I suggest manually doing that:
Subtract the annotation's location from he user's location. Then add your distance back to the original one. For example:
The user's location = (1, 1)
The annotation's location = (3, 2)
Vertical difference would be 2
Horizontal difference would be 1
Then:
(3 + 2, 2 + 1)
Your result: (5, 3)
Now you would have two points (the one you just created and the user's location) at each end with a center point (original annotation)

Related

Mapbox: iOS : Calculate remaining Polyline points from location

I'm developing one navigation application, I want to calculate the remaining polyline left. I already drawn the route over the map. As user start navigation, I want to update the remaining polyline points
I have used Turf framework for this case.
import Turf
let stepPoints = LineString(step.coordinates)
let remainingPoints = stepPoints.sliced(from: location, to: nil)

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.

Check if coordinate rectangle contains CLLocationCoordinate2D

I am using a special Map SDK for iOS and I am adding a custom shape to the map. The shape is always a different size and it could be a circle, square, star etc. the point being it is always dynamic whenever the app is run.
After adding this shape to the map, I can access it's property called overlayBounds which is described as: This property contains the smallest rectangle that completely encompasses the overlay.
The overlay is my shape that I'm adding to the map.
Whenever a location update is generated by CLLocationManager, I want to check and see if the most recent coordinate is inside of that overlayBounds property of the shape.
When accessing overlayBounds, it has an ne property and a sw property. Both of these are just CLLocationCoordinate2D's
So, if the overlayBounds is made up of two CLLocationCoordinate2D's and the CLLocationManager is always updating the user's location and giving me the most recent coordinate(CLLocationCoordinate2D), how can I check if that most recent coordinate is within the overlayBounds?
After doing a lot of research I have only found one potential solution to go off of which is this: https://stackoverflow.com/a/30434618/3344977
But that answer assumes that my overlayBounds property has 4 coordinates(CLLocationCoordinate2D's), when I only have 2.
Your description seems much harder then the actual question. So if I am getting this correctly your question is only to check if the point is inside the rectangle described in overlayBounds.
You have only 2 points as it is enough to define a rectangle. So NE and SW are the two points where the other two are received as (NE.x, SE.y) and (SE.x, NE.y). With this you may use the answer you linked or you may simply construct a MKMapRect where origin is NE and size is SE-NE. So in this case you may simply use MKMapRectMake and then use MKMapRectContainsPoint. BUT watch out when computing size as SE-NE might produce negative results in which cases you need to add degrees to the size. That is 180 to x (latitude) and 360 to y (longitude)...
MKMapRect rect = MKMapRectMake(NE.latitude, NE.longitude, SE.latitude-NE.latitude, SE.longitude-NE.longitude);
if(rect.width < .0) rect.width += 180.0;
if(rect.height < .0) rect.height += 360.0;
BOOL pointInside = MKMapRectContainsPoint(rect, pointOnMap);
Something like this should do the trick.
Now if you are trying to check if the point is inside the shape itself it really depends on how your shape is defined. If this is some form of analytic representation you might find some method already made for you to return the value but if not then your best shot would most likely be drawing the shape to some canvas and checking the color of canvas at the location you need to check. In any case the bigger problem here is converting the point and the rect to a Cartesian coordinate system. If that is the case then just add a comment and I will try to help you on that...

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 MapKit get actual visible area of MKMapView when mapView is 3D

I've tried the following to get the actual visible area of my MKMapView after a region change. None produce the desired result after the user rotates the map.
use mapView.bounds and mapView.convertPoint to get NE and SW CLLocationCoordinate2D.
use mapView.visibleRect to create NE and SW MKMapPoints and convert those points to NE and SW CLLocationCoordinate2D.
use mapView.centerCoodinate and mapView.region.span latitude and longitude delta to calculate NE and SW latitude and longitude, which are then used for new NE and SW CLLocationCoordinate2D.
#1 and #2 come from this post, and all 3 work well enough until the user rotates the map, which brings the mapView.camera into play by changing its heading. Once this happens, the mapView.visibleRect does not match the actual visible area. I'm sure changing altitude and pitch will have similar issues. I understand why properties on MKMapView don't make sense once it goes 3D, but I don't know how to account for the mapView.camera. There is mention of this in a comment on one of the proposed answers in this post, but no solution provided.
My question is, how can I get the area that's actually visible to the user, through the mapView.camera, accounting for heading, altitude and pitch?
I was looking for an answer to the similar situation, and found this. For my project I resolved like the following. Hope this helps.
let northWestCoordinate = self.mapView.convert(CGPoint(x: 0, y: 0), toCoordinateFrom: self.mapView)
let southEastCoordinate = self.mapView.convert(CGPoint(x: self.mapView.frame.size.width, y: self.mapView.frame.size.height), toCoordinateFrom: self.mapView)

Resources