javascript - 使用谷歌地图获取纬度和经度

标签 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);

最佳答案

@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);

关于javascript - 使用谷歌地图获取纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50600216/

相关文章:

javascript - 如何在 Node 应用程序中指定环境变量?

javascript - 是否可以使用 .transition() .duration() 中传递的时间作为变量?

javascript - TypeError : $(. ..).dialog 不是函数

android - Google map 比 LocationManager 更快地获得 GPS 定位!为什么?

python - python 映射中的派生值

javascript - d3js : How does the "magic" work with fill on hover for a barchart plot?

javascript - 地球夜间 map 的异步加载

java - 标记类的子类

search - 数据结构树和图有什么区别?

键中的 Python 字典键