123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- 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)
- }
|