-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathindex.js
172 lines (164 loc) · 7.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import { Linking, ActionSheetIOS, 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])))
.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 => {
// 系统内没有任何地图, 推荐下载一个
if (choices.length < 1) {
return Alert.alert('选择地图', '您还没有安装地图软件。', [
{ text: '下载高德地图', onPress: () => Linking.openURL('http://mobile.amap.com') },
{ text: '下载百度地图', onPress: () => Linking.openURL('http://map.baidu.com') },
{ text: '取消' }
]);
}
return Alert.alert('选择地图', '请选择一个地图打开', [...choices, { text: '取消' }]);
});
}
}
export default {
options: { appName: 'MapLinking' },
setOptions(opts) {
this.options = { ...this.options, ...opts };
},
/**
* 在地图上标注指定位置
*
* @param location 位置, {lat:40, lng: 118, type: 'gcj02'}
* @param title 标题
* @param content 内容
*/
markLocation(location, title, content) {
return openDialog({
android: [
[
'使用高德地图打开',
`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}`,
],
],
});
},
/**
* 规划线路
*
* @param srcLocation 起始位置: {lat:40, lng: 118, title: '起点'}
* @param distLocation 目的位置: {lat:40, lng: 118, type: 'gcj02', title: '终点'}
* @param mode 交通方式: drive - 驾车, bus - 公交, walk - 步行
*/
planRoute(srcLocation, distLocation, mode) {
return openDialog({
android: [
[
'使用高德地图规划路线',
`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')}`,
],
[
'使用百度地图规划路线',
`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')}`,
],
],
});
},
/**
* 启动导航
*
* @param distLocation 目的位置: {lat:40, lng: 118, type: 'gcj02', title: '终点'}
*/
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}`,
],
[
'使用iOS系统地图导航',
`http://maps.apple.com/?ll=${distLocation.lat + ',' + distLocation.lng}&q=${distLocation.title}&dirflg=d`,
],
],
});
},
};