【问题标题】:Google maps API: get lat / long separately谷歌地图 API:分别获取纬度/经度
【发布时间】:2016-11-11 19:01:07
【问题描述】:

发送geocode 请求并收到状态为OK 的响应后,您可以使用results[0].geometry.location 检索纬度经度.

但是您如何分别获得纬度/经度?我在Google maps API reference 中找不到合适的电话

【问题讨论】:

    标签: javascript google-maps


    【解决方案1】:

    location 字段是LatLng 类型的对象。要获得纬度,请将其称为lat() 方法。要获取经度,请将其称为 lng() 方法。

    例如:

    var latitude = results[0].geometry.location.lat();
    var longitude = results[0].geometry.location.lng();
    

    【讨论】:

    • 是的,就是这样!
    【解决方案2】:

    results[0].geometry.location 是一个google.maps.LatLng 对象,它有一个返回纬度的.lat() 方法和一个返回经度的.lng() 方法。

    【讨论】:

      【解决方案3】:
      <input id="pac-input" type="text" placeholder="Enter a location">
      <div id="map"></div>
      
      <script>
      
      function initMap(){
      
      var input = document.getElementById('pac-input');
      var autocomplete = new google.maps.places.Autocomplete(input);
      
      autocomplete.bindTo('bounds', map);
      autocomplete.addListener('place_changed', function() {
      
          var place = autocomplete.getPlace();
          var lat = place.geometry.location.lat();
          var lng = place.geometry.location.lng();
      
      });
      
      });
      
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-01-29
        • 1970-01-01
        • 2013-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-01
        相关资源
        最近更新 更多