How to find if two Google map routes intersect in an iOS project

I want to achieve the following, and I do not know if this is possible or not. I have two points on the road (imagine how the finish line is the two edges of the sidewalks - they are in a straight line), and I want to check if the user’s route between these points has passed.

So I thought I could do something like this:

  • get a route between these two points (they are pretty close - a road no more than 20 m wide)
  • get user route
  • check if these routes intersect if there is an intersection between them.

If it were pure geometry, it would be easy. I have two lines and I want to know if there is an intersection between them.

I want to use the following in an iOS project, if that matters. For example, I thought there might be a programmatic way to draw MKPolylines and see if there is an intersection. I don’t want to see it visually, I just need it programmatically.

Is it possible? Can you offer me anything else?

+6
source share
2 answers

There is no direct method for checking this intersection.

You can turn a problem into a geometry problem by converting lat / long positions to map positions (using a transform to smooth on a normalized grid). This can be done using MKMapPointForCoordinate .

One thing to consider is the inaccuracy of the positions reported by GPS. In some cases, you will find that the reported GPS track is not actually on the road, but works near the road. In particular, when turning (tightly) corners, you often get a big curve on the track. Thus, you can increase the width of the "finish line" to compensate for this.


If you're just wondering if the user is a certain distance from the given value, you can create a CLRegion to represent the target location, and then call containsCoordinate: with the user's current location. This removes any projection and uses lat / long directly. You can also force the system to track this for you and give a callback when a user enters or leaves a region using startMonitoringForRegion:desiredAccuracy: Again, you need to consider GPS accuracy when setting the radius.

+3
source

I would try to solve this problem in three stages:

Step 1. Convert each track of the user track to CGPoint and save the 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 must be implemented to create a 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 your own method to check if the point is 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; } 
+2
source

Source: https://habr.com/ru/post/952029/


All Articles