如何动态设置Google地图自定义缩放级别? [英] How to set google map custom zoom level dynamically?

查看:842
本文介绍了如何动态设置Google地图自定义缩放级别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用谷歌地图。根据要求,我需要设置不同的缩放级别,这取决于我的搜索查询。如果在国家地图上有多个位置,那么地图应该将焦点放在国家。其他情况是,如果城市中有不同的位置标记,那么地图应该集中到城市级别。

var geoCoder = new GClientGeocoder();
geoCoder.setViewport(map.getBounds());
geoCoder.getLocations('searchquery',function(latlng){
if(latlng.Placemark.length> 0){
var box = latlng.Placemark [0] .ExtendedData.LatLonBox ;
var bounds = new GLatLngBounds(new GLatLng(box.south,box.west),new GLatLng(box.north,box.east));
var center = new GLatLng(box.Placemark [ 0] .Point.coordinates [1],latlng.Placemark [0] .Point.coordinates [0]);
var zoom = oMap.getBoundsZoomLevel(bounds);
map.setCenter(center,zoom );
}
});

我认为这对你的关键部分是

  // box是一个LatLonBox,其大小为resultquery。你也可以自己创建它
var box = latlng.Placemark [0] .ExtendedData.LatLonBox;

//边界是框的边界
var bounds = new GLatLngBounds(new GLatLng(box.south,box.west),new GLatLng(box.north,box.east) );

// center是盒子的中心,你想把它作为你屏幕的中心
var center = new GLatLng(box.Placemark [0] .Point.coordinates [1] ,latlng.Placemark [0] .Point.coordinates [0]);

// zoom是您需要的全部缩放级别
var zoom = oMap.getBoundsZoomLevel(bounds);

//实际动作
map.setCenter(center,zoom);


I am working with google map. According to requirements i need to set different zoom level that is dependent to my search query.If there are multiple location on the map in country then map should focus the country. Other scenario is , if there are different locations marked in a city then map should be focused to city level.

解决方案

var geoCoder = new GClientGeocoder();
geoCoder.setViewport(map.getBounds());
geoCoder.getLocations('searchquery', function(latlng) {
  if( latlng.Placemark.length > 0 ) {
    var box = latlng.Placemark[0].ExtendedData.LatLonBox;
    var bounds = new GLatLngBounds(new GLatLng(box.south, box.west), new GLatLng(box.north, box.east));
    var center = new GLatLng(box.Placemark[0].Point.coordinates[1], latlng.Placemark[0].Point.coordinates[0]);
    var zoom = oMap.getBoundsZoomLevel(bounds);
    map.setCenter(center, zoom);
  }
});

I think the key part of this for you is

//box is a LatLonBox with the size of your resultquery. You can create this yourself as well
var box = latlng.Placemark[0].ExtendedData.LatLonBox;

//bounds are the bounds of the box
var bounds = new GLatLngBounds(new GLatLng(box.south, box.west), new GLatLng(box.north, box.east));

//center is the center of the box, you want this as the center of your screen
var center = new GLatLng(box.Placemark[0].Point.coordinates[1], latlng.Placemark[0].Point.coordinates[0]);

//zoom is the zoomlevel you need for all this
var zoom = oMap.getBoundsZoomLevel(bounds);

//the actual action
map.setCenter(center, zoom);

这篇关于如何动态设置Google地图自定义缩放级别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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