通过GMAP得到坐标对应地址

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div id='mapZone' style="width:500px;height:300px;"></div>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
    console.log(google.maps);
    
var ddd = new google.maps.Geocoder();
    
//position.coords.latitude position.coords.longitude
    var latlng = new google.maps.LatLng(25.0392,121.525002);
    ddd.geocode({
        
'address''布吉'//根据地址得到坐标等信息
    },function(data){
        console.log(data);
    })
    ddd.geocode({
        
'latLng': latlng//根据坐标得到地址等信息
    },function(data){
        console.log(data);
    })
function getPositionSuccess(position){
    
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    
var myOptions = {//绘制地图
        zoom: 15,
        center: latlng,
        mapTypeControl: 
false,
        navigationControlOptions: {
            style: google.maps.NavigationControlStyle.SMALL
        },
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    
var map = new google.maps.Map(document.getElementById("mapZone"), myOptions);
    
var marker = new google.maps.Marker({
        position: latlng,
        map: map
    });
    
var infowindow = new google.maps.InfoWindow();//弹出窗口
    infowindow.setContent("You are here!");
    infowindow.setPosition(latlng);
    infowindow.open(map);
}
function getPositionError(error){
    console.log(error);
    
switch (error.code) {
        
case error.TIMEOUT:
            alert(
" 连接超时,请重试 ");
            
break;
        
case error.PERMISSION_DENIED:
            alert(
" 您拒绝了使用位置共享服务,查询已取消 ");
            
break;
        
case error.POSITION_UNAVAILABLE:
            alert(
" 亲爱的火星网友,非常抱歉,我们暂时无法为您所在的星球提供位置服务 ");
            
break;
    }
}
if (navigator.geolocation) {//得到浏览器坐标
    navigator.geolocation.getCurrentPosition(getPositionSuccess, getPositionError);
}
else {
    alert(
"你的浏览器不支持geolocation哦~");
}

</script>
</body>
</html>
posted @ 2012-07-20 11:57  liushan  阅读(637)  评论(0编辑  收藏  举报