【问题标题】:Google maps API v3 - Marker disappears after setPosition [duplicate]Google maps API v3 - setPosition后标记消失[重复]
【发布时间】:2014-05-25 10:47:22
【问题描述】:

我的地图标记有问题。地图和标记在初始页面加载时加载良好。但是当我尝试更新标记位置时,它就会消失。警报窗口给出正确的坐标。我在这里做错了什么?

代码:

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

【问题讨论】:

  • alert(data) 显示什么?
  • GPS 坐标。它是从 PHP 文件中获取的。示例:51.8234875663, 5.79311138367 所以这很奇怪。在“数据”中,存在正确的 GPS 值。但是,当我将这些 GPS 坐标发送到标记时,它就消失了。我也尝试将 map.setcenter 设置为数据的值,但随后地图变为灰色。
  • 它(可能)是“正确”的 GPS 值,但从 google.maps.LatLng 的角度来看,它只不过是一个字符串。
  • @geocodezip,在接受答案两小时后标记为重复没有任何意义。
  • 不是重复的吗?

标签: javascript jquery google-maps-api-3 marker


【解决方案1】:
function(data) {
   alert(data);
   position = new google.maps.LatLng(data);
   ..

看起来非常错了; data 是一个字符串,可能包含"lat, lng"google.maps.LatLng 不能用它来初始化。 new google.maps.LatLng 在参数中需要一对 number 类型,例如 (number, number)

改为这样做:

data = data.split(',');
position = new google.maps.LatLng(parseFloat(data[0]), parseFloat(data[1]));

【讨论】:

  • 当然!我没有想到这一点,因为具有相同信息的初始加载有效。 Jquery get 将其作为字符串返回。干!谢谢!
猜你喜欢
  • 2013-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 2011-09-05
  • 2016-11-06
相关资源
最近更新 更多