2015-05-26 105 views
0

我遇到了一些問題。在這個網站(http://hyp.altervista.org/home.html)我想去頁面:位置,它應該出現我的谷歌地圖,由AJAX函數調用。通過ajax調用Google Maps Api不起作用

我有getLocation.php通過AJAX調用:

調用
<?php echo '<div id="map-canvas"></div>'; ?> 

同時腳本:

window.onload = loadScript; 
       $.ajax({ 
       method: "POST", 
       //dataType: "json", //type of data 
       crossDomain: true, //localhost purposes 
       url: "../getLocation.php", //Relative or absolute path to file.php file 
       success: function(response) { 


        $(".contenuti").html(" "+response); 

       }, 
       error: function(request,error) 
       { 
        console.log("Error"); 
       } 
      }); 

但我不能看地圖。我把谷歌的腳本家:

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' + 
      '&signed_in=true&callback=initialize'; 
     document.body.appendChild(script); 
     } 

回答

1

嘗試移動loadScript()爲谷歌地圖API在windows.onload的loadScript()之前,並記住添加initialize();在腳本的末尾

+0

非常感謝! –

1

更換地圖格在

<div id='map-canvas' style="display:block;height:600px;"></div> 

而且在腳本的末尾寫

initialize(); 
+0

謝謝,但它出現:「Uncaught ReferenceError:google is not defined」on console。 –

+0

我試圖把window.onload = loadScript();而不是window.onload = loadScript;但有時會工作,有時會說:Uncaught TypeError:無法讀取屬性'offsetWidth'爲空 –

+0

http://stackoverflow.com/questions/14229695/google-maps-api-throws-uncaught-referenceerror-google-is-not-defined -only-whe –

相關問題