问题
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>
回答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]));
来源:https://stackoverflow.com/questions/23854459/google-maps-api-v3-marker-disappears-after-setposition