[英]Google map infowindow ajax load content
我無法從ajax調用中加載信息窗口,數據來自json數組,這是循環代碼...
function getResults(map){
//remove all existing markers
deleteOverlays();
bounds = new google.maps.LatLngBounds();
//set search center
c = map.center;
var cLat = c.lat().toFixed(6);
var cLng = c.lng().toFixed(6);
//get search radius
var radius = $("#radius option:selected").val()
$.ajax({
url:"ajax/search_radius.php?lat="+cLat+"&lng="+cLng+"&radius="+radius,
cache:false,
async:true,
type:"POST",
dataType: "json",
success:function(data){
var l = data.markers.length;//alert(l);
for (i = 0; i < l; i++) {
var point = new google.maps.LatLng(data.markers[i][3], data.markers[i][4]);
var pin = 'images/pins/' + data.markers[i][1] + '.png';
//alert(pin);
marker = new google.maps.Marker({
position: point,
panControl: false,
map:map,
icon:pin
});
bounds.extend(point);
map.fitBounds(bounds);
//add to valid array
markersArray.push(marker);
google.maps.event.addListener(marker, 'click', function() {
infowindow.close();
load_content(this,'5');
});
}
}
});
};
這是加載內容的功能
function load_content(marker, id){
$.ajax({
url: 'ajax/get_infowindow_content.php?id='+id,
cache:true,
dataType:"html",
success: function(data){
infowindow.setContent(data);
infowindow.open(map, marker);
}
});
}
我遇到的問題是將標記的ID傳遞給函數(上面顯示了即使在運行php文件時顯式設置ID仍然返回undefined,例如
load_content(this,'5');
我也試過了..(還有其他變化)
load_content(this, data.markers[i][0]);
修改為以下內容,該內容仍返回未定義的ID
google.maps.event.addListener(marker, 'click', function() {
alert (id); //returns undefined
return $.ajax({
type: "POST",
url: "ajax/get_infowindow_content.php?",
data:{
'id':id
},
success: function(data){
infowindow.setContent(data);
infowindow.setOptions({
position: marker.getPosition()
});
infowindow.open(map, marker);
}
});
//infowindow.close();
//load_content(this,'5');
});
試試這個,使用return
return $.ajax({
type: "POST",
url: "url",
data:{
'id':id
},
success: function(data){// function to be called if the request succeeds
infowindow.setContent(data); //set the contents on infowindow
infowindow.setOptions({
position: marker.getPosition()
});
infowindow.open(map, marker);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.