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

Sha*_*sri 13 javascript google-maps google-api

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

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

map.setCenter(new GLatLng( 43.907787,-79.359741), 9);
Run Code Online (Sandbox Code Playgroud)

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

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

Bob*_*man 16

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);
  }
});
Run Code Online (Sandbox Code Playgroud)


ant*_*tun 10

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

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);
    }
});
Run Code Online (Sandbox Code Playgroud)


Jac*_*son 5

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

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