使用谷歌地图获取Latitude和longutude

时间:2018-05-30 08:46:16

标签: javascript google-maps dictionary latitude-longitude

我希望使用谷歌地图拖动事件获得标记的经度和纬度。(不是标记拖动事件。我需要设置标记固定)。

这是我的代码

function initialize() {
            var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
                var mapOptions = {
                  center: myLatlng,
                  zoom: 8
                };

            var map = new google.maps.Map(document.getElementById("sample"),
                mapOptions);

            var marker = new google.maps.Marker({
                  position: myLatlng,
                  map: map,
                  title: ''
            });

            google.maps.event.addListener(map, 'center_changed', function() {
                // 0.1 seconds after the center of the map has changed,
                // set back the marker position.
                //alert(center)
                window.setTimeout(function() {
                  var center = map.getCenter();
                  marker.setPosition(center);
                }, 100);
            });

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

1 个答案:

答案 0 :(得分:1)

@JIJOMON K.A,尝试以下解决方案,

  function initialize() {
        var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
            var mapOptions = {
              center: myLatlng,
              zoom: 8
            };
        var map = new google.maps.Map(document.getElementById("googleMap"),
            mapOptions);

        var marker = new google.maps.Marker({
              position: myLatlng,
              map: map,
              title: ''
        });
        google.maps.event.addListener(map, 'center_changed', function() {
            window.setTimeout(function() {
              var center = map.getCenter();
               console.log(marker.getPosition().lat());
               console.log(marker.getPosition().lng());
            }, 100);
        });
    }
  google.maps.event.addDomListener(window, 'load', initialize);