使用Google自动填充API根据位置名称获取纬度和经度

Cha*_*dra 39 javascript google-maps geocoding latitude-longitude

我的页面中有一个文本框,其中包含位置名称和带文本的按钮getLat&Long.现在单击我的按钮,我必须在文本框中显示警报latitudelongitude位置.有什么建议吗?

Red*_*ing 43

您可以使用Google Maps API中的Google地理编码器服务将您的位置名称转换为纬度和经度.所以你需要一些代码:

var geocoder = new google.maps.Geocoder();
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK)
  {
      // do something with the geocoded result
      //
      // results[0].geometry.location.latitude
      // results[0].geometry.location.longitude
  }
});
Run Code Online (Sandbox Code Playgroud)

更新

你是否包含v3 javascript API?

<script type="text/javascript"
     src="http://maps.google.com/maps/api/js?sensor=set_to_true_or_false">
</script> 
Run Code Online (Sandbox Code Playgroud)

  • 它应该是`results [0] .geometry.location.lat()`和`results [0] .geometry.location.lng()` (6认同)

Ari*_*iel 37

我希望这可以帮助将来的某个人.

你可以使用谷歌地理编码API,如前所述,我最近必须做一些工作,我希望这有助于:

<!DOCTYPE html>
<html>
    <head>
        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
        <script type="text/javascript">
        function initialize() {
        var address = (document.getElementById('my-address'));
        var autocomplete = new google.maps.places.Autocomplete(address);
        autocomplete.setTypes(['geocode']);
        google.maps.event.addListener(autocomplete, 'place_changed', function() {
            var place = autocomplete.getPlace();
            if (!place.geometry) {
                return;
            }

        var address = '';
        if (place.address_components) {
            address = [
                (place.address_components[0] && place.address_components[0].short_name || ''),
                (place.address_components[1] && place.address_components[1].short_name || ''),
                (place.address_components[2] && place.address_components[2].short_name || '')
                ].join(' ');
        }
      });
}
function codeAddress() {
    geocoder = new google.maps.Geocoder();
    var address = document.getElementById("my-address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {

      alert("Latitude: "+results[0].geometry.location.lat());
      alert("Longitude: "+results[0].geometry.location.lng());
      } 

      else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }
google.maps.event.addDomListener(window, 'load', initialize);

        </script>
    </head>
    <body>
        <input type="text" id="my-address">
        <button id="getCords" onClick="codeAddress();">getLat&Long</button>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

现在,这也有一个autocomlpete函数,您可以在代码中看到它,它从输入中获取地址,并在键入时由API自动完成.

点击按钮后,您的地址就会根据需要通过警报获得结果.另请注意,这使用最新的API并加载'places'库(调用API时使用'libraries'参数).

希望这会有所帮助,并阅读文档以获取更多信息,欢呼.

编辑#1:小提琴


Suj*_*eet 16

http://maps.googleapis.com/maps/api/geocode/OUTPUT?address=YOUR_LOCATION&sensor=true

OUTPUT = json或xml;

有关谷歌地图api的详细信息,请访问网址:

http://code.google.com/apis/maps/documentation/geocoding/index.html

希望这会有所帮助

  • 这两个链接都不再起作用。 (2认同)

小智 9

以下是我使用的代码.它工作得很好.

var geo = new google.maps.Geocoder;
geo.geocode({'address':address},function(results, status){
    if (status == google.maps.GeocoderStatus.OK) {              
        var myLatLng = results[0].geometry.location;

        // Add some code to work with myLatLng              

    } else {
        alert("Geocode was not successful for the following reason: " + status);
    }
});
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助.


Ash*_*edi 6

我希望这对于未来范围包含具有纬度和经度的自动完整Google API功能更有用

var latitude = place.geometry.location.lat();
var longitude = place.geometry.location.lng();  
Run Code Online (Sandbox Code Playgroud)

完整视图

<!DOCTYPE html>
<html>
    <head>
    <title>Place Autocomplete With Latitude & Longitude </title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
#pac-input {
    background-color: #fff;
    padding: 0 11px 0 13px;
    width: 400px;
    font-family: Roboto;
    font-size: 15px;
    font-weight: 300;
    text-overflow: ellipsis;
}
#pac-input:focus {
    border-color: #4d90fe;
    margin-left: -1px;
    padding-left: 14px;  /* Regular padding-left + 1. */
    width: 401px;
}
}
</style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
    <script>


  function initialize() {
        var address = (document.getElementById('pac-input'));
        var autocomplete = new google.maps.places.Autocomplete(address);
        autocomplete.setTypes(['geocode']);
        google.maps.event.addListener(autocomplete, 'place_changed', function() {
            var place = autocomplete.getPlace();
            if (!place.geometry) {
                return;
            }

        var address = '';
        if (place.address_components) {
            address = [
                (place.address_components[0] && place.address_components[0].short_name || ''),
                (place.address_components[1] && place.address_components[1].short_name || ''),
                (place.address_components[2] && place.address_components[2].short_name || '')
                ].join(' ');
        }
        /*********************************************************************/
        /* var address contain your autocomplete address *********************/
        /* place.geometry.location.lat() && place.geometry.location.lat() ****/
        /* will be used for current address latitude and longitude************/
        /*********************************************************************/
        document.getElementById('lat').innerHTML = place.geometry.location.lat();
        document.getElementById('long').innerHTML = place.geometry.location.lng();
        });
  }

   google.maps.event.addDomListener(window, 'load', initialize);

    </script>
    </head>
    <body>
<input id="pac-input" class="controls" type="text"
        placeholder="Enter a location">
<div id="lat"></div>
<div id="long"></div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)