【发布时间】:2012-02-05 19:28:31
【问题描述】:
我正在为我的位置服务应用程序使用 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
}
});
和
//cut
map.panToBounds(results[0].geometry.viewports); //setting bound
//cut
(老实说,我仍然不知道 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
还有其他解决方案吗? (或者我还不知道的来自 Google Map API 的任何东西?)
谢谢!
【问题讨论】:
标签: google-maps-api-3 google-geocoder