我正在使用jquery ajax從解析xml文檔的php腳本中獲取一些結果。之後,ajax打電話給我顯示從谷歌api地圖,在該PHP腳本創建一個div(這是我的問題)。將谷歌地圖加載到由ajax加載的div
是否有可能將地圖API加載到在php中創建的<div id="map-canvas">
,在ajax調用之後?
PHP文件
... $xmlData = simplexml_load_file("using an http service");
echo "<div id='map-canvas'></div>"; //LOAD MAPP HERE!
echo "<ul>";
foreach($xmlData->StopTimes->StopTime as $stopTime){
echo "<li>",$stopTime->attributes()->Hour,"</li>";
}
echo "</ul>";
}
AJAX FILE
...
//station click, show next stops
$('.station_link').click(function(){
var station_text = $(this).text();
$.ajax({ //make the ajax call
type: "POST", //use POST/GET
url: "../server_side/nextStop.php", //file to send data
data: {poststation : station_text}, //postlink -> php($_POST) value/text -> jquery var value
success: function(data){ //on success, do something
$('#next_stop').html(data);
$('#next_stop').show();
//Google maps API
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
}
function loadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=TRUE_OR_FALSE&callback=initialize";
document.body.appendChild(script);
}
loadScript();
}
});
});
});
這是我已經嘗試過什麼(以及更多:P),但我甚至不知道如果它只是錯。
任何幫助或建議,將不勝感激!
- 更新 -
像這樣簡單:
JavaScript文件
//station click, show next stops
$('.station_link').click(function(){
var station_text = $(this).text();
$.ajax({ //make the ajax call
type: "POST", //use POST/GET
url: "../server_side/nextStop.php", //file to send data
data: {poststation : station_text}, //postlink -> php($_POST) value/text -> jquery var value
success: function(data){ //on success, do something
$('#next_stop').html(data);
$('#next_stop').show();
// Google maps API
var mapOptions = {
center: new google.maps.LatLng(37.7831,-122.4039),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}
});
});
});
而剛剛加入這個<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
到HTML文件。
這就是我whanted,它正在加載<div id="map-canvas">
,因爲它應該。
Ty for help反正! 對不起,如果我以前沒有提出我的觀點,我有點新手;)
哎喲,這很討厭。如果加載時間超過2000毫秒會怎麼樣? – BenLanc