Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect that a point is inside a Polygon using Google Maps SDK for iOS?

With the Googlemaps SDK for iOS, is it possible to detect that a point is inside a Polygon?

I found containsLocation() function in Google Maps JavaScript API, however, I couldn't find the same one in the iOS SDK.

Do you know any other ways?

like image 754
kyon Avatar asked Oct 25 '13 10:10

kyon


2 Answers

The Google Maps SDK for iOS now contains a function called GMSGeometryContainsLocation, which will help you out with a single line of code.

if (GMSGeometryContainsLocation(yourPoint, pathOfPolygon, YES)) {
    NSLog(@"YES: you are in this polygon.");
} else {
    NSLog(@"You do not appear to be in this polygon.");
}

Source: Google Maps for iOS - Reference - GMSGeometryUtils

like image 200
Rachid Finge Jr Avatar answered Oct 04 '22 03:10

Rachid Finge Jr


Converting Rachid's answer to swift was trivial:

if GMSGeometryContainsLocation(yourPoint, pathOfPolygon, true) {
    print("YES: you are in this polygon.")
} else {
    print("You do not appear to be in this polygon.")
}
like image 44
jordan314 Avatar answered Oct 04 '22 03:10

jordan314