JavaScript API提供在Web端获取当前位置信息的方法,融合了浏览器定位、IP定位、安卓定位SDK辅助定位等多种手段,提供了获取当前准确位置、获取当前城市信息等功能。
浏览器定位精度依赖浏览器自身特性,IP定位的精度值为城市级别。
对于安卓WebView页面的开发者,可以结合定位SDK进行辅助定位,使用方法参见定位SDK相关章节。
注意:由于Chrome、iOS10等已不再支持非安全域的浏览器定位请求,为保证定位成功率和精度,请尽快升级您的站点到HTTPS。
var map = new BMap.Map("allmap");var point = new BMap.Point(116.331398,39.897445);map.centerAndZoom(point,12);var geolocation = new BMap.Geolocation();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);}else {alert('failed'+this.getStatus());}});
var map = new BMap.Map("allmap");var point = new BMap.Point(116.331398,39.897445);map.centerAndZoom(point,12);function myFun(result){var cityName = result.name;map.setCenter(cityName);alert("当前定位城市:"+cityName);}var myCity = new BMap.LocalCity();myCity.get(myFun);
var map = new BMap.Map("allmap");var point = new BMap.Point(116.331398,39.897445);map.centerAndZoom(point,12);var geolocation = new BMap.Geolocation();// 开启SDK辅助定位geolocation.enableSDKLocation();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);}else {alert('failed'+this.getStatus());}});
上一篇
下一篇