Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps (javascript API): get GPS coordinates from address

I need to calculate the distance between two addresses and I don't need a map view or anything graphical.

I simply want the user to type in the address (end location is defined by myself) and let the javascript calculate the distance between the two points.

However I don't find a way to send an address string and get the GPS-coordinates from it using the Google Maps API.

(I really only need the two GPS points, the distance calculation is trivial.)

like image 383
tamasgal Avatar asked Feb 16 '12 10:02

tamasgal


People also ask

How do I find the latitude and longitude of a geocoder address?

The short story is you need to do: Geocoder geocoder = new Geocoder(this, Locale. getDefault()); List<Address> addresses = geocoder. getFromLocation(lat, lng, 1);


1 Answers

What you are trying to do is called geocoding. It is possible with the Maps API. See the Geocoding section for more details. This is the basic idea:

var geocoder = new google.maps.Geocoder();
geocoder.geocode({
    "address": inputAddress
}, function(results) {
    console.log(results[0].geometry.location); //LatLng
});

However, you are limited to 2500 geocoding requests a day, and what you are trying to do is against the terms of use:

Note: the Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited.

like image 182
James Allardice Avatar answered Nov 06 '22 19:11

James Allardice