javascript - 如何从谷歌地图 v3 api 获取纬度和经度?

标签 javascript google-maps google-maps-api-3

我正在尝试使用 google maps v3 api 创建 map 。我在互联网上找到了以下代码,我想在 map 窗口中显示纬度和经度而不是地址。

<script>
      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(-33.8688, 151.2195),
          zoom: 13,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById('map_canvas'),
          mapOptions);

        var input = document.getElementById('searchTextField');
        var autocomplete = new google.maps.places.Autocomplete(input);

        autocomplete.bindTo('bounds', map);

        var infowindow = new google.maps.InfoWindow();
        var marker = new google.maps.Marker({
          map: map
        });

        google.maps.event.addListener(autocomplete, 'place_changed', function() {
          infowindow.close();
          marker.setVisible(false);
          input.className = '';
          var place = autocomplete.getPlace();
          if (!place.geometry) {
            // Inform the user that the place was not found and return.
            input.className = 'notfound';
            return;
          }

          // If the place has a geometry, then present it on a map.
          if (place.geometry.viewport) {
            map.fitBounds(place.geometry.viewport);
          } else {
            map.setCenter(place.geometry.location);
            map.setZoom(17);  // Why 17? Because it looks good.
          }
          var image = {
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(35, 35)
          };
          marker.setIcon(image);
          marker.setPosition(place.geometry.location);
          marker.setVisible(true);

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

          infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
          infowindow.open(map, marker);
        });

请有人帮我获取并显示纬度和经度。非常感谢。

最佳答案

你在哪里

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

  infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
  infowindow.open(map, marker);

将信息窗口行改为阅读

  infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + place.geometry.location.lat() + ',' + place.geometry.location.lng());

关于javascript - 如何从谷歌地图 v3 api 获取纬度和经度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15417130/

相关文章:

android - 在自定义图 block 上使用 Google map KML 导入实用程序

api - Google Places 自动完成 API 不返回邮政编码

javascript - 如何使这个方法异步?

java - 无法完成 MapActivity

javascript - D3.JS 垂直线分隔年份

javascript - NextJS 将记录 id 传递给具有模态的函数

javascript - 如何绑定(bind)到所有点击事件,检查属性....有效?

java - Places SDK 中的 APIException 9011 和 9010 错误

javascript - 将 Google map 信息窗口放置在标记下方(带有像素偏移?)

javascript - 循环遍历数组匹配变量并分配执行条件