87,910
社区成员
发帖
与我相关
我的任务
分享
<script src="http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en" type="text/javascript"></script>
下面示例 解析成英文
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://maps.google.com/maps/api/js?sensor=false&libraries=places&language=en" type="text/javascript"></script>
<title>谷歌地图地理解析和反解析geocode.geocoder详解</title>
<meta name="author" content="yanue" />
<meta name="copyright" content="powered by yanue" />
<link rel="site" href="http://map.yanue.net/" />
<script type="text/javascript">
window.onload = function() {
//初始化地图
var map = new google.maps.Map(document.getElementById("map_canvas"),{
center : new google.maps.LatLng(26.57, 106.72),
zoom : 8,
mapTypeId : google.maps.MapTypeId.ROADMAP
});
//实例化Geocoder服务
var geocoder = new google.maps.Geocoder();
//1.地理解析过程
//请求数据GeocoderRequest为address,值为'贵阳'
geocoder.geocode({address:'贵阳'},function geoResults(results, status){
//这里是回掉函数(即结果处理函数)
//状态为Ok说明有结果
if (status == google.maps.GeocoderStatus.OK) {
//一般情况下会有多个结果
//第一个结果为最佳匹配的结果(匹配地名最全的结果),这里只去第一个,其他的可以根据需要自己循环出来
//格式化过后的地址
alert('地理解析结果:'+results[0].formatted_address);
//geometry是一个包含bounds(界限),location(纬度/经度坐标),location_type和viewport(视图范围)
//获取解析后的经纬度
alert('地理解析结果:'+results[0].geometry.location);
}else{
alert(":error " + status);
}
});
//2.地理反解析过程
//请求数据GeocoderRequest为location,值类型为LatLng因此我们要实例化经纬度
geocoder.geocode({location:new google.maps.LatLng(26.57, 106.72)},function geoResults(results, status){
//这里处理结果和上面一模一样
if (status == google.maps.GeocoderStatus.OK) {
alert('地理反解析结果:'+results[0].formatted_address);
}else{
alert(":error " + status);
}
});
}
</script>
</head>
<body>
<div id="map_canvas" style='width: 300px; height: 200px;'></div>
</body>
</html>