2010-09-21 2 views
2

Я получаю информацию о том или ином месте, и я хочу получить название города или провинции, где лежит это место, как я могу это сделать?Как получить название провинции с предоставлением lat lng?

например, с этим lat & lng: Point (39.904667 116.408198) in beijing Китай, я хочу получить Пекин.

Есть ли в карте google какие-либо апи могут это сделать?

ответ

7

То, что вы описали, называется Reverse Geocoding. Google предоставляет два API для этого: A JavaScript API и Web Service API.

Пример использования веб-службы:

http://maps.google.com/maps/api/geocode/json?latlng=39.904667,116.408198&sensor=false

Response (с помощью JSON, но может также возвращать XML):

{ 
    "status": "OK", 
    "results": [ { 
    "types": [ "street_address" ], 
    "formatted_address": "2号 Zhengyi Rd, Dongcheng, Beijing, China", 
    "address_components": [ { 
     "long_name": "2号", 
     "short_name": "2号", 
     "types": [ "street_number" ] 
    }, { 
     "long_name": "Zhengyi Rd", 
     "short_name": "Zhengyi Rd", 
     "types": [ "route" ] 
    }, { 
     "long_name": "Dongcheng", 
     "short_name": "Dongcheng", 
     "types": [ "sublocality", "political" ] 
    }, { 
     "long_name": "Beijing", 
     "short_name": "Beijing", 
     "types": [ "locality", "political" ] 
    }, { 
     "long_name": "Beijing", 
     "short_name": "Beijing", 
     "types": [ "administrative_area_level_1", "political" ] 
    }, { 
     "long_name": "China", 
     "short_name": "CN", 
     "types": [ "country", "political" ] 
    } ], 
    "geometry": { 
     "location": { 
     "lat": 39.9042110, 
     "lng": 116.4074130 
     }, 
     "location_type": "ROOFTOP", 
     "viewport": { 
     "southwest": { 
      "lat": 39.9010634, 
      "lng": 116.4042654 
     }, 
     "northeast": { 
      "lat": 39.9073586, 
      "lng": 116.4105606 
     } 
     } 
    } 
    }, { 
    "types": [ "sublocality", "political" ], 
    "formatted_address": "Dongcheng, Beijing, China", 
    "address_components": [ { 
     "long_name": "Dongcheng", 
     "short_name": "Dongcheng", 
     "types": [ "sublocality", "political" ] 
    }, { 
     "long_name": "Beijing", 
     "short_name": "Beijing", 
     "types": [ "locality", "political" ] 
    }, { 
     "long_name": "Beijing", 
     "short_name": "Beijing", 
     "types": [ "administrative_area_level_1", "political" ] 
    }, { 
     "long_name": "China", 
     "short_name": "CN", 
     "types": [ "country", "political" ] 
    } ], 
    "geometry": { 
     "location": { 
     "lat": 39.9284190, 
     "lng": 116.4161900 
     }, 
     "location_type": "APPROXIMATE", 
     "viewport": { 
     "southwest": { 
      "lat": 39.9000758, 
      "lng": 116.3868896 
     }, 
     "northeast": { 
      "lat": 39.9754256, 
      "lng": 116.4471843 
     } 
     }, 
     "bounds": { 
     "southwest": { 
      "lat": 39.9000758, 
      "lng": 116.3868896 
     }, 
     "northeast": { 
      "lat": 39.9754256, 
      "lng": 116.4471843 
     } 
     } 
    } 
    }, { 
    "types": [ "administrative_area_level_1", "political" ], 
    "formatted_address": "Beijing, China", 
    "address_components": [ { 
     "long_name": "Beijing", 
     "short_name": "Beijing", 
     "types": [ "administrative_area_level_1", "political" ] 
    }, { 
     "long_name": "China", 
     "short_name": "CN", 
     "types": [ "country", "political" ] 
    } ], 
    "geometry": { 
     "location": { 
     "lat": 39.9046670, 
     "lng": 116.4081980 
     }, 
     "location_type": "APPROXIMATE", 
     "viewport": { 
     "southwest": { 
      "lat": 39.4432527, 
      "lng": 115.4203742 
     }, 
     "northeast": { 
      "lat": 41.0608685, 
      "lng": 117.5055691 
     } 
     }, 
     "bounds": { 
     "southwest": { 
      "lat": 39.4432527, 
      "lng": 115.4203742 
     }, 
     "northeast": { 
      "lat": 41.0608685, 
      "lng": 117.5055691 
     } 
     } 
    } 
    }, { 
    "types": [ "locality", "political" ], 
    "formatted_address": "Beijing, China", 
    "address_components": [ { 
     "long_name": "Beijing", 
     "short_name": "Beijing", 
     "types": [ "locality", "political" ] 
    }, { 
     "long_name": "Beijing", 
     "short_name": "Beijing", 
     "types": [ "administrative_area_level_1", "political" ] 
    }, { 
     "long_name": "China", 
     "short_name": "CN", 
     "types": [ "country", "political" ] 
    } ], 
    "geometry": { 
     "location": { 
     "lat": 39.9046670, 
     "lng": 116.4081980 
     }, 
     "location_type": "APPROXIMATE", 
     "viewport": { 
     "southwest": { 
      "lat": 39.6612714, 
      "lng": 116.0119343 
     }, 
     "northeast": { 
      "lat": 40.2164962, 
      "lng": 116.7829835 
     } 
     }, 
     "bounds": { 
     "southwest": { 
      "lat": 39.6612714, 
      "lng": 116.0119343 
     }, 
     "northeast": { 
      "lat": 40.2164962, 
      "lng": 116.7829835 
     } 
     } 
    } 
    }, { 
    "types": [ "country", "political" ], 
    "formatted_address": "China", 
    "address_components": [ { 
     "long_name": "China", 
     "short_name": "CN", 
     "types": [ "country", "political" ] 
    } ], 
    "geometry": { 
     "location": { 
     "lat": 35.8616600, 
     "lng": 104.1953970 
     }, 
     "location_type": "APPROXIMATE", 
     "viewport": { 
     "southwest": { 
      "lat": 17.9996000, 
      "lng": 73.4994137 
     }, 
     "northeast": { 
      "lat": 53.5609740, 
      "lng": 134.7728100 
     } 
     }, 
     "bounds": { 
     "southwest": { 
      "lat": 17.9996000, 
      "lng": 73.4994137 
     }, 
     "northeast": { 
      "lat": 53.5609740, 
      "lng": 134.7728100 
     } 
     } 
    } 
    } ] 
} 

И это пример использования JavaScript API:

<!DOCTYPE html> 
<html> 
<head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/> 
    <title>Google Maps Reverse Geocoding</title> 
    <script src="http://maps.google.com/maps/api/js?sensor=false" 
      type="text/javascript"></script> 
</head> 
<body> 
    <div id="map" style="width: 400px; height: 300px;"></div> 

    <script type="text/javascript"> 

    var map = new google.maps.Map(document.getElementById('map'), { 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     zoom: 12 
    }); 

    var infowindow = new google.maps.InfoWindow(); 

    var geocoder = new google.maps.Geocoder(); 

    geocoder.geocode({ 
     'latLng': new google.maps.LatLng(39.904667, 116.408198) 
    }, 
    function(results, status) { 
     if(status == google.maps.GeocoderStatus.OK) { 
     var marker = new google.maps.Marker({ 
      position: results[0].geometry.location, 
      map: map 
     }); 
     map.setCenter(results[0].geometry.location); 
     infowindow.setContent(results[1].formatted_address); 
     infowindow.open(map, marker); 
     } 
    }); 

    </script> 
</body> 
</html> 

Скриншот:

Reverse Geocoding

+0

Спасибо, именно то, что я ищу,^_ ^ – DiveInto

Смежные вопросы