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 properties if the user clicks on an area or uses any of the other supported event handlers.
https://developers.google.com/maps/documentation/javascript/datalayer#add_event_handlers
this.map.data.addListener('mouseover', (event) => {
console.log(event.feature.getProperty('VD'));
});
However I would like to get what geojson
area I'm in when searching and from that access these properties. Is this possible to do?
searchBox.addListener('places_changed', () => {
var places = this.searchBox.getPlaces();
...
places.forEach((place) => {
...
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 yourData layer
(populated withloadGeoJson()
oraddGeoJson()
), get each geometry and create a Polygon to use with thecontainsLocation()
function.For example, the following code checks if
loc
(LatLng) is contained in one of the features offeats
array:Here is a jsfiddle with a working example: http://jsfiddle.net/beaver71/sgtpop4f/