google maps使用lat / lng从ajax成功返回数据移动标记

时间:2022-11-02 21:18:47

Trying to move marker/map on interval with lat/long coords from sql db.

尝试使用sql db中的lat / long coords间隔移动标记/贴图。

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

Setting the new google.maps.LatLng(14,41) in moveMarkerMap() will move it, and the returnedData shows in alert() but marker won't move when used with moveMarkerMap()

在moveMarkerMap()中设置新的google.maps.LatLng(14,41)会移动它,并且在alert()中显示的是returnData,但是当与moveMarkerMap()一起使用时,marker将不会移动

The returned string from ajax is correct format; (9.624672,7.242244) as shown in alert() so not sure why its not working.

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

1 个解决方案

#1


2  

The google.maps.LatLng constructor takes two numbers for arguments. This won't work:

google.maps.LatLng构造函数为参数提供两个数字。这不起作用:

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

You need to convert newCoords into two numbers.

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

Convert String to latlng google maps

将String转换为latlng谷歌地图

Convert “[52.43242, 4.43242]” to google LatLng

将“[52.43242,4.43242]”转换为谷歌LatLng

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

如何使用变量中的位置在Google地图上获取图钉?

#1


2  

The google.maps.LatLng constructor takes two numbers for arguments. This won't work:

google.maps.LatLng构造函数为参数提供两个数字。这不起作用:

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

You need to convert newCoords into two numbers.

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

Convert String to latlng google maps

将String转换为latlng谷歌地图

Convert “[52.43242, 4.43242]” to google LatLng

将“[52.43242,4.43242]”转换为谷歌LatLng

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

如何使用变量中的位置在Google地图上获取图钉?