0

好吧,我希望這是有道理的。下面是我的谷歌地圖的代碼,它工作的很好(不是最乾淨的,但它的工作)。我怎樣才能在地圖之外創建一個HTML鏈接,以便爲我打開信息框?例如,我希望能夠這樣做:打開谷歌地圖信息框利用鏈接

<a href="#map" onclick="openInfo(2)">More Info</a> 

並且打開標記2的信息框,這將是星期二。希望這是有道理的。

<script> 
jQuery(function($) { 
// Asynchronously Load the map API 
var script = document.createElement('script'); 
script.src = "//maps.googleapis.com/maps/api/js?sensor=true&callback=initialize&key=APIKEY"; 
document.body.appendChild(script); 
}); 
var gmarkers = []; 
function initialize() { 
var map; 
var bounds = new google.maps.LatLngBounds(); 
var mapOptions = { 
    mapTypeId: 'roadmap' 
}; 

// Display a map on the page 
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); 
map.setTilt(45); 

// Multiple Markers 
var markers = [ 

     ['1', LAT,LONG],  
     ['2', LAT,LONG],  

]; 


// Info Window Content 
var infoWindowContent = [ 

     ['<strong>Monday - 7:00pm</strong>'],  
     ['<strong>Tuesday - 6:00pm</strong>'], 
]; 

// Display multiple markers on a map 
var infoWindow = new google.maps.InfoWindow(), marker, i; 

// Loop through our array of markers & place each one on the map 
for(i = 0; i < markers.length; i++) { 
    var position = new google.maps.LatLng(markers[i][1], markers[i][2]); 
    bounds.extend(position); 
    marker = new google.maps.Marker({ 
     position: position, 
     map: map, 
     title: markers[i][0] 
    }); 
    // Allow each marker to have an info window  
    google.maps.event.addListener(marker, 'click', (function(marker, i) { 
     return function() { 
      infoWindow.setContent(infoWindowContent[i][0]); 
      infoWindow.open(map, marker); 
     } 
    })(marker, i)); 

    // Automatically center the map fitting all markers on the screen 
    map.fitBounds(bounds); 
} 



// Override our map zoom level once our fitBounds function runs (Make sure it only runs once) 
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) { 
    this.setZoom(11); 
    google.maps.event.removeListener(boundsListener); 
}); 
var opt = { minZoom: 9, maxZoom: 12 }; 
map.setOptions(opt); 

} 
</script> 
+0

Open Info Window我覺得這篇文章對您會有幫助:https://stackoverflow.com/questions/18333679/google-maps-open-info-window-after-click-on-a -link – VA79

+0

似乎不是工作,我得到以下錯誤: VM10743:1未捕獲的ReferenceError:標記沒有定義 –

回答

0

你必須將它傳遞您的實際標記......你與標記創建標記=新google.maps.Marker({...你必須存儲你要定位的一個,和。傳遞到的onclick觸發funtion

+0

我希望能夠針對任何人與我分配給他們的定製ID,所以例如,如果我有一個標記ID 34,那麼我希望能夠爲該信息窗口調用標記[34]。那有意義嗎?我不想依靠標記的自動0,1,2,3編號。 –