javascriptgoogle-mapsmarker

Google Maps Javascript API: marker.setPosition Issue


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

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

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


Solution

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

    marker.setPosition(myLatlng);
    

    Please check whether it works.