提交 06fc9c30 authored 作者: 王绍森's avatar 王绍森

定位组件修改

上级 27b036ca
const isHttps = window.location.protocol.indexOf('https') > -1;
const apiVersion = isHttps ? '3.0' : '2.0';
const mapContaienrId = "baiduMap";
const apiSrc = `https://api.map.baidu.com/api?v=${apiVersion}&ak=WPau2m9l3Rty2HR7Kfc9879nzP2Fj0UU&callback=baiduMapInitialize`;
const hasMapContainer = !!document.getElementById(mapContaienrId);
function loadScript(callBack) {
window.baiduMapInitialize = function () {
getLocationByMap(callBack);
}
if (!hasMapContainer) {
document.write(`
<div id="${mapContaienrId}"></div>
`);
}
document.write(`<script type="text/javascript" src="${apiSrc}"></script>`);
}
function getLocationByMap(callback) {
const BMap = window.BMap;
const map = new BMap.Map(mapContaienrId);
const point = new BMap.Point(107.303748,34.346725);
map.centerAndZoom(point, 12);
const geolocation = new BMap.Geolocation();
geolocation.enableSDKLocation();
geolocation.getCurrentPosition(function(r){
let msg = '';
switch (this.getStatus()) {
case window.BMAP_STATUS_SUCCESS: {
getAddress(r.point, callback);
let mk = new BMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
break;
}
case window.BMAP_STATUS_CITY_LIST: {
msg = '城市列表';
break;
}
case window.BMAP_STATUS_UNKNOWN_LOCATION: {
msg = '位置结果未知';
break;
}
case window.BMAP_STATUS_UNKNOWN_ROUTE: {
msg = '导航结果未知';
break;
}
case window.BMAP_STATUS_INVALID_KEY: {
msg = '非法密钥';
break;
}
case window.BMAP_STATUS_INVALID_REQUEST: {
msg = '非法请求';
break;
}
case window.BMAP_STATUS_PERMISSION_DENIED: {
msg = '没有权限';
break;
}
case window.BMAP_STATUS_SERVICE_UNAVAILABLE: {
msg = '服务不可用';
break;
}
case window.BMAP_STATUS_TIMEOUT: {
msg = '超时';
break;
}
default: {
msg = '未知错误';
}
}
console.log(msg);
},{
enableHighAccuracy: true,
});
}
/**
* 使用百度地图api 获取当前用户的位置
* 返回 {
* @function 使用百度地图api,获取当前用户的位置,
* 可以不用在ejs写api脚本,直接传回调函数,调用getLocation函数,
* @param {Function} callBack 获取位置成功后的回调函数
* @example
* getLocation(function(addressInfo) {
* console.log('位置信息', addressInfo)
* });
* addressInfo = {
* address : '地理位置中文',
* lng: '纬度',
* lat: '经度',
* }
* */
*/
export default function getLocation(callBack = () => {}){
if(typeof BMap == 'undefined'){
console.log('请先加载百度api js 文件: http://api.map.baidu.com/api?v=2.0&ak=qWnwdKnruRGnojAtsZ7Cfb2g');
return false;
}
const map = new BMap.Map('baiduMap');
const point = new BMap.Point(107.303748,34.346725);
map.centerAndZoom(point, 12);
const geolocation = new BMap.Geolocation();
geolocation.enableSDKLocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() === 0) {
let address = getAddress(r.point, callBack);
let mk = new BMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
return address;
}else{
console.log('发生错误!');
return false;
}
},{enableHighAccuracy: true});
if(typeof BMap === 'undefined') {
loadScript(callBack);
return false;
}
getLocationByMap(callBack);
}
export function getAddress(point, callBack=()=>{}){
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论