Google maps api 3 自动为地理编码结果查找适当的缩放
我正在使用谷歌地图和谷歌地理编码服务为我的位置服务应用程序。我使用谷歌地理编码服务将地址转换为lat/lng位置。我的问题是如何像maps.google.com那样自动为某个地址找到合适的缩放 例如,当我在maps.google.com搜索一条街道时(例如万隆Cisitu Baru,Bandung),它将以较小的缩放比例显示该街道。当我搜索一个区域时(例如,Google maps api 3 自动为地理编码结果查找适当的缩放,google-maps-api-3,google-geocoder,Google Maps Api 3,Google Geocoder,我正在使用谷歌地图和谷歌地理编码服务为我的位置服务应用程序。我使用谷歌地理编码服务将地址转换为lat/lng位置。我的问题是如何像maps.google.com那样自动为某个地址找到合适的缩放 例如,当我在maps.google.com搜索一条街道时(例如万隆Cisitu Baru,Bandung),它将以较小的缩放比例显示该街道。当我搜索一个区域时(例如,万隆),它将显示更大的缩放。以及对该省进行更大的缩放(例如,爪哇巴拉特省/西爪哇省),等等 我两者都试过了 var geocoder = n
万隆
),它将显示更大的缩放。以及对该省进行更大的缩放(例如,爪哇巴拉特省/西爪哇省
),等等
我两者都试过了
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
}
});
及
(老实说,我仍然不知道bounds
和viewport
之间有什么区别,它们在code.google.com/api/maps/documentation/javascript/geocoding.html中有什么用途)
但两者仍然不能以适当的比例显示地图
现在,我用的是这样的小技巧
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
还有其他解决办法吗?(或者谷歌地图API中我还不知道的任何东西?)
谢谢 使用glatlingbounds类 例如:
// 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:GMap2的一个实例
//latlng:GLatLng实例的数组
var latlngbounds=新的GLatLngBounds();
对于(变量i=0;i
^
诀窍是将需要在地图上同时可见的所有点的列表添加到GLatLngBounds对象中。谷歌地图API可以完成剩下的计算
或者在v3中,您可以使用LatLngBounds类(类似于v2中的GLatLngBounds),链接: 例如
最好检查一下:使用结果几何体的视口。如果您的搜索结果没有特定的边界,则geometry.bounds将出现错误 “视口”为结果提供最佳视图
map.fitBounds(结果[0].geometry.viewport) 我不熟悉v2api。你能用API v3描述它吗?谢谢。在v3中,您可以使用LatLngBounds类Nice,我刚刚从您的示例URL中找到了
fitBounds
方法。我只做了map.fitBounds(结果[0].geometry.bounds)代码>它就像一个符咒。谢谢
// 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 ) );