template-project/jsFile/map/yaya-map.js

329 lines
9.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

const tools = require('../tools.js')
// 解决微信小程序
var QQMapWX = require('./qqmap-wx-jssdk.min.js');
// 腾讯地图
var qqmapsdk = new QQMapWX({
key: 'QNHBZ-55RKF-OMFJJ-NPU7O-EPSDH-ACBAA'
});
// 解决H5跨域
const jsonp = function(url, data) {
return new Promise((resolve, reject) => {
// 1.初始化url
let dataString = url.indexOf('?') === -1 ? '?' : '&'
let callbackName = `jsonpCB_${ Date.now() }`;
url += `${ dataString }callback=${ callbackName }`
if(data) {
// 2.有请求参数依次添加到url
for(let k in data) {
url += `&${ k }=${ data[k] }`
}
}
let scriptNode = document.createElement('script');
scriptNode.src = url;
// 3. callback
window[callbackName] = (result) => {
result ? resolve(result) : reject('没有返回数据');
delete window[callbackName];
document.body.removeChild(scriptNode);
}
// 4. 异常情况
scriptNode.addEventListener('error', () => {
reject('接口返回数据失败');
delete window[callbackName];
document.body.removeChild(scriptNode);
}, false)
// 5. 开始请求
document.body.appendChild(scriptNode)
})
}
const yayaMap = {
// 判断app、小程序是否开启GPS定位服务
checkOpenGPSServiceByAndroidIOS() {
// #ifdef APP-PLUS
let system = uni.getSystemInfoSync(); // 获取系统信息
if (system.platform === 'android') { // 判断平台
var context = plus.android.importClass("android.content.Context");
var locationManager = plus.android.importClass("android.location.LocationManager");
var main = plus.android.runtimeMainActivity();
var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
uni.showModal({
title: '提示',
content: '请打开定位服务功能',
showCancel: false, // 不显示取消按钮
success() {
if (!mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER)) {
var Intent = plus.android.importClass('android.content.Intent');
var Settings = plus.android.importClass('android.provider.Settings');
var intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
main.startActivity(intent); // 打开系统设置GPS服务页面
} else {
console.log('GPS功能已开启');
}
}
});
}
} else if (system.platform === 'ios') {
console.log("苹果");
var cllocationManger = plus.ios.import("CLLocationManager");
var enable = cllocationManger.locationServicesEnabled();
var status = cllocationManger.authorizationStatus();
plus.ios.deleteObject(cllocationManger);
if (enable && status != 2) {
console.log("手机系统的定位已经打开");
} else {
console.log("手机系统的定位没有打开");
uni.showModal({
title: '提示',
content: '请前往设置-隐私-定位服务打开定位服务功能',
showCancel: false, // 不显示取消按钮
success() {
var UIApplication = plus.ios.import("UIApplication");
var application2 = UIApplication.sharedApplication();
var NSURL2 = plus.ios.import("NSURL");
// var setting2 = NSURL2.URLWithString("prefs:root=LOCATION_SERVICES");
// var setting2 = NSURL2.URLWithString("App-Prefs:root=LOCATION_SERVICES");
var setting2 = NSURL2.URLWithString("app-settings:");
//var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION");
// var setting2 = NSURL2.URLWithString("App-Prefs:root=Privacy&path=LOCATION_SERVICES");
application2.openURL(setting2);
plus.ios.deleteObject(setting2);
plus.ios.deleteObject(NSURL2);
plus.ios.deleteObject(application2);
}
});
}
}
// #endif
// #ifdef MP-WEIXIN
wx.getSetting({
success: (res) => {
console.log(res,99);
if (!res.authSetting['scope.userLocation']) {
//打开提示框,提示前往设置页面
uni.showModal({
title:'为了更好的体验,请开启定位服务',
confirmText:'开启',
cancelText:'关闭',
success:(res)=> {
if(res.confirm) {
wx.openSetting({success (res) {}})
}
}
})
}
}
})
// #endif
},
// 平台判断
platformEv(){
// #ifdef APP-PLUS
this.getAddress();
// #endif
// #ifdef MP-WEIXIN
this.getAddressWx();
// #endif
// #ifdef H5
this.getAddressH5();
// #endif
},
/**
* @description app获取经纬度和详细地址方法
*/
getAddress(){
uni.getLocation({
type: 'gcj02',
geocode:true,
success: (res)=> {
// console.log(res,'地址信息');
let params = {
latitude:res.latitude,
longitude:res.longitude,
address:`${res.address.province}${res.address.city}${res.address.district || ''}${res.address.street || ''}${res.address.streetNum || ''}${res.address.poiName || ''}`
}
console.log(params,'APP');
}
});
},
/**
* @description 微信获取经纬度和详细地址方法
*/
getAddressWx(){
uni.getLocation({
type: 'gcj02',
geocode:true,
success: (res)=> {
// console.log(res,'地址信息');
qqmapsdk.reverseGeocoder({
location: {latitude: res.latitude, longitude: res.longitude},
success:(res)=> {
// console.log(res,'WX');
let params = {
latitude:res.result.location.lat,
longitude:res.result.location.lng,
address:`${res.result.address}`
}
console.log(params,'WX');
},
fail(err) {
console.log(err)
}
})
}
});
},
/**
* @description H5获取经纬度和详细地址方法
*/
getAddressH5(){
uni.showLoading({
title: '定位中...',
mask:true
});
uni.getLocation({
type: 'gcj02', // wgs84 gcj02
altitude: true,
// geocode: true, // wgs84
success: (res)=> {
let str = `output=jsonp&key=QNHBZ-55RKF-OMFJJ-NPU7O-EPSDH-ACBAA&location=${res.latitude},${res.longitude}`
jsonp('https://apis.map.qq.com/ws/geocoder/v1/?'+str,{}).then(res=>{
// console.log(res,'H5');
uni.hideLoading();
if(res.status == 0){
let params = {
latitude:res.result.location.lat,
longitude:res.result.location.lng,
address:`${res.result.address}`
}
console.log(params,'H5');
}
})
}
});
},
/**
* @description 打开地图导航
*/
openLocation(latitude,longitude,name,address){
uni.openLocation({
latitude: parseFloat(latitude),
longitude: parseFloat(longitude),
name:name,
address:address,
success: ()=> {
console.log('success');
},
fail:(err)=> {
console.log(err)
}
});
},
/**
* @description 传入目的地的经纬度、地点名称、详细地址,打开地图导航到达目的地
*/
goFlag:true,
goThere(latitude=30.656693,longitude=104.136425,name="大向天诚有限责任公司",address="四川省成都市成华区双店路B口"){
if(this.goFlag){
this.goFlag = false;
// #ifdef MP-WEIXIN
wx.getLocation({//获取当前经纬度
type: 'wgs84', //返回可以用于wx.openLocation的经纬度官方提示bug: iOS 6.3.30 type 参数不生效,只会返回 wgs84 类型的坐标信息
success: (res)=> {
wx.openLocation({//​使用微信内置地图查看位置。
latitude: parseFloat(latitude),//要去的纬度-地址
longitude: parseFloat(longitude),//要去的经度-地址
name: name,
address: address,
fail:err=>{
tools.showToast('地址信息错误');
}
})
}
})
// #endif
// #ifdef APP-PLUS || H5
uni.openLocation({
latitude: parseFloat(latitude),
longitude: parseFloat(longitude),
name:name,
address:address,
success:()=> {
console.log('success');
},
fail:err=>{
console.log(err)
}
});
// #endif
setTimeout(()=>{
this.goFlag = true;
},2000)
} else {
tools.showToast('请勿多次点击');
}
},
/**
* @description 经纬度===>地址
*/
getLocation(lat,lng){
return new Promise((resolve, reject) => {
uni.getLocation({
type:'gcj02',
geocode:true,
success:(res)=> {
console.log(res)
qqmapsdk.reverseGeocoder({
location: {
latitude: res.latitude,
longitude: res.longitude
},
success:(res)=> {
console.log(res);
resolve(res)
},
fail:(res)=> {
if(res.status == 1000){
uni.showToast({
title:'请勿频繁查询,稍后再试',
icon:'none'
})
}
reject(res)
},
complete:(res)=> {
// console.log(res);
}
})
}
})
})
},
/**
* @description 地址===>经纬度
*/
getAddressLocation(address) {
return new Promise((resolve, reject) => {
qqmapsdk.geocoder({
address:address,
success: (res)=> {
resolve(res)
},
fail: (res)=> {
if(res.status == 1000){
uni.showToast({
title:'请勿频繁查询,稍后再试',
icon:'none'
})
}
reject(res)
},
complete: (res)=> {
// console.log(res);
}
})
})
},
}
export default yayaMap;