Find latitude and longitude with javascript

I am using javascript for the first time. In fact, I want to get the latitude and longitude of the address using java script. Can anyone guide me ..

+4
source share
3 answers

If you need the latitude and longitude of a given address, you can use google maps api https://developers.google.com/maps/documentation/javascript/

Here is an example: https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple

EDIT: display it in a warning popup:

var address = document.getElementById("address").value; var geocoder = new google.maps.Geocoder(); geocoder.geocode( { 'address': address}, function(results, status) { var location = results[0].geometry.location; alert(location.lat() + '' + location.lng()); }); 
+7
source

Here is the JS + HTML code (based on a response from Jerome C.):

 <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <title>Geocoding service</title> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> <script> function codeAddress() { var address = document.getElementById("address").value; var geocoder = new google.maps.Geocoder(); geocoder.geocode( { 'address': address}, function(results, status) { var location = results[0].geometry.location; alert('LAT: ' + location.lat() + ' LANG: ' + location.lng()); }); } google.maps.event.addDomListener(window, 'load', codeAddress); </script> </head> <body> <div id="panel"> <input id="address" type="textbox" value="Tembhurkheda, Maharashtra, INDIA"> <input type="button" value="Geocode" onclick="codeAddress()"> </div> </body> </html> 
+3
source

using Google maps api v3 , you can study the source code of this example:
http://universimmedia.pagesperso-orange.fr/geo/loc.htm

+1
source

All Articles