Google Maps Javascript API:marker.setPosition 问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12662469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 10:58:38  来源:igfitidea点击:

Google Maps Javascript API: marker.setPosition Issue

javascriptgoogle-mapsmarker

提问by A Smith

It's probably something obvious... but I cannot see it. The following code works as expected for everything except the marker.setPosition action:

这可能很明显……但我看不到。除了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>

The "before_map" test area shows the new latitude and longitude values -- which update every 5 seconds.

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

I've made adjustments based on answers to similar questions but I'm still stuck.

我已经根据类似问题的答案进行了调整,但我仍然被卡住了。

回答by abhishek

setPosition is a method. You need to modify your code to

setPosition 是一种方法。您需要将代码修改为

marker.setPosition(myLatlng);

Please check whether it works.

请检查它是否有效。

回答by lccarrasco

marker.setPosition is a function, not a property, I think you want

marker.setPosition 是一个函数,而不是一个属性,我想你想要

marker.setPosition(myLatlng);

marker.setPosition(myLatlng);