문제

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 ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top