0

我使用帶有大量標記的Google Maps API V3創建ASP.NET MVC3網站。每個標記都有一個InfoWindow,其中包含有關此地點的一些信息。

我希望每個標記都有一個直接鏈接來訪問我的網站直接在這個標記上,如http://www.mywebsite/1589。因此,用戶可以訪問網站,地圖以標記1589爲中心,其InfoWindows將會打開。

標記已經在地圖上,他們的InfoWindow已經顯示信息,但我不知道如何創建一個直接鏈接到標記...有人能幫助我嗎?如何創建直接鏈接以訪問特定的Google Maps API V3標記?

在此先感謝

+2

[例1](http://www.geocodezip.com/v3_MW_example_linktomarker.html?marker=1) ,[示例2](http://www.geocodezip.com/v3_MW_example_linktomarker.html?id=Marker%20Two),從邁克爾威廉姆斯的v2教程示例移植到v3 [本教程的這一頁](http:// econym.org.uk/gmap/linktothis.htm) – geocodezip

+0

@geocodezip您網站上的例子似乎正是我想要的;但是,我不知道如何做到這一點。我是否需要創建我的鏈接,如mywebsite.com/1539;請求這個標記的緯度/經度,然後將我的地圖放在這個標記上並打開InfoWindow?或者,如果我錯了,你可以發佈一個代碼樣本嗎? – AlexB

+0

查看示例的來源。您需要編寫代碼來處理URL中的查詢字符串。 – geocodezip

回答

2

的關鍵部分是:

  • 解析查詢字符串

    // skip the first character, we are not interested in the "?" 
    var query = location.search.substring(1); 
    
    // split the rest at each "&" character to give a list of "argname=value" pairs 
    var pairs = query.split("&"); 
    for (var i=0; i<pairs.length; i++) { 
        // break each pair at the first "=" to obtain the argname and value 
        var pos = pairs[i].indexOf("="); 
        var argname = pairs[i].substring(0,pos).toLowerCase(); 
        var value = pairs[i].substring(pos+1).toLowerCase(); 
    
        // process each possible argname - use unescape() if theres any chance of spaces 
        if (argname == "id") {id = unescape(value);} 
        if (argname == "marker") {index = parseFloat(value);} 
    } 
    
  • 加載標記後打開信息窗口,如果一個參數傳遞

    // ========= If a parameter was passed, open the info window ========== 
        if (id) { 
        if (idmarkers[id]) { 
         google.maps.event.trigger(idmarkers[id],"click"); 
        } else { 
         alert("id "+id+" does not match any marker"); 
        } 
        } 
        if (index > -1) { 
        if (index < gmarkers.length) { 
         google.maps.event.trigger(gmarkers[index],"click"); 
        } else { 
         alert("marker "+index+" does not exist"); 
        } 
        } 
    
  • 你可能也想在「鏈接」功能,在this example,其中規定了查詢字符串