Skip to content Skip to sidebar Skip to footer

Google Maps V3 -> Is It Possible To Get Properties From Geojson Using Lat Lng Coordinates Or Other Information From Search Box?

I'm using an external geojson-file to add layers to my Google Map. This file contains two properties per area that I would like to access when searching. I can easily fetch the pro

Solution 1:

You can use containsLocation utility to check if a LatLng location is contained by a Polygon.

So in a for loop get all features in your Data layer (populated with loadGeoJson() or addGeoJson()), get each geometry and create a Polygon to use with the containsLocation() function.

For example, the following code checks if loc (LatLng) is contained in one of the features of feats array:

inWhichArea = function(map, loc) {
  for (var i in feats) {
    var area = feats[i].getGeometry();
    var poly = new google.maps.Polygon({
      paths: area.getAt(0).getArray(),
      map: map,
      clickable: false
    });
    if (google.maps.geometry.poly.containsLocation(loc, poly)) {
      return feats[i];
    }
  }
  return null;
}

Here is a jsfiddle with a working example: http://jsfiddle.net/beaver71/sgtpop4f/

Post a Comment for "Google Maps V3 -> Is It Possible To Get Properties From Geojson Using Lat Lng Coordinates Or Other Information From Search Box?"