2014-02-24 21 views
0

我有這對我的PHP文件的谷歌地圖Javascript API第3不工作

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
    <script> 
    var geocoder; 
var map; 
function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-34.397, 150.644); 
    var mapOptions = { 
    zoom: 15, 
    center: latlng 
    } 
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); 
} 

function codeAddress(address) { 
    geocoder.geocode({ 'address': address}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 
    } else { 
     alert('Geocode was not successful for the following reason: ' + status); 
    } 
    }); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 
</script> 

,並在頁面我有一個模式

<div id="map-canvas"></div> 

股利但是地圖並沒有顯示在模式...我沒有在Opera控制檯上出現錯誤,當我嘗試通過控制檯調用函數[initialize()和/或codeAddress(「西班牙」)]時,它說未定義

我創建了頁面僅適用於testin的地圖克,它工作得很好。 有什麼建議嗎? == ==編輯添加 的高度,以我的CSS文件後的地圖顯示着一些問題 http://postimg.org/image/mhj9awdez/ 只有第二初始化後的地圖顯示正確(初始化必須與開模做)

+0

[代碼沒有問題](http://jsfiddle.net/andyuws/Csk95/)。 – Andy

+0

地圖對我來說很不錯。 – user2524908

+2

順便說一句,確保您將寬度和高度添加到'map-canvas'元素,無論是內聯還是使用CSS。 – Andy

回答

0

您需要在加載js文件後調用initialize()函數... 我認爲這會幫助您 < https://maps.googleapis.com/maps/api/js?v=3.exp & sensor =假&'+ 'callback = initialize'; >>

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Asynchronous Loading</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <style> 
     html, body, #map-canvas { 
     height: 100%; 
     margin: 0px; 
     padding: 0px 
     } 
    </style> 
    <script> 
function initialize() { 
    var mapOptions = { 
    zoom: 8, 
    center: new google.maps.LatLng(-34.397, 150.644) 
    }; 

    var map = new google.maps.Map(document.getElementById('map-canvas'), 
     mapOptions); 
} 

function loadScript() { 
    var script = document.createElement('script'); 
    script.type = 'text/javascript'; 
    script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&' + 
     'callback=initialize'; 
    document.body.appendChild(script); 
} 

window.onload = loadScript; 

    </script> 
    </head> 
    <body> 
    <div id="map-canvas"></div> 
    </body> 
</html> 
+0

仍然無法正常工作。當我調用codeAddress函數時,它在控制檯上顯示「Uncought TypeError:Can not call method'geocode'of undefined」,如果我調用initialize(),它會給我「ReferenceError:google沒有定義」 –

+0

您可以將地圖設置爲iframe也許你有陽極js文件什麼是與你的地圖js衝突..我有西馬問題我解決它與iframe – VostanAzatyan