141 lines
5.1 KiB
JavaScript
141 lines
5.1 KiB
JavaScript
// 打开手机已有地图
|
|
const openMap = (options) => {
|
|
// #ifdef MP-WEIXIN
|
|
uni.openLocation({
|
|
latitude: parseFloat(options.latitude),
|
|
longitude: parseFloat(options.longitude),
|
|
name: options.name,
|
|
address: options.address,
|
|
success: function (res) {
|
|
console.log('打开系统位置地图成功');
|
|
},
|
|
fail: function (error) {
|
|
console.log(error);
|
|
}
|
|
});
|
|
// #endif
|
|
// #ifdef APP-PLUS
|
|
let platform = uni.getSystemInfoSync().platform;
|
|
let mapApps = [
|
|
{ title: '高德地图', name: 'amap', androidName: 'com.autonavi.minimap', iosName: 'iosamap://' },
|
|
{ title: '百度地图', name: 'baidumap', androidName: 'com.baidu.BaiduMap', iosName: 'baidumap://' },
|
|
{ title: '腾讯地图', name: 'qqmap', androidName: 'com.tencent.map', iosName: 'qqmap://' },
|
|
// { title: '苹果地图', name: 'applemap', androidName: 'com.apple.Maps', iosName: 'maps://' }
|
|
];
|
|
// ,
|
|
// { title: '百度地图', name: 'baidumap', androidName: 'com.baidu.BaiduMap', iosName: 'baidumap://' },
|
|
// { title: '腾讯地图', name: 'qqmap', androidName: 'com.tencent.map', iosName: 'qqmap://' }
|
|
|
|
let buttons = [];
|
|
mapApps.forEach(item => {
|
|
if (platform === 'android' && plus.runtime.isApplicationExist({ pname: item.androidName })) {
|
|
buttons.push(item);
|
|
} else if (platform === 'ios' && plus.runtime.isApplicationExist({ action: item.iosName })) {
|
|
buttons.push(item);
|
|
}
|
|
});
|
|
|
|
if (buttons.length) {
|
|
plus.nativeUI.actionSheet({
|
|
title: "选择地图应用",
|
|
cancel: "取消",
|
|
buttons: buttons
|
|
}, function (e) {
|
|
let selectedMap = buttons[e.index - 1];
|
|
openURL(selectedMap, platform, options);
|
|
});
|
|
} else {
|
|
uni.showToast({
|
|
title: '请安装地图软件',
|
|
icon: 'none'
|
|
});
|
|
}
|
|
// #endif
|
|
}
|
|
const openURL = (map, platform, options) => {
|
|
console.log(2222,map)
|
|
const androidMapConfigs = {
|
|
amap: {
|
|
scheme: `amapuri://route/plan/?dlat=${options.latitude}&dlon=${options.longitude}&dname=${options.name}&dev=0&t=0`,
|
|
name: "高德地图",
|
|
},
|
|
baidumap: {
|
|
scheme: `baidumap://map/direction?destination=latlng:${options.latitude},${options.longitude}|name:${options.name}&mode=driving&coord_type=gcj02`,
|
|
name: "百度地图",
|
|
},
|
|
qqmap: {
|
|
scheme: `qqmap://map/routeplan?type=drive&from=我的位置&fromcoord=0,0&to=${options.name}&tocoord=${options.latitude},${options.longitude}&policy=0`,
|
|
name: "腾讯地图",
|
|
},
|
|
};
|
|
const iosMapConfigs= {
|
|
amap: {
|
|
scheme: `iosamap://navi?sourceApplication=plus&poiname=${encodeURIComponent(options.name)}&lat=${parseFloat(options.latitude)}&lon=${parseFloat(options.longitude)}&dev=0&style=0`,
|
|
name: "高德地图",
|
|
},
|
|
baidumap: {
|
|
scheme: `baidumap://map/direction?destination=${parseFloat(options.latitude)},${parseFloat(options.longitude)}&mode=driving&coord_type=gcj02`,
|
|
name: "百度地图",
|
|
},
|
|
qqmap: {
|
|
scheme: `qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&to=${encodeURIComponent(options.name)}&tocoord=${options.latitude},${options.longitude}`,
|
|
name: "腾讯地图",
|
|
},
|
|
// applemap: {
|
|
// scheme: `maps://?q=${encodeURIComponent(options.name)}&sll=${options.latitude},${options.longitude}`,
|
|
// name: "苹果地图",
|
|
// },
|
|
};
|
|
let url = ''
|
|
if(platform === 'android'){
|
|
url = androidMapConfigs[map.name].scheme
|
|
}else{
|
|
url = iosMapConfigs[map.name].scheme
|
|
}
|
|
// let url = platform === 'android' ?
|
|
// `amapuri://route/plan/?dlat=${options.latitude}&dlon=${options.longitude}&dname=${options.name}&dev=0&t=0` :
|
|
// `qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&to=${encodeURIComponent(options.name)}&tocoord=${options.latitude},${options.longitude}`;
|
|
console.log(url)
|
|
plus.runtime.openURL(url, function(res) {
|
|
console.log(res)
|
|
uni.showModal({ content: res.message });
|
|
}, map.androidName ? map.androidName : '');
|
|
}
|
|
// 计算两地距离 distance
|
|
const calculateDistance = (lat1, lon1, lat2, lon2) => {
|
|
const R = 6371; // 地球半径,单位:千米
|
|
const toRadians = (degree) => degree * (Math.PI / 180);
|
|
|
|
const dLat = toRadians(lat2 - lat1);
|
|
const dLon = toRadians(lon2 - lon1);
|
|
|
|
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) *
|
|
Math.sin(dLon / 2) * Math.sin(dLon / 2);
|
|
|
|
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
const distance = R * c;
|
|
|
|
return distance.toFixed(2); // 返回距离,单位:千米,保留两位小数
|
|
};
|
|
// 导出请求方法
|
|
export default {
|
|
// 打开手机地图
|
|
goMap(options = {}) {
|
|
return openMap({
|
|
...options
|
|
})
|
|
},
|
|
calcDistance(lat1, lon1, lat2, lon2) {
|
|
return calculateDistance(lat1, lon1, lat2, lon2)
|
|
},
|
|
roundUpToTwoDecimals(price, ratio) {
|
|
return (Math.floor(price * ratio * 100 + 0.9) / 100).toFixed(2);
|
|
},
|
|
// 获取日期字符串
|
|
getDateStr(days) {
|
|
const date = new Date();
|
|
date.setDate(date.getDate() + days);
|
|
return `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
|
|
},
|
|
} |