1

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>
Ronald
  • 17
  • 1
  • 6
  • What does `alert(data)` show? – Anto Jurković May 25 '14 at 10:52
  • the GPS coördinates. It's obtained from a PHP file. Example: 51.8234875663, 5.79311138367 So that's the strange thing. In 'data', the correct GPS value is present. However, when I sent those GPS coördinates to the marker, it disappears. I also tried map.setcenter to the value of data, but then the map turns grey. – Ronald May 25 '14 at 10:58
  • It is (probably) the "correct" GPS value, but seen from a google.maps.LatLng point of view, it is nothing but a string. – davidkonrad May 25 '14 at 11:07
  • @geocodezip, marking as duplicate two hours after an accepted answer does not make any sense. – davidkonrad May 25 '14 at 14:16
  • Is it not a duplicate? – geocodezip May 25 '14 at 14:31

1 Answers1

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]));
davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • Of course! I didn't think of that because the initial load with the same info worked. Jquery get returns it as a string. Do'h! Thanks! – Ronald May 25 '14 at 11:14