<!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Hello, World</title> <style type="text/css"> html{height:100%} body{height:100%;margin:0px;padding:0px} #container{height:100%} </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.4"></script> </head> <body> <div id="allmap"></div> <div id="input" style="height: 100px;"></div> <script type="text/javascript"> // 百度地图API功能 var input = document.getElementById("input"); getLocationText(function(text){ input.innerHTML = text; }) function getLocationText(fn){ var map = new BMap.Map("allmap"); var point = new BMap.Point(116.331398,39.897445); map.centerAndZoom(point,20); var geolocation = new BMap.Geolocation(); var geoc = new BMap.Geocoder(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ var mk = new BMap.Marker(r.point); map.addOverlay(mk); map.panTo(r.point); alert('您的位置:'+r.point.lng+','+r.point.lat); point = new BMap.Point(r.point.lng,r.point.lat); geoc.getLocation(point, function(rs){ var addComp = rs.addressComponents; var location = addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber; fn(location); }); } else { alert('failed'+this.getStatus()); } },{enableHighAccuracy: true}); } </script> </body> </html>