1
votes

This is driving me nuts. Using Google Maps v3 I am loading the map using php to determine lat long :

function initialize() {
    var latlng = "<?php echo $lat;?>,<?php echo $long;?>";
    console.log(latlng);
    var mapOptions = {
        center: new google.maps.LatLng(<?php echo $lat;?>,<?php echo $long;?>),
        zoom: 8,
        mapTypeId: google.maps.MapTypeId.ROADMAP 
    };
    var map = new google.maps.Map(document.getElementById("map_canvas")
                                       , mapOptions);
}

This works fine. However if I try to pass in the latlng var

center: new google.maps.LatLng(latlng),

the maps just shows a blue screen and is broken (no zoom or controls or anything). The console log and source look like it is generating the parameters correctly. Any ideas on what might be happening?

I did have initialize() on body but I moved it to the (document).ready in my script. This did not seem to make any difference for this problem.

2
shoudn't it be LatLng(lat, lng) -- code.google.com/apis/maps/documentation/javascript/…georg

2 Answers

5
votes

This is because

new google.maps.LatLng(<?php echo $lat;?>,<?php echo $long;?>)

is not the same as

new google.maps.LatLng("<?php echo $lat;?>,<?php echo $long;?>")

The quotes mean you are passing in a string literal to the LatLng constructor, which does not work.

4
votes
new google.maps.LatLng(12.3456, 12.3456);

is not the same as

var latlng = "12.3456,12.3456";
new google.maps.LatLng(latlng);

The former passes two numeric parameters into the function, which is what's expected. The latter passes one string into the function, which is an error.