问题
I have gotten answer of the same question for apple map kit from this link, but don't have idea about how to check if some location or annotation (GMSMarker) is inside the GMSPolygon in Google Map SDK. So please, if anyone has any idea, help me.
回答1:
I got the answer by my own.
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
回答2:
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
}
来源:https://stackoverflow.com/questions/24826631/how-to-determine-if-an-gmsmarker-is-inside-of-gmspolygon-ios-google-map-sdk