Google maps API v3 - Marker disappears after setPosition [duplicate]

旧城冷巷雨未停 提交于 2019-12-24 08:17:36

问题


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?

Code:

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

回答1:


function(data) {
   alert(data);
   position = new google.maps.LatLng(data);
   ..

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


来源:https://stackoverflow.com/questions/23854459/google-maps-api-v3-marker-disappears-after-setposition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!