Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if an GMSMarker is inside of GMSPolygon (iOS Google Map SDK)

I know how to do this using Apple MapKit following this link, but don't have any idea about how to check if location or annotation (GMSMarker) is inside the GMSPolygon in Google Map SDK.

like image 754
Nitesh Borad Avatar asked Jul 18 '14 13:07

Nitesh Borad


2 Answers

I got the answer.

There is an inline function defined in GMSGeometryUtils.h file in Google SDK:

//Returns whether |point|,CLLocationCoordinate2D lies inside of path, GMSPath
    BOOL GMSGeometryContainsLocation(CLLocationCoordinate2D point, GMSPath *path,
                                 BOOL geodesic);

It contains a function called GMSGeometryContainsLocation, which determines whether location point lies inside of polygon path.

if (GMSGeometryContainsLocation(locationPoint, polygonPath, YES)) {
    NSLog(@"locationPoint is in polygonPath.");
} else {
    NSLog(@"locationPoint is NOT in polygonPath.");
}

Source: GMSGeometryContainsLocation

like image 121
Nitesh Borad Avatar answered Nov 16 '22 08:11

Nitesh Borad


For Swift 4 you can use this extension:

extension GMSPolygon {

    func contains(coordinate: CLLocationCoordinate2D) -> Bool {

        if self.path != nil {
            if GMSGeometryContainsLocation(coordinate, self.path!, true) {
                return true
            }
            else {
                return false
            }
        }
        else {
            return false
        }
    }
}

You can call it directly from your polygon object like this:

if gms_polygon.contains(coordinate: point) {
    //do stuff here
}
like image 4
Mario Jaramillo Avatar answered Nov 16 '22 08:11

Mario Jaramillo