问题描述
尝试使用来自 sql db 的经纬度坐标移动标记/地图.
Trying to move marker/map on interval with lat/long coords from 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() 一起使用时标记不会移动
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()
ajax返回的字符串格式正确;(9.624672,7.242244) 如 alert() 所示所以不知道为什么它不起作用.
The returned string from ajax is correct format; (9.624672,7.242244) as shown in alert()so not sure why its not working.
推荐答案
google.maps.LatLng 构造函数接受两个数字作为参数.这行不通:
The google.maps.LatLng constructor takes two numbers for arguments. This won't work:
var newLatLang = new google.maps.LatLng(newCoords);
您需要将 newCoords 转换为两个数字.
You need to convert newCoords into two numbers.
将[52.43242, 4.43242]"转换为 google LatLng一个>
如何如何使用来自变量的位置在 Google 地图上获得图钉?
这篇关于谷歌地图移动标记与 lat/lng 从 ajax 成功返回数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!