|
@@ -1,49 +1,3 @@
|
|
-function mapInit(callback) {
|
|
|
|
- function loadScript() {
|
|
|
|
- var script = document.createElement('script')
|
|
|
|
- script.src =
|
|
|
|
- 'https://api.map.baidu.com/api?v=1.0&type=webgl&ak=vsIfSztpPmCtmBRfRiIAM57hbxBQbmgQ&callback=initMapApi'
|
|
|
|
- document.body.appendChild(script)
|
|
|
|
- }
|
|
|
|
- if (!window.BMapGL) {
|
|
|
|
- window.initMapApi = callback
|
|
|
|
- loadScript()
|
|
|
|
- } else {
|
|
|
|
- callback()
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-// 定位当前位置
|
|
|
|
-export function loactionSelf() {
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
|
- mapInit(() => {
|
|
|
|
- const BMapGL = window.BMapGL
|
|
|
|
- const geolocation = new BMapGL.Geolocation({
|
|
|
|
- // 是否使用高精度定位,默认:true
|
|
|
|
- enableHighAccuracy: true,
|
|
|
|
- // 设置定位超时时间,默认:无穷大
|
|
|
|
- timeout: 10000,
|
|
|
|
- })
|
|
|
|
- // 开启SDK辅助定位
|
|
|
|
- geolocation.enableSDKLocation()
|
|
|
|
- geolocation.getCurrentPosition(function (r) {
|
|
|
|
- // alert(JSON.stringify(r))
|
|
|
|
- console.log(r)
|
|
|
|
- // console.log(this)
|
|
|
|
- if (this.getStatus() === 0) {
|
|
|
|
- // alert(JSON.stringify(r))
|
|
|
|
- resolve({
|
|
|
|
- point: r.point,
|
|
|
|
- address: r.address,
|
|
|
|
- })
|
|
|
|
- } else {
|
|
|
|
- reject('failed' + this.getStatus())
|
|
|
|
- }
|
|
|
|
- })
|
|
|
|
- })
|
|
|
|
- })
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
export function geolocation() {
|
|
export function geolocation() {
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
window.AMap.plugin('AMap.Geolocation', () => {
|
|
window.AMap.plugin('AMap.Geolocation', () => {
|
|
@@ -51,7 +5,7 @@ export function geolocation() {
|
|
// 是否使用高精度定位,默认:true
|
|
// 是否使用高精度定位,默认:true
|
|
enableHighAccuracy: true,
|
|
enableHighAccuracy: true,
|
|
// 设置定位超时时间,默认:无穷大
|
|
// 设置定位超时时间,默认:无穷大
|
|
- timeout: 10000
|
|
|
|
|
|
+ timeout: 10000,
|
|
})
|
|
})
|
|
|
|
|
|
geolocation.getCurrentPosition((status, result) => {
|
|
geolocation.getCurrentPosition((status, result) => {
|
|
@@ -65,14 +19,30 @@ export function geolocation() {
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+// 高德转百度坐标
|
|
|
|
+export function convertor(lng, lat) {
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
+ const convertor = new window.BMapGL.Convertor()
|
|
|
|
+ // 高德转百度
|
|
|
|
+ convertor.translate([new window.BMapGL.Point(lng, lat)], 3, 5, (data) => {
|
|
|
|
+ if (data.status === 0) {
|
|
|
|
+ resolve(data.points[0])
|
|
|
|
+ } else {
|
|
|
|
+ reject()
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ })
|
|
|
|
+}
|
|
|
|
|
|
// 地址导航
|
|
// 地址导航
|
|
-export function mapNavigate(options = {}, origin) {
|
|
|
|
|
|
+export async function mapNavigate(options = {}, origin) {
|
|
console.log(options)
|
|
console.log(options)
|
|
// 百度
|
|
// 百度
|
|
if (origin === 'baidu') {
|
|
if (origin === 'baidu') {
|
|
|
|
+ const point = await convertor(options.lng, options.lat)
|
|
|
|
+ console.log(point)
|
|
console.log('百度地图')
|
|
console.log('百度地图')
|
|
- options.locationUrl = `http://api.map.baidu.com/marker?location=${options.lat},${options.lng}&title=${options.title}&content=${options.address}&output=html&src=webapp.baidu.openAPIdemo`
|
|
|
|
|
|
+ options.locationUrl = `http://api.map.baidu.com/marker?location=${point.lat},${point.lng}&title=${options.title}&content=${options.address}&output=html&src=webapp.baidu.openAPIdemo`
|
|
}
|
|
}
|
|
// 腾讯
|
|
// 腾讯
|
|
if (origin === 'tx') {
|
|
if (origin === 'tx') {
|
|
@@ -84,7 +54,7 @@ export function mapNavigate(options = {}, origin) {
|
|
console.log('高德地图')
|
|
console.log('高德地图')
|
|
options.locationUrl = `https://uri.amap.com/marker?position=${options.lng},${options.lat}&name=${options.title}&coordinate=gaode&callnative=0`
|
|
options.locationUrl = `https://uri.amap.com/marker?position=${options.lng},${options.lat}&name=${options.title}&coordinate=gaode&callnative=0`
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ // return
|
|
// window.open(options.locationUrl)
|
|
// window.open(options.locationUrl)
|
|
window.location.href = options.locationUrl
|
|
window.location.href = options.locationUrl
|
|
}
|
|
}
|