标签:type ati rand ext cas rip 一个 功能 on()
利用百度地图API可以做很多事情,个人感觉最核心也是最基础的就是定位功能了。这里分享一个制作的JS可以实现登录网页后定位:
1 <script type="text/javascript"> 2 var map; 3 var gpsPoint; 4 var baiduPoint; 5 var gpsAddress; 6 var baiduAddress; 7 var x; 8 var y; 9 function getLocation() { 10 //根据IP获取城市 11 var myCity = new BMap.LocalCity(); 12 myCity.get(getCityByIP); 13 14 //获取GPS坐标 15 if (navigator.geolocation) { 16 navigator.geolocation.getCurrentPosition(showMap, handleError, { enableHighAccuracy: true, maximumAge: 1000 }); 17 } else { 18 alert("您的浏览器不支持使用HTML 5来获取地理位置服务"); 19 } 20 } 21 22 function showMap(value) { 23 var longitude = value.coords.longitude; 24 var latitude = value.coords.latitude; 25 map = new BMap.Map("map"); 26 x=latitude; 27 y=longitude; 28 //alert("坐标经度为:" + latitude + ", 纬度为:" + longitude ); 29 gpsPoint = new BMap.Point(longitude, latitude); // 创建点坐标 30 31 32 //根据坐标逆解析地址 33 var geoc = new BMap.Geocoder(); 34 geoc.getLocation(gpsPoint, getCityByCoordinate); 35 36 BMap.Convertor.translate(gpsPoint, 0, translateCallback); 37 map.enableScrollWheelZoom(true); 38 } 39 40 translateCallback = function (point) { 41 baiduPoint = point; 42 map.centerAndZoom(baiduPoint, 18); 43 var geoc = new BMap.Geocoder(); 44 geoc.getLocation(baiduPoint, getCityByBaiduCoordinate); 45 } 46 47 function getCityByCoordinate(rs) { 48 gpsAddress = rs.addressComponents; 49 var address = "GPS标注:" + gpsAddress.province + "," + gpsAddress.city + "," + gpsAddress.district + "," + gpsAddress.street + "," + gpsAddress.streetNumber; 50 var marker = new BMap.Marker(gpsPoint); // 创建标注 51 map.addOverlay(marker); // 将标注添加到地图中 52 var labelgps = new BMap.Label(address, { offset: new BMap.Size(20, -10) }); 53 marker.setLabel(labelgps); //添加GPS标注 54 } 55 56 function getCityByBaiduCoordinate(rs) { 57 baiduAddress = rs.addressComponents; 58 var address = "百度标注:" + baiduAddress.province + "," + baiduAddress.city + "," + baiduAddress.district + "," + baiduAddress.street + "," + baiduAddress.streetNumber; 59 var marker = new BMap.Marker(baiduPoint); // 创建标注 60 map.addOverlay(marker); // 将标注添加到地图中 61 var labelbaidu = new BMap.Label(address, { offset: new BMap.Size(20, -10) }); 62 marker.setLabel(labelbaidu); //添加百度标注 63 } 64 65 //根据IP获取城市 66 function getCityByIP(rs) { 67 var cityName = rs.name; 68 alert("根据IP定位您所在的城市为:" + cityName); 69 } 70 71 function handleError(value) { 72 switch (value.code) { 73 case 1: 74 alert("位置服务被拒绝"); 75 break; 76 case 2: 77 alert("暂时获取不到位置信息"); 78 break; 79 case 3: 80 alert("获取信息超时"); 81 break; 82 case 4: 83 alert("未知错误"); 84 break; 85 } 86 } 87 88 function init() { 89 getLocation(); 90 } 91 92 window.onload = init; 93 94 </script>
完成定位功能后可以添加相关代码编辑地图控件 覆盖物 信息窗口等等各种功能。
附上百度地图连接:http://lbsyun.baidu.com/
标签:type ati rand ext cas rip 一个 功能 on()
原文地址:http://www.cnblogs.com/wewin/p/7787493.html