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

我的未来我决定 提交于 2020-01-01 05:48:07

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!