useMap.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // 文档地址 https://lbs.qq.com/service/webService/webServiceGuide/search/webServiceSearch
  2. // 注意当前请求仅打包后有效
  3. import request from '@/utils/request'
  4. import {
  5. ref
  6. } from "vue"
  7. import {
  8. jsonp
  9. } from "@/utils/common.js"
  10. // 关键词不全
  11. const httpUrl = "https://apis.map.qq.com/ws/place/v1/search"
  12. const Key = `KBYBZ-FS3KZ-U2FXR-7VPDX-L7J46-23F55`
  13. export function useCustomMap() {
  14. // 关键字 范围 页码 数量 成功回调 失败回调
  15. function getPositionSearchByKeyword(keyword, boundary, successFun, errorFun, options) {
  16. let cpage = options.page || 1;
  17. let csize = options.size || 20;
  18. let cboundary = boundary; //'nearby(39.992870,116.310250,1000)'
  19. uni.showLoading({
  20. title: '加载中'
  21. })
  22. // #ifdef H5
  23. jsonp(httpUrl, {
  24. key: Key,
  25. output: "jsonp",
  26. keyword: keyword,
  27. boundary: cboundary,
  28. page_size: csize,
  29. page_index: cpage,
  30. }).then(res => {
  31. successFun && successFun(res)
  32. }).catch(err => {
  33. errorFun && errorFun(err);
  34. }).finally(() => {
  35. uni.hideLoading();
  36. })
  37. // #endif
  38. // #ifdef APP
  39. uni.request({
  40. url: httpUrl,
  41. method: 'get',
  42. data: {
  43. key: Key,
  44. output: "JSON",
  45. keyword: keyword,
  46. boundary: cboundary,
  47. page_size: csize,
  48. page_index: cpage,
  49. },
  50. timeout: 20000,
  51. }).then(res => {
  52. successFun && successFun(res)
  53. }).catch(err => {
  54. console.log('err')
  55. errorFun && errorFun(err);
  56. }).finally(() => {
  57. uni.hideLoading();
  58. })
  59. // #endif
  60. }
  61. return {
  62. getPositionSearchByKeyword
  63. }
  64. }