diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..33ec01a8d9e1f771f3eba182b03f1f4097370679 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# @react-native-ohos/react-native-map-linking + +This project is based on [react-native-map-linking](<[原库github/gitee链接](https://github.com/starlight36/react-native-map-linking)>) + +## Documentation + +- [中文](https://gitee.com/react-native-oh-library/usage-docs/blob/master/zh-cn/react-native-map-linking.md) + +- [English](https://gitee.com/react-native-oh-library/usage-docs/blob/master/en/react-native-map-linking.md) + +## License + +This library is licensed under [The MIT License (MIT)](https://github.com/starlight36/react-native-map-linking/blob/master/LICENSE) diff --git a/index.js b/index.js index 8ae8ad1ae3338ffb2cb635ee435d0ab720eb2f47..b8bfe80b65ee1eb3f405d1e8db86dcc1f364f8fe 100644 --- a/index.js +++ b/index.js @@ -1,58 +1,40 @@ -import { Linking, ActionSheetIOS, Alert, Platform } from 'react-native'; +import { Linking, Alert, Platform } from 'react-native'; function openDialog(urls) { - if (Platform.OS === 'ios') { - urls = urls.ios; - return Promise.all(urls.map(element => Linking.canOpenURL(element[1]))) + if (Platform.OS === 'harmony') { + urls = urls.harmony; + return Promise.all(urls.map((element) => Linking.canOpenURL(element[1]))) .then((results) => { - return urls.filter((element, index) => results[index]); - }).then(choices => { - // 系统内没有任何地图, 推荐下载一个 - if (choices.length < 1) { - return ActionSheetIOS.showActionSheetWithOptions({ - options: ['下载高德地图', '下载百度地图', '取消'], - cancelButtonIndex: 2, - title: '选择地图', - }, buttonIndex => { - if (buttonIndex === 0) { - Linking.openURL('https://itunes.apple.com/cn/app/gao-tu-zhuan-ye-shou-ji-tu/id461703208?mt=8'); - } else if (buttonIndex === 1) { - Linking.openURL('https://itunes.apple.com/cn/app/bai-du-tu-shou-ji-tu-lu-xian/id452186370?mt=8'); - } - }); - } - - return ActionSheetIOS.showActionSheetWithOptions({ - options: [...(choices.map(element => element[0])), '取消'], - cancelButtonIndex: choices.length, - title: '选择地图', - }, buttonIndex => { - if (buttonIndex < choices.length) { - Linking.openURL(choices[buttonIndex][1]); - } - }); - }); - } else if (Platform.OS === 'android') { - urls = urls.android; - return Promise.all(urls.map(element => Linking.canOpenURL(element[1]))) - .then((results) => { - return urls.filter((element, index) => results[index]).map(url => ({ - text: url[0], - onPress: () => { - Linking.openURL(url[1]); - }, - })); - }).then(choices => { + return urls + .filter((element, index) => results[index]) + .map((url) => ({ + text: url[0], + onPress: () => { + Linking.openURL(url[1]); + }, + })); + }) + .then((choices) => { // 系统内没有任何地图, 推荐下载一个 if (choices.length < 1) { return Alert.alert('选择地图', '您还没有安装地图软件。', [ - { text: '下载高德地图', onPress: () => Linking.openURL('http://mobile.amap.com') }, - { text: '下载百度地图', onPress: () => Linking.openURL('http://map.baidu.com') }, - { text: '取消' } + { + text: '下载高德地图', + onPress: () => + Linking.openURL( + 'https://www.amap.com/download' + ), + }, + // { text: '下载百度地图', onPress: () => Linking.openURL('') },//百度地图暂无应用市场地址 + { text: '取消' }, + { cancelable: true }, ]); } - return Alert.alert('选择地图', '请选择一个地图打开', [...choices, { text: '取消' }]); + return Alert.alert('选择地图', '请选择一个地图打开', [ + ...choices, + { text: '取消' }, + ]); }); } } @@ -73,28 +55,11 @@ export default { */ markLocation(location, title, content) { return openDialog({ - android: [ + harmony: [ + // 当前版本暂不支持拉起其他地图标注指定位置 [ - '使用高德地图打开', - `androidamap://viewMap?sourceApplication=${this.options.appName}&poiname=${title}&lat=${location.lat}&lon=${location.lng}&dev=${location.type === 'gcj02' ? '0' : '1'}`, - ], - [ - '使用百度地图打开', - `bdapp://map/marker?location=${location.lat},${location.lng}&coord_type=${location.type === 'gcj02' ? 'gcj02' : 'wgs84'}&title=${title}&content=${content}&src=${this.options.appName}`, - ] - ], - ios: [ - [ - '使用高德地图打开', - `iosamap://viewMap?sourceApplication=${this.options.appName}&poiname=${title}&lat=${location.lat}&lon=${location.lng}&dev=${location.type === 'gcj02' ? '0' : '1'}`, - ], - [ - '使用百度地图打开', - `baidumap://map/marker?location=${location.lat},${location.lng}&coord_type=${location.type === 'gcj02' ? 'gcj02' : 'wgs84'}&title=${title}&content=${content}&src=${this.options.appName}`, - ], - [ - '使用iOS系统地图打开', - `http://maps.apple.com/?ll=${location.lat},${location.lng}&q=${title}`, + '使用花瓣地图打开', + `maps://locationInfo?linkSource=${this.options.appName}&destinationLatitude=${location.lat}&destinationLongitude=${location.lng}&destinationName=${title}`, ], ], }); @@ -109,28 +74,22 @@ export default { */ planRoute(srcLocation, distLocation, mode) { return openDialog({ - android: [ + harmony: [ + // 当前版本暂不支持调用其他地图的规划线路功能 [ '使用高德地图规划路线', - `androidamap://route?sourceApplication=${this.options.appName}&slat=${srcLocation && srcLocation.lat}&slon=${srcLocation && srcLocation.lng}&sname=${srcLocation && srcLocation.title}&dlat=${distLocation.lat}&dlon=${distLocation.lng}&dname=${distLocation.title}&dev=${distLocation.type === 'gcj02' ? '0' : '1'}&m=0&t=${mode === 'drive' ? '2' : (mode === 'bus' ? '1' : '4')}`, + `amapuri://route/plan?dlat=${distLocation.lat}&dname=${distLocation.title + }&slat=${srcLocation && srcLocation.lat}&dlon=${distLocation.lng + }&slon=${srcLocation && srcLocation.lng}&t=${mode === 'drive' ? '0' : mode === 'bus' ? '1' : '2' + }&sourceApplication=${this.options.appName}`, ], [ - '使用百度地图规划路线', - `bdapp://map/direction?origin=${srcLocation ? (srcLocation.lat + ',' + srcLocation.lng) : ''}&destination=${distLocation.lat},${distLocation.lng}&mode=${mode === 'drive' ? 'driving' : (mode === 'bus' ? 'transit' : 'walking')}&coord_type=${distLocation.type === 'gcj02' ? 'gcj02' : 'wgs84'}&src=${this.options.appName}`, - ] - ], - ios: [ - [ - '使用高德地图规划路线', - `iosamap://path?sourceApplication=${this.options.appName}&slat=${srcLocation && srcLocation.lat}&slon=${srcLocation && srcLocation.lng}&sname=${srcLocation && srcLocation.title}&dlat=${distLocation.lat}&dlon=${distLocation.lng}&dname=${distLocation.title}&dev=${distLocation.type === 'gcj02' ? '0' : '1'}&t=${mode === 'drive' ? '0' : (mode === 'bus' ? '1' : '2')}`, - ], - [ - '使用百度地图规划路线', - `baidumap://map/direction?origin=${srcLocation ? (srcLocation.lat + ',' + srcLocation.lng) : ''}&destination=${distLocation.lat},${distLocation.lng}&mode=${mode === 'drive' ? 'driving' : (mode === 'bus' ? 'transit' : 'walking')}&coord_type=${distLocation.type === 'gcj02' ? 'gcj02' : 'wgs84'}&src=${this.options.appName}`, - ], - [ - '使用iOS系统地图规划路线', - `http://maps.apple.com/?ll=${distLocation.lat},${distLocation.lng}&q=${distLocation.title}&dirflg=${mode === 'drive' ? 'd' : (mode === 'bus' ? 'r' : 'w')}`, + '使用花瓣地图规划路线', + `maps://routes?linkSource=${this.options.appName}&originLatitude=${srcLocation && srcLocation.lat + }&originLongitude=${srcLocation && srcLocation.lng}&originName=${srcLocation && srcLocation.title + }&destinationLatitude=${distLocation.lat}&destinationLongitude=${distLocation.lng + }&destinationName=${distLocation.title}&vehicleType=${mode === 'drive' ? '0' : mode === 'bus' ? '3' : '1' + }`, ], ], }); @@ -143,28 +102,11 @@ export default { */ navigate(distLocation) { return openDialog({ - android: [ - [ - '使用高德地图导航', - `androidamap://navi?sourceApplication=${this.options.appName}&poiname=${distLocation.title}&lat=${distLocation.lat}&lon=${distLocation.lng}&dev=${distLocation.type === 'gcj02' ? '0' : '1'}`, - ], - [ - '使用百度地图导航', - `bdapp://map/direction?origin=&destination=${distLocation.lat},${distLocation.lng}&mode=driving&coord_type=${distLocation.type === 'gcj02' ? 'gcj02' : 'wgs84'}&src=${this.options.appName}`, - ] - ], - ios: [ - [ - '使用高德地图导航', - `iosamap://navi?sourceApplication=${this.options.appName}&poiname=${distLocation.title}&lat=${distLocation.lat}&lon=${distLocation.lng}&dev=${distLocation.type === 'gcj02' ? '0' : '1'}`, - ], - [ - '使用百度地图导航', - `baidumap://map/direction?origin=&destination=${distLocation.lat},${distLocation.lng}&mode=driving&coord_type=${distLocation.type === 'gcj02' ? 'gcj02' : 'wgs84'}&src=${this.options.appName}`, - ], + // 当前版本暂不支持拉起其他地图进行导航 + harmony: [ [ - '使用iOS系统地图导航', - `http://maps.apple.com/?ll=${distLocation.lat + ',' + distLocation.lng}&q=${distLocation.title}&dirflg=d`, + '使用花瓣地图导航', + `maps://navigation/?linkSource=${this.options.appName}&destinationLatitude=${distLocation.lat}&destinationLongitude=${distLocation.lng}&destinationName=${distLocation.title}`, ], ], }); diff --git a/package.json b/package.json index ca6b5819c59e8c36513d732be0e67b39ba6d8328..a485d4322203a1772b3ebcbf79bb66ab50cfd162 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "react-native-map-linking", + "name": "@react-native-ohos/react-native-map-linking", "version": "1.0.1", "description": "Linking to map for React Native", "main": "index.js", @@ -14,14 +14,17 @@ "url": "https://github.com/starlight36" }, "repository": { - "url": "https://github.com/starlight36/react-native-map-linking.git" + "url": "https://gitee.com/openharmony-sig/rntpc_react-native-map-linking" }, "bugs": { - "url": "https://github.com/starlight36/react-native-map-linking/issues" + "url": "https://gitee.com/openharmony-sig/rntpc_react-native-map-linking/issues" }, "license": "MIT", "devDependencies": { "react": "^15.1.0", "react-native": "^0.28.0" + }, + "harmony": { + "alias": "react-native-map-linking" } }