2012-10-09 38 views
0

我堅持在我的網站上實施豐富網頁摘要。 頁面遵守schema.org/MusicEvent標準。但我無法弄清楚如何提取ncenter中包含的座標對以填充html代碼中的經度和緯度元標籤。從谷歌地圖API到豐富的片段geocoords

var geocoder = new google.maps.Geocoder(); 
var map; 
var ncenter; 
function initialize() { 

    var address = document.getElementById("address").firstChild.data; 
    geocoder.geocode({'address':address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      ncenter = results[0].geometry.location; 
      var mapOptions = { 
       zoom:12, 
       center:ncenter, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      } 
      map=new google.maps.Map(document.getElementById("map"),mapOptions); 
      var marker = new google.maps.Marker({ 
       map:map, 
       position:ncenter 
      });    

     } else { 
      alert("Geocode ERROR: " + status); 
     } 
     }); 
} 
window.onload=initialize; 

此外,這不會與搜索引擎的蜘蛛,因爲他們不運行JS。 有沒有其他解決方案(基於PHP的查詢也許)?

回答

1

您需要使用JavaScript編寫元標記的內容,例如, (使用jQuery):

$('body').append('<div itemprop="geo">' + 
'<meta itemprop="latitude" content="' + ncenter.lat() + '" />' + 
'<meta itemprop="longitude" content="' + ncenter.lng() + '" />' + 
'</div>'); 

或者是的,你可以用PHP代替它。

+0

感謝您的回答,我終於選擇實施基於PHP的解決方案,這要歸功於XML地理編碼器。 我解析XML以查找經度和緯度,並用簡單的回聲填充元標記。然後,JS只需獲取元標記的內容值來構建座標對,並在該點創建一個具有中心和標記的地圖。 – Vektor88