Check if an address is within an area map

我只是一个虾纸丫 提交于 2019-12-05 18:41:46

Here is a sketch for you: https://jsfiddle.net/zw6f78xk/

First the Place Autocomplete was used to input an address, which gives you the location of the address

Then the location is checked whether it is inside the polygon.

google.maps.geometry.poly.containsLocation(place.geometry.location, polygon)

Hope this helps.

Update:

But now because you already have the polygon on your google map, it is easy to modify its shape and retrieve its coordinates. Just add editable: true to the polygon options then you can modify it manually and finally you can get the coordinates for example like this:

google.maps.event.addListener(polygon, 'click', function(evt) { 
    var pathArray = polygon.getPath().getArray();
    for (var i = 0; i < pathArray.length; i++){
        console.log(pathArray[i].toUrlValue());
    };
});

So clicking on the polygon it logs its path.

Update 2:

Geocoding the typed (but not selected from the autocomplete) address. https://jsfiddle.net/zw6f78xk/3/

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