2010-11-11 66 views
1

在使用地理編碼初始化谷歌地圖時遇到一些問題。第一個問題是在$ gmap字符串中使用任何逗號,第二個問題是獲取「gmap_initialize未定義」。我知道這個功能以外的所有東西都是正確的,任何想法?在加載頁面時將地址傳遞給谷歌地圖

<?php $gmap = "Prague, Czech Republic"; ?> 

<script type="text/javascript"> 
function gmap_initialize() { 
var geocoder = new google.maps.Geocoder(); 
geocoder.geocode({ 'address': <?php echo $gmap; ?>}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
    var options = { 
     zoom: 16, 
     position: results[0].geometry.location, 
     center: results[0].geometry.location, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 
    var map = new google.maps.Map(document.getElementById("map_canvas"), options); 

    var marker = new google.maps.Marker({ 
     map: map, 
     position: results[0].geometry.location 
    }); 
    } else { 
    alert("Geocode was not successful for the following reason: " + status); 
    } 
}); 

} 
</script> 

回答

2

你看過腳本的輸出嗎?

我的猜測是,它看起來是這樣的:

geocoder.geocode({ 'address': Prague, Czech Republic}, function(results, status) {

在你的PHP腳本,你可能真正想要的是這樣的:

geocoder.geocode({ 'address': '<?php echo $gmap; ?>'}, function(results, status) {

,你可能會想逃跑單引號出$gmap變量。

+0

謝謝Broady,這是修復(忘了更新我的答案)。好的呼喚也逃跑。 – Alex 2010-11-15 08:16:39

0

這看起來像一個JavaScript錯誤。說這個函數(看起來是正確的)沒有被定義。也許調用gmap_initialize發生在這個定義之前?

0

通常這個錯誤出現使用JavaScript時,頁面上的其他東西是不完全正確,如單缺少結束標記...

您確認符合W3C的代碼?