富贵资源网 Design By www.hznty.com
利用百度地图API可以做很多事情,个人感觉最核心也是最基础的就是定位功能了。这里分享一个制作的JS可以实现登录网页后定位:
<script type="text/javascript"> var map; var gpsPoint; var baiduPoint; var gpsAddress; var baiduAddress; var x; var y; function getLocation() { //根据IP获取城市 var myCity = new BMap.LocalCity(); myCity.get(getCityByIP); //获取GPS坐标 if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showMap, handleError, { enableHighAccuracy: true, maximumAge: 1000 }); } else { alert("您的浏览器不支持使用HTML 5来获取地理位置服务"); } } function showMap(value) { var longitude = value.coords.longitude; var latitude = value.coords.latitude; map = new BMap.Map("map"); x=latitude; y=longitude; //alert("坐标经度为:" + latitude + ", 纬度为:" + longitude ); gpsPoint = new BMap.Point(longitude, latitude); // 创建点坐标 //根据坐标逆解析地址 var geoc = new BMap.Geocoder(); geoc.getLocation(gpsPoint, getCityByCoordinate); BMap.Convertor.translate(gpsPoint, 0, translateCallback); map.enableScrollWheelZoom(true); } translateCallback = function (point) { baiduPoint = point; map.centerAndZoom(baiduPoint, 18); var geoc = new BMap.Geocoder(); geoc.getLocation(baiduPoint, getCityByBaiduCoordinate); } function getCityByCoordinate(rs) { gpsAddress = rs.addressComponents; var address = "GPS标注:" + gpsAddress.province + "," + gpsAddress.city + "," + gpsAddress.district + "," + gpsAddress.street + "," + gpsAddress.streetNumber; var marker = new BMap.Marker(gpsPoint); // 创建标注 map.addOverlay(marker); // 将标注添加到地图中 var labelgps = new BMap.Label(address, { offset: new BMap.Size(20, -10) }); marker.setLabel(labelgps); //添加GPS标注 } function getCityByBaiduCoordinate(rs) { baiduAddress = rs.addressComponents; var address = "百度标注:" + baiduAddress.province + "," + baiduAddress.city + "," + baiduAddress.district + "," + baiduAddress.street + "," + baiduAddress.streetNumber; var marker = new BMap.Marker(baiduPoint); // 创建标注 map.addOverlay(marker); // 将标注添加到地图中 var labelbaidu = new BMap.Label(address, { offset: new BMap.Size(20, -10) }); marker.setLabel(labelbaidu); //添加百度标注 } //根据IP获取城市 function getCityByIP(rs) { var cityName = rs.name; alert("根据IP定位您所在的城市为:" + cityName); } function handleError(value) { switch (value.code) { case 1: alert("位置服务被拒绝"); break; case 2: alert("暂时获取不到位置信息"); break; case 3: alert("获取信息超时"); break; case 4: alert("未知错误"); break; } } function init() { getLocation(); } window.onload = init; </script>
完成定位功能后可以添加相关代码编辑地图控件 覆盖物 信息窗口等等各种功能。
附上百度地图连接:http://lbsyun.baidu.com/
以上这篇利用百度地图API获取当前位置信息的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com
暂无评论...