放大谷歌地图API - 城市与国家不同 [英] zoom in google maps API - different for city vs country

查看:119
本文介绍了放大谷歌地图API - 城市与国家不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以..
我遇到了一个可能非常普遍的问题。

刚刚开始实施google maps api,以下是我的代码将城市解析为lat / lang并将地图居中:

 函数SetMapAddress(address){//London,UK例如
var geocoder = new google.maps.Geocoder();
if(geocoder){
geocoder.geocode({'address':address},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
var loc = results [0] .geometry.location;
document.map.setCenter(new google.maps.LatLng(loc.lat(),loc.lng(),13));
}

问题是我传递了静态缩放(13)。



如果有人输入国家名称,我想缩小更多。如果是城市,我想放大更多等。



我能想到的唯一办法是为每个城市和国家找出合适的缩放比例,将它们存储在一些散列中,并尝试找出哪些是正在使用的,以便通过适当的放大。



也许google想到了一个更智能的方法?

解决方案

地理编码器返回建议d视口



你可以在你的SetMapAddress函数中使用它:

 函数SetMapAddress(address){//London,UK,例如
var geocoder = new google.maps.Geocoder();
if(geocoder){
geocoder.geocode({'address':address},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
document.map.fitBounds(results [0] .geometry.viewport);
}
});
}
}


so.. i run into a possibly very common problem.

just started implementing google maps api, and following is my code to resolve a city into lat/lang and center the map there:

function SetMapAddress(address) {  // "London, UK" for example 
  var geocoder = new google.maps.Geocoder();
  if (geocoder) {
     geocoder.geocode({ 'address': address }, function (results, status) {
     if (status == google.maps.GeocoderStatus.OK) {
        var loc = results[0].geometry.location;
        document.map.setCenter(new google.maps.LatLng(loc.lat(),loc.lng(), 13));
     }

the problem is that i am passing a static zoom (13).

if someone types a name of a country, i'd like to zoom out more. if it is a city, i'd like to zoom in more etc..

the only thing i can think of is to figure out appropriate zoom for every city and country, store them in some hash, and try to figure out which is being used, to pass appropriate zoom.

maybe google thought of a more intelligent approach ?

解决方案

The geocoder returns a "recommended" viewport

Which you can use in your SetMapAddress function like this:

 function SetMapAddress(address) {  // "London, UK" for example 
   var geocoder = new google.maps.Geocoder();
   if (geocoder) {
      geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          document.map.fitBounds(results[0].geometry.viewport);
        }
      });
   }
 }

这篇关于放大谷歌地图API - 城市与国家不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆