簡體   English   中英

單擊外部html鏈接后,谷歌地圖v3打開信息窗口

[英]google maps v3 open infowindow on click of external html link

想知道是否有人可以幫助我,我已經設置了谷歌地圖,一切正常。 我唯一無法解決的方法是根據不在JS中的外部html鏈接打開基於ID的信息窗口。

function initialize() {
// Create the map 
// No need to specify zoom and center as we fit the map further down.
var map = new google.maps.Map(document.getElementById("map"), {
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDefaultUI: true
});
infowindow = new google.maps.InfoWindow();
// Custom markers
var icon = "img/marker.png";

// Define the list of markers.
// This could be generated server-side with a script creating the array.
var markers = [
    { val:0, lat: -40.149049, lng: 172.033095, title: "Title", html: "<div style='text-align:left'><h4 style='color:#0068a6;font-size:16px;margin:0px 0px 10px 0px;'>Title</h4><strong>Telephone</strong><br /><br />Address</div>" },
    { val:1, lat: -41.185765, lng: 174.827516, title: "Title", html: "<div style='text-align:left'><h4 style='color:#0068a6;font-size:16px;margin:0px 0px 10px 0px;'>Title</h4><strong>Telephone</strong><br /><br />Address</div>" },
];
// Create the markers ad infowindows.
for (index in markers) addMarker(markers[index]);
function addMarker(data) {
  // Create the marker
    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(data.lat, data.lng),
        map: map,
        title: data.title,
        icon: icon,
        id: data.val
    });

    // Create the infowindow with two DIV placeholders
    // One for a text string, the other for the StreetView panorama.
    var content = document.createElement("DIV");
    var title = document.createElement("DIV");
    title.innerHTML = data.html;
    content.appendChild(title);
    // Open the infowindow on marker click
    google.maps.event.addListener(marker, "click", function() {
        infowindow.setContent(content);
        infowindow.open(map, this);
        map.setCenter(this.position);
        console.log(this.id);
    });
}

// Zoom and center the map to fit the markers
// This logic could be conbined with the marker creation.
// Just keeping it separate for code clarity.
var bounds = new google.maps.LatLngBounds();
for (index in markers) {
    var data = markers[index];
    bounds.extend(new google.maps.LatLng(data.lat, data.lng));
}
map.fitBounds(bounds);
}

<p id="1">link to open marker</p>

任何幫助將不勝感激

理查德:)

<a href="javascript:show(7)">The Golden Goose</a>

然后在您的js中有一個函數來打開信息窗口(例如show()),該函數從該鏈接獲取屬性(打開ID 7)。


function show(id){
  myid = id;
  if(markers[myid]){
    map.panTo(markers[myid].getPoint());
    setTimeout('GEvent.trigger(markers[myid], "click")',500);
    map.hideControls();
  }
}

這就是我以前與v2中的一個標記管理器一起使用的功能。 您必須確保在設置每個標記時都設置了一個ID,然后才能調用它。

我確保(簡化事情)的一件事是確保映射標記集/數組與我在頁面上使用的sql結果完全相同。 這樣,使用id就是小菜一碟。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM