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.
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
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
}
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