import { mapKey } from '../config.js' // #ifdef H5 import { jsonp } from 'vue-jsonp' // #endif /** * 百度坐标系 (BD-09) 转 火星坐标系 (GCJ-02) * 将百度地图经纬度转换为腾讯/高德地图经纬度 * @param {Object} lng * @param {Object} lat */ export function bMapTransQQMap(lng, lat) { let x_pi = Math.PI * 3000.0 / 180.0; let x = lng - 0.0065; let y = lat - 0.006; let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi); let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi); let lngs = z * Math.cos(theta); let lats = z * Math.sin(theta); return { longitude: lngs, latitude: lats } } /** * 火星坐标系 (GCJ-02) 转 百度坐标系 (BD-09) * 将腾讯/高德地图经纬度转换为百度地图经纬度 * @param {Object} lng * @param {Object} lat */ export function qqMapTransBMap(lng, lat) { let x_pi = Math.PI * 3000.0 / 180.0; let x = lng; let y = lat; let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi); let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi); let lngs = z * Math.cos(theta) + 0.0065; let lats = z * Math.sin(theta) + 0.006; return { longitude: lngs, latitude: lats } } // >> 高德地图 << // /** // * 定位 // * @param {Boolean} geocode 默认false,是否解析地址信息 // */ // export function getLocation(geocode = true) { // return new Promise((resolve, reject) => { // uni.getLocation({ // type: 'gcj02', // geocode: geocode, // success: data => { // console.log('定位结果:', data) // if (!geocode) { // resolve(data) // return // } // // #ifdef APP-PLUS // const d = data.address || {} // const addr = `${d.province || ''}${d.city || ''}${d.district || ''}${d.street || ''}${d.streetNum || ''}${d.poiName || ''}` // resolve({ // ...data, // addr // }) // // #endif // // #ifndef APP-PLUS // getNearbyPois({ // location: `${data.longitude},${data.latitude}`, // extensions: 'base' // }).then(p_data => { // const addr = p_data.regeocode.formatted_address instanceof Array ? '' : p_data.regeocode.formatted_address // resolve({ // ...data, // addr // }) // }).catch(error => { // reject(error) // }) // // #endif // }, // fail: error => { // reject(error) // } // }) // }) // } // /** // * 地图专用API请求(主要是为了处理跨域问题) // * @author yxk // * @param {Object} url // * @param {Object} param // */ // const mapRequest = function(url, params) { // return new Promise((resolve, reject) => { // params = { // key: mapKey, // output: 'json', // ...params // } // // #ifdef H5 // jsonp(url, params).then(data => { // resolve(data) // }).catch(error => { // reject(error) // }) // // #endif // // #ifndef H5 // uni.request({ // url: url, // method: 'GET', // data: params, // success: data => { // resolve(data.data) // }, // fail: error => { // reject(error) // } // }) // // #endif // }) // } // /** // * 获取附近的POI点 // */ // export function getNearbyPois(params) { // const url = 'https://restapi.amap.com/v3/geocode/regeo' // return mapRequest(url, { // radius: 200, // ...params // }) // } // /** // * 关键字搜索 POIS // */ // export function searchPois(params) { // const url = 'https://restapi.amap.com/v3/place/text' // return mapRequest(url, params) // } // /** // * 周边搜索POIS // */ // export function searchAroundPois(params) { // const url = 'https://restapi.amap.com/v3/place/around' // return mapRequest(url, params) // } // >> 腾讯地图 << /** * 定位 * @param {Boolean} geocode 默认false,是否解析地址信息 */ export function getLocation(geocode = true) { return new Promise((resolve, reject) => { uni.getLocation({ type: 'gcj02', geocode: geocode, success: data => { console.log('定位结果:', data) if (!geocode) { resolve(data) return } // #ifdef APP-PLUS const d = data.address || {} const addr = `${d.province || ''}${d.city || ''}${d.district || ''}${d.street || ''}${d.streetNum || ''}${d.poiName || ''}` resolve({ ...data, addr }) // #endif // #ifndef APP-PLUS getNearbyPois({ location: `${data.latitude},${data.longitude}` }).then(p_data => { console.log('pddd', p_data) resolve({ addr: p_data.result.address, address_component: p_data.result.address_component || {}, ...data }) }).catch(error => { reject(error) }) // #endif }, fail: error => { reject(error) } }) }) } /** * 地图专用API请求(主要是为了处理跨域问题) * @author yxk * @param {Object} url * @param {Object} param */ const mapRequest = function(url, params) { return new Promise((resolve, reject) => { params = { key: mapKey, output: 'json', ...params } // #ifdef H5 params.output = 'jsonp' jsonp(url, params).then(data => { resolve(data) }).catch(error => { reject(error) }) // #endif // #ifndef H5 uni.request({ url: url, method: 'GET', data: params, success: data => { resolve(data.data) }, fail: error => { reject(error) } }) // #endif }) } /** * 获取附近的POI点 */ export function getNearbyPois(params) { const url = 'https://apis.map.qq.com/ws/geocoder/v1/' return mapRequest(url, params) } /** * 关键字搜索 POIS */ export function searchPois(params) { const url = 'https://apis.map.qq.com/ws/place/v1/suggestion' return mapRequest(url, params) } /** * 周边搜索POIS */ export function searchAroundPois(params) { const url = 'https://apis.map.qq.com/ws/geocoder/v1/' return mapRequest(url, params) }