Google maps API v3 - Marker在setPosition后消失 [英] Google maps API v3 - Marker disappears after setPosition
问题描述
我遇到了地图标记问题。在初始页面加载的地图和标记加载罚款。但是当我尝试更新标记位置时,它就会消失。警报窗口提供了正确的结果。
I'm having a problem with the maps markers. The map and marker load fine on the inital pageload. But when I try to update the marker location it simply disappears. The Alert window gives the correct coördinates. What am I doing wrong here?
代码:
<script>
var myCenter=new google.maps.LatLng(<?php echo $loc; ?>);
function initialize()
{
var mapProp = {
center:new google.maps.LatLng(<?php echo $loc; ?>),
zoom:15,
mapTypeId:google.maps.MapTypeId.ROADMAP,
};
var map=new google.maps.Map(document.getElementById("googleMap")
,mapProp);
var marker=new google.maps.Marker({
position:myCenter,
});
marker.setMap(map);
setInterval(function(){
jQuery.get('loc.php?v=<?php echo $_GET['voertuignummer'];?>', function(data) {
alert(data);
position = new google.maps.LatLng(data);
marker.setPosition(position);
map.setCenter(position);
});
}, 15000);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
推荐答案
function(data) {
alert(data);
position = new google.maps.LatLng(data);
..
看起来非常错误; data
是一个字符串,可能包含lat,lng
,其中 google.maps。 LatLng
不能使用初始化。 new google.maps.LatLng
在参数中需要一对类型数字
,例如(number,number)
。
Looks very wrong; data
is a string, probably containing "lat, lng"
, which google.maps.LatLng
cannot be initialized with. new google.maps.LatLng
requires a pair of type number
in the arguments, like (number, number)
.
执行此操作:
Do this instead :
data = data.split(',');
position = new google.maps.LatLng(parseFloat(data[0]), parseFloat(data[1]));
这篇关于Google maps API v3 - Marker在setPosition后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!