【问题标题】:google maps move marker with lat/lng from ajax success returned data谷歌地图使用 lat/lng 从 ajax 成功返回数据移动标记
【发布时间】:2013-07-15 13:05:48
【问题描述】:

尝试使用 sql db 中的纬度/经度坐标在间隔上移动标记/地图。

function initialize() {
    var myLatLng = new google.maps.LatLng(41,14);

    var myOptions = {
        zoom: 16,
        center: myLatLng,
        scrollwheel: false,
        panControl: true,
        zoomControl: true,
        mapTypeControl: true,
        scaleControl: true,
        streetViewControl: true,
        overviewMapControl: true,
        mapTypeId: google.maps.MapTypeId.SATELLITE,
    }

    map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

    marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
        draggable: false
});

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

function getCoords() {

$.ajax({
url: "../ajaxscript.php",
type: "POST",
data: {
foo : "bar"
},
dataType: "text",
success: function(returnedData) {
      alert(returnedData);
    moveMarkerMap(returnedData);
}
});

}

function moveMarkerMap(newCoords) {
var newLatLang = new google.maps.LatLng(newCoords);
map.panTo(newLatLang);
marker.setPosition(newLatLang);

}
window.setInterval(getCoords, 5000);

在 moveMarkerMap() 中设置新的 google.maps.LatLng(14,41) 会移动它,并且返回的数据会在 alert() 中显示,但与 moveMarkerMap() 一起使用时标记不会移动

ajax返回的字符串格式正确; (9.624672,7.242244) 如图alert() 所以不知道为什么它不起作用。

【问题讨论】:

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


    【解决方案1】:

    google.maps.LatLng 构造函数接受两个数字作为参数。这行不通:

    var newLatLang = new google.maps.LatLng(newCoords);
    

    您需要将 newCoords 转换为两个数字。

    Convert String to latlng google maps

    Convert “[52.43242, 4.43242]” to google LatLng

    How do I get a pin on Google Maps using location from a variable?

    【讨论】:

    • ajax调用返回的数据是“13.444,98.7777”...为什么手动输入就不行了?
    • 那是一个字符串,不是两个数字。
    • 我同意这一点。本来以为这行得通,因为所有纬度/经度坐标都以该格式存储:) 感谢您指出,我会尝试!
    • 如果其他人遇到这个问题,拆分字符串:success: function(returnedData) { // alert(returnedData); var coordsArray = 返回数据.split(","); moveMarkerMap(coordsArray[0], coordsArray[1]); }
    猜你喜欢
    • 2020-08-13
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-23
    • 2013-09-15
    相关资源
    最近更新 更多