123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 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() {
- return new Promise((resolve, reject) => {
- window.AMap.plugin('AMap.Geolocation', () => {
- const geolocation = new window.AMap.Geolocation({
- // 是否使用高精度定位,默认:true
- enableHighAccuracy: true,
- // 设置定位超时时间,默认:无穷大
- timeout: 10000
- })
- geolocation.getCurrentPosition((status, result) => {
- if (status === 'complete') {
- resolve(result)
- } else {
- reject(result)
- }
- })
- })
- })
- }
- // 地址导航
- export function mapNavigate(options = {}, origin) {
- console.log(options)
- // 百度
- if (origin === 'baidu') {
- 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`
- }
- // 腾讯
- if (origin === 'tx') {
- console.log('腾讯地图')
- options.locationUrl = `https://apis.map.qq.com/uri/v1/marker?marker=coord:${options.lat},${options.lng};${options.title};addr:${options.address}&referer=BWUBZ-LRLCQ-JON5T-GJLC4-URIMQ-CRBO6`
- }
- // 高德
- if (origin === 'gaode') {
- console.log('高德地图')
- options.locationUrl = `https://uri.amap.com/marker?position=${options.lng},${options.lat}&name=${options.title}&coordinate=gaode&callnative=0`
- }
- // window.open(options.locationUrl)
- window.location.href = options.locationUrl
- }
|