谷歌地图API -多个信息窗口和自动定心
Google Maps API - multiple info windows and automatic centering
我一直在拼凑来自互联网(包括StackOverflow)的各种代码,我有一个工作地图(几乎),从数组中地理编码邮政编码,并为每个创建信息窗口。
两个问题:
1)我的信息窗口,它应该从另一个数组中获取文本,总是使用最后一个数组值
我不能自动把地图调到中间。我使用了一些在其他情况下工作的代码,但在我的代码中没有。代码很容易理解:
var geocoder = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById('map'), {
//zoom: 10,
//center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var postcodes = [
"EH14 1PR",
"KY7 4TP",
"IV6 7UP"
];
var descriptions = [
"Slateford",
"Cortachy",
"Marybank"
];
var markersArray = [];
var infowindow = new google.maps.InfoWindow();
var marker, i, address, description;
for(var i = 0; i < postcodes.length; i++) {
address = postcodes[i];
description = descriptions[i];
geocoder.geocode(
{
'address': address,
'region' : 'uk'
},
function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
markersArray[i] = marker;
console.log(markersArray[i]);
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(description);
infowindow.open(map, marker);
}
})(marker, i));
} else {
alert("Geocode was not successful for the following reason: " + status);
return false;
}
}
);
var bounds = new google.maps.LatLngBounds();
$.each(markersArray, function (index, marker) {
bounds.extend(marker.position);
});
map.fitBounds(bounds);
console.log(bounds);
}
任何想法吗?问题似乎与计数器i在geocode函数中的值有关。
任何帮助都非常感谢!
1)我的信息窗口,它应该从另一个数组的文本,始终使用最后一个数组值
我不能自动把地图调到中间。我用了一点代码可以在其他情况下工作,但在我的代码。
1)是的,这是一个闭包问题。这就是我如何绕过它的方法。
我创建了一个对象来存储我将要使用的所有属性。在您的示例中,我将使用postcode
和description
。
function location(postalcode, desc){
this.PostalCode = postalcode;
this.Description = desc;
}
现在做一个快速循环,将所有的location
对象添加到数组中。
var locations = [];
for(var i = 0; i < postcodes.length; i++) {
locations.push(new location(postcodes[i], descriptions[i]));
}
将geocode功能提取到其自己的函数中,并使用参数接受location
对象。然后,您可以循环location
对象数组并分别对每个对象进行地理编码。因此,现在在构建和发送请求时,邮政编码和描述都在范围内。
function GeoCode(singleLocation){
geocoder.geocode(
{
'address': singleLocation.PostalCode,
'region' : 'uk'
},
function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
//quick and dirty way
bounds.extend(marker.position);
map.fitBounds(bounds);
markersArray[i] = marker;
console.log(markersArray[i]);
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(singleLocation.Description);
infowindow.open(map, this); //this refers to the marker
});
} else {
alert("Geocode was not successful for the following reason: "
+ status);
return false;
}
}
);
}
2)正如您在上面看到的那样,这样做的快速而肮脏的方法是扩展和适合地理代码回调函数内部的边界。这将导致fitBounds
函数被多次调用,如果您只有几个标记,这并不是什么大问题,但如果您有数百或数千个标记,则会导致问题。在这种情况下,正确的方法是创建一个异步循环函数。你可以在我之前的一个回答中看到一个例子。
下面是基于您的示例的代码的功能示例。
两个问题:
1)我的信息窗口,它应该从另一个数组的文本,始终使用最后一个数组值
我不能自动把地图调到中间。我用了一点代码可以在其他情况下工作,但在我的代码。
答案:
1)这是因为这里很可能有闭包问题。
2) center将定义地图的中心点,但在你的代码中你已经注释了这一点//center: new google.maps.LatLng(-33.92, 151.25
),删除这一行的注释将中心映射到纬度-33.92和经度151.25。
使用如下:
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 防止Iframe窗体在新窗口中打开
- 内部分区字体大小获胜'调整浏览器窗口大小时不会随媒体查询而更改
- 调整窗口大小时,可拖动的对象会出现在容器外部
- Javascript排序的图像弹出窗口..可以't单独弹出
- 窗口大小html css
- 弹出窗口出现,然后退出
- 为什么不是't窗口.恢复正常工作吗?(javascript/jquery)
- Javascript更新孙窗口中的表单元素
- 如何在选项卡上定义属性'的主窗口对象
- CSS-若窗口太小,滚动条会出现在“表格”单元格上
- 召回窗口加载事件 - javascript
- 为什么不显示警报窗口
- 如何使用modalDialog来显示可以在所有浏览器中工作的弹出窗口
- Chrome应用程序调整窗口大小保持纵横比
- 新选项卡被弹出窗口阻止程序阻止
- 如何访问UIWebView'的子窗口上下文
- 刷新父窗口后无法关闭窗口
- 自动关闭弹出窗口的Javascript
- 如何使用特定大小的浏览器窗口打开我的页面