map.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import { mapKey } from '../config.js'
  2. // #ifdef H5
  3. import { jsonp } from 'vue-jsonp'
  4. // #endif
  5. /**
  6. * 百度坐标系 (BD-09) 转 火星坐标系 (GCJ-02)
  7. * 将百度地图经纬度转换为腾讯/高德地图经纬度
  8. * @param {Object} lng
  9. * @param {Object} lat
  10. */
  11. export function bMapTransQQMap(lng, lat) {
  12. let x_pi = Math.PI * 3000.0 / 180.0;
  13. let x = lng - 0.0065;
  14. let y = lat - 0.006;
  15. let z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
  16. let theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
  17. let lngs = z * Math.cos(theta);
  18. let lats = z * Math.sin(theta);
  19. return {
  20. longitude: lngs,
  21. latitude: lats
  22. }
  23. }
  24. /**
  25. * 火星坐标系 (GCJ-02) 转 百度坐标系 (BD-09)
  26. * 将腾讯/高德地图经纬度转换为百度地图经纬度
  27. * @param {Object} lng
  28. * @param {Object} lat
  29. */
  30. export function qqMapTransBMap(lng, lat) {
  31. let x_pi = Math.PI * 3000.0 / 180.0;
  32. let x = lng;
  33. let y = lat;
  34. let z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);
  35. let theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
  36. let lngs = z * Math.cos(theta) + 0.0065;
  37. let lats = z * Math.sin(theta) + 0.006;
  38. return {
  39. longitude: lngs,
  40. latitude: lats
  41. }
  42. }
  43. // >> 高德地图 <<
  44. // /**
  45. // * 定位
  46. // * @param {Boolean} geocode 默认false,是否解析地址信息
  47. // */
  48. // export function getLocation(geocode = true) {
  49. // return new Promise((resolve, reject) => {
  50. // uni.getLocation({
  51. // type: 'gcj02',
  52. // geocode: geocode,
  53. // success: data => {
  54. // console.log('定位结果:', data)
  55. // if (!geocode) {
  56. // resolve(data)
  57. // return
  58. // }
  59. // // #ifdef APP-PLUS
  60. // const d = data.address || {}
  61. // const addr = `${d.province || ''}${d.city || ''}${d.district || ''}${d.street || ''}${d.streetNum || ''}${d.poiName || ''}`
  62. // resolve({
  63. // ...data,
  64. // addr
  65. // })
  66. // // #endif
  67. // // #ifndef APP-PLUS
  68. // getNearbyPois({
  69. // location: `${data.longitude},${data.latitude}`,
  70. // extensions: 'base'
  71. // }).then(p_data => {
  72. // const addr = p_data.regeocode.formatted_address instanceof Array ? '' : p_data.regeocode.formatted_address
  73. // resolve({
  74. // ...data,
  75. // addr
  76. // })
  77. // }).catch(error => {
  78. // reject(error)
  79. // })
  80. // // #endif
  81. // },
  82. // fail: error => {
  83. // reject(error)
  84. // }
  85. // })
  86. // })
  87. // }
  88. // /**
  89. // * 地图专用API请求(主要是为了处理跨域问题)
  90. // * @author yxk
  91. // * @param {Object} url
  92. // * @param {Object} param
  93. // */
  94. // const mapRequest = function(url, params) {
  95. // return new Promise((resolve, reject) => {
  96. // params = {
  97. // key: mapKey,
  98. // output: 'json',
  99. // ...params
  100. // }
  101. // // #ifdef H5
  102. // jsonp(url, params).then(data => {
  103. // resolve(data)
  104. // }).catch(error => {
  105. // reject(error)
  106. // })
  107. // // #endif
  108. // // #ifndef H5
  109. // uni.request({
  110. // url: url,
  111. // method: 'GET',
  112. // data: params,
  113. // success: data => {
  114. // resolve(data.data)
  115. // },
  116. // fail: error => {
  117. // reject(error)
  118. // }
  119. // })
  120. // // #endif
  121. // })
  122. // }
  123. // /**
  124. // * 获取附近的POI点
  125. // */
  126. // export function getNearbyPois(params) {
  127. // const url = 'https://restapi.amap.com/v3/geocode/regeo'
  128. // return mapRequest(url, {
  129. // radius: 200,
  130. // ...params
  131. // })
  132. // }
  133. // /**
  134. // * 关键字搜索 POIS
  135. // */
  136. // export function searchPois(params) {
  137. // const url = 'https://restapi.amap.com/v3/place/text'
  138. // return mapRequest(url, params)
  139. // }
  140. // /**
  141. // * 周边搜索POIS
  142. // */
  143. // export function searchAroundPois(params) {
  144. // const url = 'https://restapi.amap.com/v3/place/around'
  145. // return mapRequest(url, params)
  146. // }
  147. // >> 腾讯地图 <<
  148. /**
  149. * 定位
  150. * @param {Boolean} geocode 默认false,是否解析地址信息
  151. */
  152. export function getLocation(geocode = true) {
  153. return new Promise((resolve, reject) => {
  154. uni.getLocation({
  155. type: 'gcj02',
  156. geocode: geocode,
  157. success: data => {
  158. console.log('定位结果:', data)
  159. if (!geocode) {
  160. resolve(data)
  161. return
  162. }
  163. // #ifdef APP-PLUS
  164. const d = data.address || {}
  165. const addr = `${d.province || ''}${d.city || ''}${d.district || ''}${d.street || ''}${d.streetNum || ''}${d.poiName || ''}`
  166. resolve({
  167. ...data,
  168. addr
  169. })
  170. // #endif
  171. // #ifndef APP-PLUS
  172. getNearbyPois({
  173. location: `${data.latitude},${data.longitude}`
  174. }).then(p_data => {
  175. console.log('pddd', p_data)
  176. resolve({
  177. addr: p_data.result.address,
  178. address_component: p_data.result.address_component || {},
  179. ...data
  180. })
  181. }).catch(error => {
  182. reject(error)
  183. })
  184. // #endif
  185. },
  186. fail: error => {
  187. reject(error)
  188. }
  189. })
  190. })
  191. }
  192. /**
  193. * 地图专用API请求(主要是为了处理跨域问题)
  194. * @author yxk
  195. * @param {Object} url
  196. * @param {Object} param
  197. */
  198. const mapRequest = function(url, params) {
  199. return new Promise((resolve, reject) => {
  200. params = {
  201. key: mapKey,
  202. output: 'json',
  203. ...params
  204. }
  205. // #ifdef H5
  206. params.output = 'jsonp'
  207. jsonp(url, params).then(data => {
  208. resolve(data)
  209. }).catch(error => {
  210. reject(error)
  211. })
  212. // #endif
  213. // #ifndef H5
  214. uni.request({
  215. url: url,
  216. method: 'GET',
  217. data: params,
  218. success: data => {
  219. resolve(data.data)
  220. },
  221. fail: error => {
  222. reject(error)
  223. }
  224. })
  225. // #endif
  226. })
  227. }
  228. /**
  229. * 获取附近的POI点
  230. */
  231. export function getNearbyPois(params) {
  232. const url = 'https://apis.map.qq.com/ws/geocoder/v1/'
  233. return mapRequest(url, params)
  234. }
  235. /**
  236. * 关键字搜索 POIS
  237. */
  238. export function searchPois(params) {
  239. const url = 'https://apis.map.qq.com/ws/place/v1/suggestion'
  240. return mapRequest(url, params)
  241. }
  242. /**
  243. * 周边搜索POIS
  244. */
  245. export function searchAroundPois(params) {
  246. const url = 'https://apis.map.qq.com/ws/geocoder/v1/'
  247. return mapRequest(url, params)
  248. }