<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <style type="text/css"> body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";font-size:14px;} #allmap {width:100%;height:500px;} </style> <script src="jquery-1.10.2.min.js" type="text/javascript"></script> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=xxxxxx_____key"></script> <title>逆地址解析</title> </head> <body> <div id="allmap"></div> <p id="pmap"></p> </body> </html> <script type="text/javascript"> // 百度地图API功能 var map = new BMap.Map("allmap"); //默认一个中心坐标 map.centerAndZoom(new BMap.Point(107.164226, 31.859637), 5); map.addControl(new BMap.NavigationControl()); map.addControl(new BMap.NavigationControl()); map.addControl(new BMap.ScaleControl()); map.addControl(new BMap.OverviewMapControl()); map.addControl(new BMap.MapTypeControl()); map.setCurrentCity("海南"); // 仅当设置城市信息时,MapTypeControl的切换功能才能可用 //定位拾坐标 var geoc = new BMap.Geocoder(); //点击坐标事件 map.addEventListener("click", function (e) { var pt = e.point; //移除原有的标注 map.clearOverlays(); var pointMarker = new BMap.Point(e.point.lng, e.point.lat); // 创建标注的坐标 addMarker(pointMarker); geoc.getLocation(pt, function (rs) { var addComp = rs.addressComponents; $("#pmap").html(addComp.province + ", " + addComp.city + ", " + addComp.district); //+ ", " + addComp.street + ", " + addComp.streetNumber }); }); //添加标注 function addMarker(point) { var myIcon = new BMap.Icon("mapMarker.png", new BMap.Size(30, 30), { offset: new BMap.Size(10, 25), // 指定定位位置 imageOffset: 0 // 设置图片偏移 }); // var myIcon = new BMap.Icon("http://api.map.baidu.com/img/markers.png", new BMap.Size(23, 25), { // offset: new BMap.Size(10, 25), // 指定定位位置 // imageOffset: new BMap.Size(0, 0 - 10 * 25) // 设置图片偏移 // }); var marker = new BMap.Marker(point, { icon: myIcon }); map.addOverlay(marker); } </script>