adress.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import config from '@/config'
  2. const mapKey = config.mapKey;
  3. const appName = config.appName;
  4. /**
  5. * 腾讯位置服务地图选点
  6. */
  7. const chooseLocationInit = () => {
  8. const key = mapKey; //使用在腾讯位置服务申请的key
  9. const referer = appName; //调用插件的app的名称
  10. wx.navigateTo({
  11. url: `plugin://chooseLocation/index?key=${key}&referer=${referer}`,
  12. });
  13. }
  14. /**
  15. * 腾讯位置服务城市选择器
  16. * @param {*} city 城市名称
  17. */
  18. const citySelectorNavigateTo = (city) => {
  19. const key = mapKey; // 使用在腾讯位置服务申请的key
  20. const referer = appName; // 调用插件的app的名称
  21. const hotCitys = '重庆'; // 用户自定义的的热门城市
  22. wx.navigateTo({
  23. url: `plugin://citySelector/index?key=${key}&referer=${referer}&hotCitys=${hotCitys}`,
  24. })
  25. }
  26. function splitAddress(address) {
  27. // 处理直辖市(如北京市朝阳区)
  28. const directCityRegex = /^(北京|天津|上海|重庆)(市)?(.+?区)/;
  29. const directMatch = address.match(directCityRegex);
  30. if (directMatch) {
  31. return {
  32. province: directMatch[1],
  33. city: directMatch[1]+'市',
  34. district: directMatch[3],
  35. detail: address.replace(directCityRegex, '')
  36. }
  37. }
  38. // 常规拆分逻辑
  39. const provinceRegex = /(.+?(省|自治区|行政区|特别行政区))/;
  40. const cityRegex = /(.+?(市|自治州|州|盟))/;
  41. const province = address.match(provinceRegex)?.[1] || '';
  42. const city = address.replace(province, '').match(cityRegex)?.[1] || '';
  43. const remaining = address.replace(province + city, '');
  44. const district = remaining.match(/(.+?( 区|县|市|旗))/)?.[1] || '';
  45. const detail = remaining.replace(district, '').trim();
  46. return {province, city, district, detail};
  47. }
  48. export {
  49. chooseLocationInit,
  50. citySelectorNavigateTo,
  51. splitAddress
  52. }