I get the lat&am开发者_开发百科p;lng info of a certain place ,and I want to get the city or province name where this place lies,how can I do that?
for example ,with this lat&lng: Point(39.904667 116.408198) in beijing China,I want to get beijing.
Does google map has any api can do that?
What you describe is called Reverse Geocoding. Google provides two APIs for that: A JavaScript API, and a Web Service API.
Example using the web service:
http://maps.google.com/maps/api/geocode/json?latlng=39.904667,116.408198&sensor=false
Response (using JSON, but can also return 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
}
}
}
} ]
}
And this is an example using the 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>
Screenshot:
Not sure about Google Maps, but geonames (http://www.geonames.org/) might have what you need, especially this one: http://www.geonames.org/export/web-services.html#countrysubdiv
精彩评论