Search code examples
google-maps-sdk-ios

How to see if a GMSPath contains a coordinate


Google Maps 1.2.0 GMSCoordinateBounds now contains a containsCoordinate method that I was intending to use for filtering markers that are not on the visibleRegion. Unfortunately, when you init a GMSCoordinateBounds, you get bounds that encompass your region or path.

So my question is : is it possible to see if a CLLocationCoordinate2D is within a GMSPath?


Solution

  • So I'm answering my own question. I just needed to use the pointForCoordinate method to see if the point was on the screen. Works perfectly.

    for (int i = 0; i < self.visibleLocations.count; i++) {
            Location *location = [self.visibleLocations objectAtIndex:i];
            CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([location.lat floatValue], [location.lng floatValue]);
            CGPoint markerPoint = [self.googleMap.projection pointForCoordinate:coordinate];
            if (markerPoint.x >= 0 && markerPoint.y >= 0 && markerPoint.x <= self.googleMap.frame.size.width && markerPoint.y <= self.googleMap.frame.size.height) {
                GMSMarker *marker = [GMSMarker markerWithPosition:coordinate];
                marker.title = location.title;
                marker.icon = [UIImage imageNamed:@"search_measle_small.png"];
                marker.map = self.googleMap;
            }
        }