在使用Google Maps api定义纬度和经度时使用JavaScript变量[关闭]

问题描述 投票:-2回答:1

有人知道如何将“纬度”和“长整数”传递到底部的标记中吗?我已经尝试过“ latlng”,但是没有用。

        var divlat = document.getElementById("dom-targetlat");
        var str = divlat.textContent;
        lat = str.replace(/\s/g, '');
        var divlon = document.getElementById("dom-targetlon");
        var str = divlon.textContent;
        lon = str.replace(/\s/g, '');
        var map;
  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      center: {lat: 54.6034984, lng: -7.3633107},
      zoom: 4.5
    });
            var marker = new google.maps.Marker({
// The below line is equivalent to writing:
// position: new google.maps.LatLng(-34.397, 150.644)
position: {lat: lat, lng: lon},
map: map
});
javascript google-maps google-maps-api-3
1个回答
0
投票

我不确定,但我认为您需要确保latlon是数字。尝试使用position: {lat: parseFloat(lat), lng: parseFloat(lon)})

© www.soinside.com 2019 - 2024. All rights reserved.