gpt4 book ai didi

google-maps-api-3 - 为地理编码结果自动寻找合适的缩放比例

转载 作者:行者123 更新时间:2023-12-04 11:58:01 25 4
gpt4 key购买 nike

我正在为我的位置服务应用程序使用谷歌地图和谷歌地理编码服务。我使用 Google 地理编码服务将地址转换为 lat/lng 位置。我的问题是如何像 maps.google.com 那样自动为某个地址找到合适的缩放比例。

例如,当我在 maps.google.com 中搜索街道时(例如 Cisitu Baru, Bandung ),它会以较小的缩放比例显示街道。当我搜索一个区域(例如 Bandung )时,它会显示更大的缩放。以及更大的省份缩放(例如 Jawa Barat/West Java ),等等。

我都试过了

var geocoder = new google.maps.Geocoder();
geocoder.geocode( {
'address': someAddress
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.dir(results);
//cut
map.panToBounds(results[0].geometry.bounds); //setting bound
//cut
}
});


//cut
map.panToBounds(results[0].geometry.viewports); //setting bound
//cut

(老实说,我仍然不知道 boundsviewport 之间的区别以及它们的用途,来自 code.google.com/apis/maps/documentation/javascript/geocoding.html)

但两者仍然没有以适当的缩放比例显示 map 。

现在,我使用这样的小技巧
var tabZoom =  {
street_address: 15,
route: 15,
sublocality: 14,
locality: 13,
country: 10
};
//cut
map.setCenter(results[0].geometry.location);
if (tabZoom[results[0].types[0]] != undefined){
map.setZoom(tabZoom[results[0].types[0]]);
} else {
map.zetZoom(10);
}
//cut

还有其他解决方案吗? (或者我还不知道的来自 Google Map API 的任何东西?)

谢谢!

最佳答案

使用 GLatLngBounds 类

一个例子:

// map: an instance of GMap2

// latlng: an array of instances of GLatLng

var latlngbounds = new GLatLngBounds( );

for ( var i = 0; i < latlng.length; i++ )
{
latlngbounds.extend( latlng[ i ] );
}

map.setCenter( latlngbounds.getCenter( ), map.getBoundsZoomLevel( latlngbounds ) );

^

诀窍是将需要在 map 上同时可见的所有点的列表添加到 GLatLngBounds 对象中。 Google Maps API 可以完成其余的数学运算。

或者在 v3 中,您可以使用 LatLngBounds 类(类似于 v2 中的 GLatLngBounds),链接: http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLngBounds

举个例子
最好看看这个: http://unicornless.com/code/google-maps-v3-auto-zoom-and-auto-center

关于google-maps-api-3 - 为地理编码结果自动寻找合适的缩放比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8785432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com