我知道如何使用Apple MapKit实现这一点,按照此链接进行操作。但是对于如何检查位置或注释(GMSMarker)是否在Google Map SDK中的GMSPolygon中,我没有任何想法。
我知道如何使用Apple MapKit实现这一点,按照此链接进行操作。但是对于如何检查位置或注释(GMSMarker)是否在Google Map SDK中的GMSPolygon中,我没有任何想法。
我得到了答案。
在Google SDK的GMSGeometryUtils.h文件中定义了一个内联函数:
//Returns whether |point|,CLLocationCoordinate2D lies inside of path, GMSPath
BOOL GMSGeometryContainsLocation(CLLocationCoordinate2D point, GMSPath *path,
BOOL geodesic);
它包含一个名为GMSGeometryContainsLocation的函数,用于确定位置点是否位于多边形路径内。
if (GMSGeometryContainsLocation(locationPoint, polygonPath, YES)) {
NSLog(@"locationPoint is in polygonPath.");
} else {
NSLog(@"locationPoint is NOT in polygonPath.");
}
对于Swift 4,您可以使用以下扩展:
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
}
}
}
像这样:
if gms_polygon.contains(coordinate: point) {
//do stuff here
}