Google maps api 3 谷歌地图多信息窗口不工作
我尝试使用javascript向google地图添加多个标记和infowindow。代码如下:Google maps api 3 谷歌地图多信息窗口不工作,google-maps-api-3,google-maps-markers,infowindow,Google Maps Api 3,Google Maps Markers,Infowindow,我尝试使用javascript向google地图添加多个标记和infowindow。代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <title>Google Maps Multiple Markers</title> <script src="ht
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
<script type="text/javascript">
var locations = [ '1961 East Mall Vancouver BC',
'2366 Main Mall Vancouver BC', '2053 Main Mall, Vancouver, BC ' ];
var map = new google.maps.Map(document.getElementById('map'), {
zoom : 14,
center : new google.maps.LatLng(49.26526, -123.250541),
mapTypeId : google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
var geocoder = new google.maps.Geocoder();
geocoder
.geocode(
{
'address' : locations[i]
},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map : map,
position : results[0].geometry.location
});
} else {
alert('Geocode was not successful for the following reason: '
+ status);
}
});
//add info window
google.maps.event.addListener(marker, 'click',
(function(marker, i) {
return function() {
infowindow.setContent(locations[i]);
infowindow.open(map, marker);
}
})(marker, i));
//end of adding info window
}//end of for loop
</script>
</body>
</html>
当点击时,我只会得到一个没有弹出信息窗口的标记
若我在代码块上面注释,我将得到三个标记,但也不会弹出信息窗口
我哪里出错了
谢谢。地理编码器是异步的,您需要在地理编码器回调中添加信息窗口。在您当前拥有它的位置,它将在定义任何标记之前运行。我想您可能会遇到javascript错误。地理编码器是异步的,您需要在地理编码器回调中添加infowindow。在您当前拥有它的位置,它将在定义任何标记之前运行。我想您可能会遇到javascript错误。这可以:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
<script type="text/javascript">
var locations = [ '1961 East Mall Vancouver BC',
'2366 Main Mall Vancouver BC', '2053 Main Mall, Vancouver, BC ' ];
var map = new google.maps.Map(document.getElementById('map'), {
zoom : 14,
center : new google.maps.LatLng(49.26526, -123.250541),
mapTypeId : google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker;
var i;
$(document).ready(function() {
initialize();
});
function initialize(){
setMarkers(map,locations);
}
function setMarkers(map, address) {
for ( var i = 0; i < address.length; i++) {
setMarker(map, address[i])
}
}
function setMarker(map, address) {
geocoder = new google.maps.Geocoder();
geocoder
.geocode(
{
'address' : address
},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker(
{
position : results[0].geometry.location,
map : map
});
google.maps.event.addListener(marker,
"click", function() {
infowindow.setContent(address);
infowindow.open(map, marker);
});
} else {
alert("Geocode was not successful for the following reason: "
+ status);
}
});
}
</script>
</body>
</html>
谷歌地图多个标记
var位置=['1961年温哥华东广场BC',
‘公元前2366年温哥华主街’、‘公元前2053年温哥华主街’;
var map=new google.maps.map(document.getElementById('map'){
缩放:14,
中心:新google.maps.LatLng(49.26526,-123.250541),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var infowindow=new google.maps.infowindow();
var标记;
var i;
$(文档).ready(函数(){
初始化();
});
函数初始化(){
设置标记(地图、位置);
}
函数设置标记(映射、地址){
对于(变量i=0;i
多亏了这一功能:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
<script type="text/javascript">
var locations = [ '1961 East Mall Vancouver BC',
'2366 Main Mall Vancouver BC', '2053 Main Mall, Vancouver, BC ' ];
var map = new google.maps.Map(document.getElementById('map'), {
zoom : 14,
center : new google.maps.LatLng(49.26526, -123.250541),
mapTypeId : google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker;
var i;
$(document).ready(function() {
initialize();
});
function initialize(){
setMarkers(map,locations);
}
function setMarkers(map, address) {
for ( var i = 0; i < address.length; i++) {
setMarker(map, address[i])
}
}
function setMarker(map, address) {
geocoder = new google.maps.Geocoder();
geocoder
.geocode(
{
'address' : address
},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker(
{
position : results[0].geometry.location,
map : map
});
google.maps.event.addListener(marker,
"click", function() {
infowindow.setContent(address);
infowindow.open(map, marker);
});
} else {
alert("Geocode was not successful for the following reason: "
+ status);
}
});
}
</script>
</body>
</html>
谷歌地图多个标记
var位置=['1961年温哥华东广场BC',
‘公元前2366年温哥华主街’、‘公元前2053年温哥华主街’;
var map=new google.maps.map(document.getElementById('map'){
缩放:14,
中心:新google.maps.LatLng(49.26526,-123.250541),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
var infowindow=new google.maps.infowindow();
var标记;
var i;
$(文档).ready(函数(){
初始化();
});
函数初始化(){
设置标记(地图、位置);
}
函数设置标记(映射、地址){
对于(变量i=0;i
多亏了