【发布时间】:2010-12-10 11:34:03
【问题描述】:
有人能告诉我如何在 Google Maps API v3 上以国家名称为中心吗?我知道如何在 v2 中执行此操作,但需要在 v3 中执行。
【问题讨论】:
有人能告诉我如何在 Google Maps API v3 上以国家名称为中心吗?我知道如何在 v2 中执行此操作,但需要在 v3 中执行。
【问题讨论】:
您可以使用地理编码来查找国家/地区的纬度/经度。看看这个来自 Google 的sample。
基本上你需要做这样的事情:
var country = "Germany";
var geocoder;
geocoder.geocode( {'address' : country}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
}
});
在您的初始化函数中,您需要设置地理编码器对象的值,如下所示:
geocoder = new google.maps.Geocoder();
您需要确定合适的缩放级别,然后在地图居中后进行设置。
【讨论】:
这段代码对我有用:
let geocoder = new google.maps.Geocoder();
let location = "England";
geocoder.geocode({ 'address': location }, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
} else {
alert("Could not find location: " + location);
}
});
【讨论】:
我知道如何做到这一点的唯一方法是列出国家/地区及其各自的经纬度和经度,知道这些信息后,您可以使用以下代码。
var myLatlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
出于好奇,您将如何在 v2 中做到这一点?
【讨论】: