2013-03-20 160 views
0

我正在嘗試使用ajax和php的組合動態地爲我的Google地圖添加標記。 代碼的第一部分將latlng發送到php文件。 PHP文件然後返回所需的標記位置。向Google地圖動態添加標記

當我提醒返回部分(警告測試以確保PHP處理的數據正確)時,它看起來不錯,但我似乎不能將返回的標記添加到我的地圖。

查看下面的代碼。
//將數據發送到URL(發送到PHP文件) //返回數據的位置標記(這是返回PHP文件產生)

非常感謝,

//將數據發送到URL

 var xmlHttp = getXMLHttp(); 
    xmlHttp.onreadystatechange = function() { 
    if (xmlHttp.readyState == 4) { 
    HandleResponse(xmlHttp.responseText); 
    }}      
    xmlHttp.open("POST",'MYPHPFILE',true); 
    xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
    xmlHttp.send("LATLON="+strLAT); 

//返回數據的位置標記

 var wxlatlng = new google.maps.LatLng(52,1); 
    var marker = new google.maps.Marker({ 
    position: wxlatlng, 
    map: map, 
    icon: '../../icons/flags/traffic_light_red.png', 
    title: 'TEST', }); 

//返回數據的位置標記

 var wxlatlng = new google.maps.LatLng(52,1.1); 
    var marker = new google.maps.Marker({ 
    position: wxlatlng, 
    map: map, 
    icon: '../../icons/flags/traffic_light_red.png', 
    title: 'TEST', }); 

//警告測試,以確保PHP處理的數據正確

 function HandleResponse(response) { 
      document.getElementById('ResponseDiv').innerHTML = response; 
      alert($('#ResponseDiv').text()); 
     }       

回答

0

,我發現我的問題的答案是使用PHP文件來創建標記XML文件,並通過jQuery加載XML文件響應

看到下面的代碼;

jQuery.get(YOURXMLFILE, function(data) { 
     jQuery(data).find("marker").each(function() { 
     var eachMarker = jQuery(this); 
     var markerCoords = new google.maps.LatLng(
     parseFloat(eachMarker.find("Lat").text()), 
     parseFloat(eachMarker.find("Lng").text()) 
     ); 
     var header = eachMarker.find("title").text(); 
     var content = eachMarker.find("Content").text(); 
     var wxicon = eachMarker.find("icon").text(); 
     //--------------------------------------------------------------------------   
     marker = new google.maps.Marker({ 
     position: markerCoords, 
     icon: wxicon, 
     map: map, 
     animation: google.maps.Animation.DROP, 
     title: header, 
     }); 
     }); 
     });