Javascript 如何让谷歌地图API为一个国家设置正确的缩放级别?
是否有根据地图居中国家的大小自动设置缩放级别的方法 maps.google.com正是我所需要的,所以,例如,如果我搜索俄罗斯,我会得到一个缩放级别,这样俄罗斯就可以在屏幕上显示出来,而当我搜索古巴时,我会得到一个更高的缩放级别,这样古巴就可以显示出来 是否有某种方法可以为地图Api提供国家/地区位置并获得适当的缩放级别 如果没有,我想我必须手动(啊!)为这些信息创建自己的表。或者这些信息在什么地方可以免费获得 对于API v3检查 您可以使用谷歌地图获取国家的边界框,如以下示例所示:Javascript 如何让谷歌地图API为一个国家设置正确的缩放级别?,javascript,google-maps,Javascript,Google Maps,是否有根据地图居中国家的大小自动设置缩放级别的方法 maps.google.com正是我所需要的,所以,例如,如果我搜索俄罗斯,我会得到一个缩放级别,这样俄罗斯就可以在屏幕上显示出来,而当我搜索古巴时,我会得到一个更高的缩放级别,这样古巴就可以显示出来 是否有某种方法可以为地图Api提供国家/地区位置并获得适当的缩放级别 如果没有,我想我必须手动(啊!)为这些信息创建自己的表。或者这些信息在什么地方可以免费获得 对于API v3检查 您可以使用谷歌地图获取国家的边界框,如以下示例所示: // A
// API version 2
var geocoder = new GClientGeocoder();
geocoder.getLocations("Russia", function (locations) {
var north = locations.Placemark[0].ExtendedData.LatLonBox.north;
var south = locations.Placemark[0].ExtendedData.LatLonBox.south;
var east = locations.Placemark[0].ExtendedData.LatLonBox.east;
var west = locations.Placemark[0].ExtendedData.LatLonBox.west;
var bounds = new GLatLngBounds(new GLatLng(south, west),
new GLatLng(north, east));
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
});
// API version 3
// ... set north, south, east and west ...
var bounds = new google.maps.LatLngBounds(new google.maps.LatLng(south, west),
new google.maps.LatLng(north, east));
map.fitBounds(bounds);
下面的屏幕截图显示了搜索俄罗斯和古巴时上述技术的结果:
如果您不使用谷歌地图api,只使用其地理编码,您可以使用以下公式:
var url = 'http://maps.google.com/maps/geo?q=YOUR_QUERY&output=json&oe=utf8&sensor=false&key=YOUR_KEYback=geoCodeDone';
jQuery.getScript(url);
function geoCodeDone(data)
{
if (data.Status.code == 200)
{
lng = data.Placemark[0].Point.coordinates[0];
lat = data.Placemark[0].Point.coordinates[1];
var east = data.Placemark[0].ExtendedData.LatLonBox.east;
var west = data.Placemark[0].ExtendedData.LatLonBox.west;
var zoom = 11 - Math.round(Math.log(Math.abs(west-east))/Math.log(2));
}
}
对于V3,此代码适用于我:
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
map.fitBounds(results[0].geometry.viewport);
}
});
如果您不想使用谷歌的geocoder客户端,由于使用限制,您可以使用自己的列表。你可以从这里得到一个 下面是一个使用jQuery的getJSON函数和google maps API v3的代码示例:
function initialize() {
// read the list of countries
$.getJSON('countries.json', function (countries) {
// will use the country with index 40 (Cyprus)
var index_country = 40;
var myOptions = {
center: new google.maps.LatLng(
countries[index_country].center_lat,
countries[index_country].center_lng),
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// set the bounds of the map
var bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(countries[index_country].sw_lat, countries[index_country].sw_lng),
new google.maps.LatLng(countries[index_country].ne_lat, countries[index_country].ne_lng) );
map.fitBounds(bounds);
});
}
太好了,太好了,谢谢。我在GWT中工作,它还不能访问ExtendedData,所以我必须创建一些JSNI方法来实现这一点。有一件事让我有了一段时间:确保调用getBoundsZoomLevel时地图是可见的,否则它会返回零的缩放级别。@krishnaz:干得好。感谢分享
getBoundsZoomLevel()
技巧。我正在查看api,但目前还没有找到一种使用V3实现这一点的方法。我也想为一个城市做这件事。我想这就是我一直在寻找的。我有更好的运气和ViewPort完美地工作。感谢您添加V3的更新版本。非常感谢,太好了。在任何地方工作。城市、省份、国家等是setCenter
和fitBounds
的参数,对吗?我似乎无法让它工作,出现了一个JS错误,说“太多的递归”-map.setCenter(lat,lon)代码>有效。地址
可能只是一个国家的名称吗?人们真的认为这很好吗?(不是答案)但我指的是谷歌的实际实施?一些国家几乎不占用任何屏幕。难道没有办法设置它并说“占用我容器的80%”这肯定是以某种方式实现的吗?