使用javascript查找纬度和经度

时间:2012-06-14 12:53:27

标签: javascript latitude-longitude

我第一次使用javascript。实际上我想使用java脚本获取地址的纬度和经度。任何人都可以指导我..

3 个答案:

答案 0 :(得分:7)

如果您需要给定地址的纬度和经度,可以使用google maps api https://developers.google.com/maps/documentation/javascript/

这是一个例子: https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple

编辑:在警告弹出窗口中显示:

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());
});

答案 1 :(得分:3)

这是JS + HTML代码(基于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>

答案 2 :(得分:1)

使用Google maps api v3,您可以学习此示例的源代码:
http://universimmedia.pagesperso-orange.fr/geo/loc.htm