5

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?

Amal T S
  • 3,327
  • 2
  • 24
  • 57
kyon
  • 183
  • 1
  • 3
  • 12
  • 1
    there is no direct method available in google maps ios sdk . you can find if a point is inside a polygon by using mathematical calculations based on the algorithms suggested in an [earlier answer](http://stackoverflow.com/questions/19106556/get-location-address-w-r-t-the-polygon-i-am-in/19109375#19109375) . you can use the same approach in ios as well – tony m Oct 29 '13 at 04:34
  • Please file a [feature request for Google Maps SDK for iOS](https://code.google.com/p/gmaps-api-issues/issues/entry?template=Maps%20SDK%20for%20iOS%20-%20Feature%20Request) on our bug tracker. – Brett Oct 29 '13 at 05:26

2 Answers2

17

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

Rachid Finge Jr
  • 1,171
  • 10
  • 14
  • 1
    Hi @Rachid Finge Jr I have KML parser which is loaded from file by this code: `let kmlParser = GMUKMLParser(url: url) kmlParser.parse() kmlParser.placemarks[0] let renderer = GMUGeometryRenderer(map: self.mapView, geometries: kmlParser.placemarks, styles: kmlParser.styles) renderer.render()` How I can check if point is in some of placemarks? I search for example but didn't found anything. Thanks in advance – Dragisa Dragisic Oct 17 '18 at 16:37
  • Hi @DragisaDragisic di you got the solution . I am also stuck at this :) – luckyShubhra Mar 31 '21 at 09:47
2

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.")
}
alana314
  • 633
  • 8
  • 11