Gabriel Calle Torrez
Gabriel Calle Torrez

Reputation: 123

Check if an address is within an area map

The need I have is that a user (client) can put his/her address and to validate if it is within our delivery area.

Currently I have designed this layer on google maps:

https://www.google.com/maps/d/u/0/viewer?mid=1E3bcIm6Dy4icmTA4S-hEqOZeHpU

What is the technology that I use?

I just need an input text of a formulary to write the address and respond with an OK or a sorry.

All the information that I find are questions and solutions to calculate routes and only need to verify an address within an area.

Now I know as autofill and get save the address to verify. Now I just need to know how I can verify that direction in my area.

Upvotes: 10

Views: 15005

Answers (1)

MKiss
MKiss

Reputation: 766

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/

Upvotes: 19

Related Questions