Pet*_*rus 7 google-maps-api-3 google-geocoder
我正在为我的位置服务应用程序使用Google地图和Google地理编码服务.我使用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
}
});
Run Code Online (Sandbox Code Playgroud)
和
//cut
map.panToBounds(results[0].geometry.viewports); //setting bound
//cut
Run Code Online (Sandbox Code Playgroud)
(老实说,我仍然不知道什么之间的区别bounds,并viewport和他们有什么用途从code.google.com/apis/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
Run Code Online (Sandbox Code Playgroud)
还有其他解决方案吗?(或谷歌地图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 ) );
Run Code Online (Sandbox Code Playgroud)
^
诀窍是将需要在地图上同时可见的所有点的列表添加到 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
| 归档时间: |
|
| 查看次数: |
6356 次 |
| 最近记录: |