谷歌地图Javascript API:标记.marker.setPosition问题

3

很可能是一些显而易见的问题,但我没能看出来。以下代码在除了marker.setPosition操作外都正常工作:

<head>
<title>Test Simulation</title>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
  html { height: 100% }
  body { height: 100%; margin: 0; padding: 0 }
  #map_canvas { height: 100% }
</style>
<script type="text/javascript"  src="http://maps.googleapis.com/maps/api/js?key=x&sensor=false">
</script>

<script type="text/javascript">
  var marker = null;
  var myLatlng = null;
  var map = null;
  var image = null;
  var mapOptions = null

  function movemarker()
  {
    xmlhttp=new XMLHttpRequest();
    xmlhttp.open("GET","http://x.com/x13/?testloc=1",false);
    xmlhttp.send();

    var data = xmlhttp.responseText;
    vals = data.split(',');
    latv = parseFloat(vals[0]);
    lonv = parseFloat(vals[1]);
    myLatlng = new google.maps.LatLng(latv,lonv);

    var fld = document.getElementById('before_map');
    fld.innerText = ' ' + 'lat:' + latv + ', lon: ' + lonv + ' ' + myLatlng.toString();
    marker.setPosition = myLatlng;
  }

  function initialize()
  {
    myLatlng = new google.maps.LatLng(44.809122,-36.650391);
    mapOptions = 
    {
      center: myLatlng,
      zoom: 3,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map_canvas"),
      mapOptions);

    image = 'http://x.com/x_icon.gif';
    marker = new google.maps.Marker(
    {
      position: myLatlng,
      map: map,
      icon: image,
      title:"Hello World!"
    })

    window.setInterval("movemarker()",5000);
  }
</script>
</head>
<body onload="initialize(); ">
<div id="before_map">Test area...</div>
<div id="map_canvas" style="align:right; width:600; height:500"></div>

“before_map”测试区显示新的纬度和经度值,每5秒更新一次。

我根据类似问题的答案进行了调整,但仍然无法解决。

2个回答

26

setPosition是一个方法。您需要修改您的代码来

marker.setPosition(myLatlng);
请检查它是否有效。

10

marker.setPosition是一个函数,而不是一个属性,我认为你想要执行以下代码:

marker.setPosition(myLatlng);


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接