我正在使用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