繁体   English   中英

如何按名称将Google地图置于国家/地区中心

[英]How to center Google Map on a country by name

我正在使用Google Maps API(v2),并希望将地图上传到一个国家/地区(例如英格兰)。

目前我使用以下地图居中:

map.setCenter(new GLatLng( 43.907787,-79.359741), 9);

但这显然需要经度和纬度。

通过插入国家名称的任何方式来做到这一点?

var country = "United States"
var map = new GMap2($("#map")[0]);
map.setUIToDefault();

var geocoder = new GClientGeocoder();
geocoder.getLatLng(country, function (point) {
  if (!point) {
    // Handle error
  } else {
    map.setCenter(point, 8, G_PHYSICAL_MAP);
  }
});

您需要首先对地址进行地理编码:

var geocoder = new google.maps.Geocoder();
var 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);
    }
});

将位置名称或地址转换为像这样的纬度/经度称为地理编码。 Google Maps API现在包含此功能:请参阅http://code.google.com/apis/maps/documentation/services.html#Geocoding

它们包括一个示例应用程序,您可以在其中键入地址,只需键入国家/地区名称即可。 我不知道他们是否要前往该国的确切中心。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM