openMap.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import TransformCoordinate from './transformCoordinate.js'
  2. function openMapByDefault(latitude, longitude, name) {
  3. uni.openLocation({
  4. latitude: latitude,
  5. longitude: longitude,
  6. name: name,
  7. fail: () => {
  8. uni.showModal({
  9. content: '打开地图失败,请重'
  10. })
  11. },
  12. })
  13. }
  14. function openMapByAndroid(latitude, longitude, name) {
  15. let url = ''; // 回调地址
  16. let identity = ''; // 程序名称
  17. if(plus.runtime.isApplicationExist({pname: 'com.baidu.BaiduMap'})) { // baidumap
  18. url = `baidumap://map/marker?location=${latitude},${longitude}&title=${name}&coord_type=gcj02&src=andr.baidu.openAPIdemo`
  19. identity = 'com.baidu.BaiduMap'
  20. openURL(url, identity)
  21. }
  22. else if(plus.runtime.isApplicationExist({pname: 'com.autonavi.minimap'})) { // 高德
  23. url = `androidamap://viewMap?sourceApplication=appname&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`
  24. identity = 'com.autonavi.minimap'
  25. openURL(url, identity)
  26. }
  27. else {
  28. openMapByDefault(latitude, longitude, name)
  29. }
  30. }
  31. function openMapByIos(latitude, longitude, name) {
  32. let url = ''; // 回调地址
  33. let errorCB = ''; // url失败的回调地址
  34. let identity = ''; // 程序名称
  35. if(plus.runtime.isApplicationExist({action: 'baidumap://'})) { // baidumap
  36. url = `baidumap://map/marker?location=${latitude},${longitude}&title=${name}&content=${name}&src=ios.baidu.openAPIdemo&coord_type=gcj02`;
  37. openURL(url, identity)
  38. }
  39. else if(plus.runtime.isApplicationExist({action: 'iosamap://'})) { // 高德
  40. url = `iosamap://viewMap?sourceApplication=applicationName&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`
  41. openURL(url, identity)
  42. }
  43. else {
  44. openMapByDefault(latitude, longitude, name)
  45. }
  46. }
  47. function openURL(url, identity ) {
  48. let newurl = encodeURI(url);
  49. plus.runtime.openURL( newurl, function(res){
  50. uni.showModal({
  51. content: res.message
  52. })
  53. }, identity);
  54. }
  55. function getCoordByType(longitude, latitude, coord_type) {
  56. switch (coord_type){
  57. case 'gcj02':
  58. return [longitude, latitude]
  59. break;
  60. case 'bd09':
  61. return TransformCoordinate.bd09togcj02(longitude, latitude)
  62. break;
  63. case 'wgs84':
  64. return TransformCoordinate.wgs84togcj02(longitude, latitude)
  65. break;
  66. default:
  67. return [longitude, latitude]
  68. break;
  69. }
  70. }
  71. export default {
  72. /* 打开地图 */
  73. openMap(latitude, longitude, name, coord_type='gcj02') {
  74. let arr = getCoordByType(longitude, latitude, coord_type)
  75. // #ifdef APP-PLUS
  76. switch(uni.getSystemInfoSync().platform){
  77. case 'android':
  78. console.log('运行Android上')
  79. openMapByAndroid(arr[1], arr[0], name)
  80. break;
  81. case 'ios':
  82. console.log('运行iOS上')
  83. openMapByIos(arr[1], arr[0], name)
  84. break;
  85. default:
  86. openMapByDefault(arr[1], arr[0], name)
  87. console.log('运行在开发者工具上')
  88. break;
  89. }
  90. // #endif
  91. // #ifndef APP-PLUS
  92. openMapByDefault(arr[1], arr[0], name)
  93. // #endif
  94. }
  95. }