I want to achieve the following and I am unaware whether it is possible or not. I am having two points on a road (imagine like a finishing line - they are the two edges of the pavements - they are in a straight line) and I want to check whether a user's route has passed between these points.
So I thought, I can do something like this:
If it was pure geometry, it would be easy. I am having two lines and I want to know if there is any intersection between them.
I want to use the following in an IOS project, if it makes any difference. For example, I thought that maybe there is a programmatically way of drawing the MKPolylines and see if there is intersection. I do not want to see it visually, I just need it programmatically.
Is it possible? Can you suggest me anything else?
The 'Search along route' option helps you to search various places while you are driving or covering a route. The Google Maps will give you notifications are alerts if you have reached the particular place that you want to search along the route in the app. In this way, you would not forget or miss that particular place you want to stop by.
On your iPhone or iPad, open the Google Maps app . Search for your destination or tap it on the map. In the bottom left, tap Directions . Tap Transit . At the top, enter your starting location. Confirm your departure date and time, then tap the route you want.
To get started, open the Google Maps app on your iPhone or Android device. Use the search bar to find the location you would like to ride your bike to. Next, tap “Directions” on the location information card. Select the bike icon in the top section of the screen. Just like with car directions, you may see a few different routes to choose from.
On the right-hand side, tap Pin . To send someone directions and a link to the route in Google Maps, follow the steps below. On your iPhone or iPad, open the Google Maps app . In the bottom left, tap Directions . Select a route. In the top right, tap More .
There is no direct method to check for that intersection.
You can turn the problem into a geometry problem by converting the lat/long positions into map positions (applying the transform to flatten onto a normalised grid). This can be done with MKMapPointForCoordinate
.
One thing to consider is the inaccuracy of the GPS reported positions. In a number of cases you will find that the reported track from the GPS isn't actually on the road but running beside the road. Particularly when turning (tight) corners you will often get a large curve in the track. As such you may want to extend the width of the 'finishing line' to compensate for this.
If you just care about whether the user is within a certain distance of a set point then you can create a CLRegion
to represent the target location and then call containsCoordinate:
with the current user location. This removes any projection and uses the lat/long directly. You can also get the system to monitor this for you and give you a callback when the user enters or exits the region with startMonitoringForRegion:desiredAccuracy:
. Again, you need to consider GPS accuracy when setting the radius.
I would try to solve this problem in three steps:
Step 1. Convert each coordinate user's track has to CGPoint
and save it an array.
// in viewDidLoad
locManager = [[CLLocationManager alloc] init];
[locManager setDelegate:self];
[locManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locManager startPdatingLocation];
self.userCoordinatePoints = [NSMutableArray alloc]init];
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D loc = [newLocation coordinate];
CGPoint *currentPoint = [self.mapView convertCoordinate:loc toPointToView:self.mapView];
// add points to array
self.userCoordinatePoints addObject:currentpoint];
}
Step 2. Convert MKPolylineView
to CGPathRef
Create a class variable of type CGPathRef
{
CGPathRef path;
}
This method you must have implemented to create the route between two points:
- (MKOverlayView*)mapView:(MKMapView*)theMapView
viewForOverlay:(id <MKOverlay>)overlay
{
MKPolylineView *overlayView = [[MKPolylineView alloc]
initWithOverlay:overlay];
overlayView.lineWidth = 3;
overlayView.strokeColor = [[UIColor blueColor]colorWithAlphaComponent:0.5f];
// overlayView.fillColor = [[UIColor purpleColor] colorWithAlphaComponent:0.1f];
path = overlayView.path;
return overlayView;
}
Step 3: Create a custom method to check whether point lies in CGPath or not
- (BOOL)userRouteIntersectsGoogleRoute
{
// Loop through all CGPoints
for(CGPoint point in self.userCoordinatePoints)
{
BOOL val = CGPathContainsPoint(path, NULL, point);
if(val)
{
return YES;
}
}
return NO;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With