googlemaps移动标记与来自ajax的lat/lng成功返回数据

google maps move marker with lat/lng from ajax success returned data

本文关键字:lat lng 成功 数据 返回 ajax 移动 googlemaps      更新时间:2023-09-26

尝试从sqldb中使用lat/long坐标在间隔上移动标记/映射。

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()所示所以不确定为什么它不起作用。

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

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

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

将字符串转换为latlng谷歌地图

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

如何使用变量中的位置在谷歌地图上获取密码?