Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find if two google map routes are being intersected in an IOS project

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:

  1. get the route between these two points (they are quite near - the road is 20 m wide at most)
  2. get the users route
  3. check if these routes are interecting, if there is any crossing between them.

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?

like image 546
ghostrider Avatar asked Aug 19 '13 13:08

ghostrider


People also ask

What is the 'search along route' option in Google Maps?

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.

How do I find public transit routes on my iPhone?

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.

How do I get directions from Google Maps to my bike?

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.

How do I send someone directions from Google Maps on iPhone?

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 .


2 Answers

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.

like image 83
Wain Avatar answered Oct 02 '22 05:10

Wain


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;
}
like image 20
Puneet Sharma Avatar answered Oct 02 '22 07:10

Puneet Sharma