提交 8672b2fe authored 作者: 钟是志's avatar 钟是志

百度地图api的使用

上级 15344365
/**
* 使用百度地图api 获取当前用户的位置
* */
export default function getLocation(){
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('map');
map.centerAndZoom(new BMap.Point(121.491, 31.223), 11);
const geolocation = new BMap.Geolocation();
geolocation.getCurrentPosition(function(r){
if(this.getStatus() === 0) {
getAddress(r.point);
let mk = new BMap.Marker(r.point);
map.addOverlay(mk);
map.panTo(r.point);
}else{
alert('发生错误!');
}
},{enableHighAccuracy: true});
// 通过坐标 拿取地址名字
function getAddress(point) {
const lng = point.lng; // 经度
const lat = point.lat; // 纬度
let gc = new BMap.Geocoder();
gc.getLocation(point,function(res){
let formValues = {};
let status = 0;
// 判断范围
const zoneCaculate = (origin,r) => {
let currR = Math.sqrt(Math.pow(origin.lng - lng,2) + Math.pow(origin.lat - lat,2))
if(currR <= r) {
return 1;
}else{
return 0
}
};
// 判断时间
const caculateTime = (startTime, endTime) => {
let currTime = new Date().getTime();
if(startTime < currTime && currTime < endTime) {
return 1;
}else{
return 0;
}
};
// 处理状态
const isZone = zoneCaculate({lng: 104, lat: 30},1000);
const isTime = caculateTime(new Date().getTime() - 10000,new Date().getTime() + 10000);
if(isZone === 0) status = 0;
if(isZone === 1 && isTime === 1) status = 2;
if(isZone === 1 && isTime === 0) status = 1;
// 更改状态
formValues.address = res.address;
formValues.lng = lng;
formValues.lat = lat;
return formValues;
});
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论