Javascript 使用javascript查找纬度和经度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11033451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:08:24  来源:igfitidea点击:

Find the latitude and longitude using javascript

javascriptlatitude-longitude

提问by Aditi Sharma

I am using javascript for the first time.Actually i want to get the latitude and longitude of an address using java script.Can anyone guide me..

我是第一次使用 javascript。实际上我想使用 java 脚本获取地址的纬度和经度。谁能指导我..

回答by Jerome Cance

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

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

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

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

EDIT: to display it in an alert 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());
});

回答by Abhishek Dhote

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

这是 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>

回答by Fabrizio Calderan

using Google maps api v3you could study the source code of this example:
http://universimmedia.pagesperso-orange.fr/geo/loc.htm

使用谷歌地图 api v3你可以研究这个例子的源代码:http:
//universimmedia.pagesperso-orange.fr/geo/loc.htm