-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathios.js
194 lines (173 loc) · 7.83 KB
/
ios.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
'use strict';
import {NativeModules} from 'react-native';
let ln = require('./constants');
/*********************
* Internal properties
*********************/
let RNLaunchNavigator = NativeModules.RNLaunchNavigator;
/******************
* Public Constants
******************/
// Launch modes for Apple Maps
ln.LAUNCH_MODE = {
URI_SCHEME: "uri_scheme",
MAPKIT: "mapkit"
};
/******************
* Public API
******************/
/**
* Opens navigator app to navigate to given destination, specified by either place name or lat/lon.
* If a start location is not also specified, current location will be used for the start.
*
* @param {string/number[]} destination (required) - destination location to use for navigation.
* Either:
* - a {string} containing the address. e.g. "Buckingham Palace, London"
* - an {array}, where the first element is the latitude and the second element is a longitude, as decimal numbers. e.g. [50.1, -4.0]
*
* @param {object} options (optional) - optional parameters:
*
* - {string} app - navigation app to use for directions, as a constant. e.g. LaunchNavigator.APP.WAZE
* If not specified, defaults to Apple Maps.
*
* - {string} destinationName - nickname to display in app for destination. e.g. "Bob's House".
*
* - {mixed} start - start location to use for navigation. If not specified, the current location of the device will be used.
* Either:
* - a {string} containing the address. e.g. "Buckingham Palace, London"
* - an {array}, where the first element is the latitude and the second element is a longitude, as decimal numbers. e.g. [50.1, -4.0]
*
* - {string} startName - nickname to display in app for start. e.g. "My Place".
*
* - {string} transportMode - transportation mode for navigation.
* Defaults to "driving" if not specified.
*
* - {string} launchMode - method to use to open Apple Maps app:
* - `LaunchNavigator.LAUNCH_MODE.URI_SCHEME` or `LaunchNavigator.MAPKIT`
* - Defaults to `LaunchNavigator.LAUNCH_MODE.URI_SCHEME` if not specified.
*
* - {object} extras - a key/value map of extra app-specific parameters. For example, to tell Google Maps to display Satellite view in "maps" launch mode: `{"t": "k"}`
*
* - {boolean} enableGeocoding - if true, and input location type(s) doesn't match those required by the app, use geocoding to obtain the address/coords as required. Defaults to TRUE.
*
* @return Promise
* resolve - invoke when the navigation app is successfully launched.
* reject - if an error is encountered while launching the app.
* A single string argument containing the error message will be passed.
*/
ln.navigate = function(destination, options) {
options = ln.util.conformNavigateOptions(arguments);
// Set defaults
options.transportMode = options.transportMode ? options.transportMode : ln.TRANSPORT_MODE.DRIVING;
options.enableGeocoding = typeof options.enableGeocoding !== "undefined" ? options.enableGeocoding : true;
options.launchMode = typeof options.launchMode !== "undefined" ? options.launchMode : ln.LAUNCH_MODE.URI_SCHEME;
// Input validation
var throwError = function(errMsg){
throw new Error(errMsg);
};
if(!destination){
throwError("No destination was specified");
}
if(options.extras && typeof options.extras !== "object"){
throwError("'options.extras' must be a key/value object");
}
if(options.extras) options.extras = JSON.stringify(options.extras);
// Default to Apple Maps if not specified
if(!options.app) options.app = ln.APP.APPLE_MAPS;
ln.util.validateApp(options.app);
ln.util.validateTransportMode(options.transportMode);
// Process options
destination = ln.util.extractCoordsFromLocationString(destination);
if(typeof(destination) === "object"){
if(typeof destination.length === "undefined") throw "destination must be a string or an array";
destination = destination.join(",");
options.destType = "coords";
}else{
options.destType = "name";
}
options.start = ln.util.extractCoordsFromLocationString(options.start);
if(!options.start){
options.startType = "none";
}else if(typeof(options.start) === "object"){
if(typeof options.start.length === "undefined") throw "start must be a string or an array";
options.start = options.start.join(",");
options.startType = "coords";
}else{
options.startType = "name";
}
return RNLaunchNavigator.navigate({
appName: options.app,
destType: options.destType,
dest: destination,
destName: options.destinationName,
startType: options.startType,
start: options.start,
startName: options.startName,
transportMode: options.transportMode,
launchMode: options.launchMode,
extras: options.extras,
enableGeocoding: options.enableGeocoding
});
};
/**
* Returns the list of transport modes supported by an app on a given platform.
* @param {string} app - specified as a constant in `LaunchNavigator.APP`. e.g. `LaunchNavigator.APP.GOOGLE_MAPS`.
* @param {string} platform - specified as a constant in `LaunchNavigator.PLATFORM`. e.g. `LaunchNavigator.PLATFORM.IOS`.
* @return {string[]} - list of transports modes as constants in `LaunchNavigator.TRANSPORT_MODE`.
* If app/platform combination doesn't support specification of transport mode, the list will be empty;
*/
ln.getTransportModes = function(app, platform){
if(ln.supportsTransportMode(app, platform)){
return ln.TRANSPORT_MODES[platform][app];
}
return [];
};
/**
* Indicates if an app on a given platform supports specification of a custom nickname for destination location.
* @param {string} app - specified as a constant in `LaunchNavigator.APP`. e.g. `LaunchNavigator.APP.APPLE_MAPS`.
* @param {string} platform - specified as a constant in `LaunchNavigator.PLATFORM`. e.g. `LaunchNavigator.PLATFORM.IOS`.
* @param {string} launchMode (optional) - only applies to Apple Maps on iOS. Specified as a constant in `LaunchNavigator.LAUNCH_MODE`. e.g. `LaunchNavigator.LAUNCH_MODE.MAPKIT`.
* @return {boolean} - true if app/platform combination supports specification of destination location.
*/
ln.supportsDestName = function(app, platform, launchMode){
ln.util.validateApp(app);
ln.util.validatePlatform(platform);
let result;
if(launchMode && platform === ln.PLATFORM.IOS && app === ln.APP.APPLE_MAPS){
ln.util.validateLaunchMode(launchMode);
result = launchMode === ln.LAUNCH_MODE.MAPKIT;
}else{
result = ln._supportsDestName(app, platform);
}
return result;
};
/**
* Indicates if an app on a given platform supports specification of a custom nickname for start location.
* @param {string} app - specified as a constant in `LaunchNavigator.APP`. e.g. `LaunchNavigator.APP.APPLE_MAPS`.
* @param {string} platform - specified as a constant in `LaunchNavigator.PLATFORM`. e.g.
* @param {string} launchMode (optional) - only applies to Apple Maps on iOS. Specified as a constant in `LaunchNavigator.LAUNCH_MODE`. e.g. `LaunchNavigator.LAUNCH_MODE.MAPKIT`.
* @return {boolean} - true if app/platform combination supports specification of start location.
*/
ln.supportsStartName = function(app, platform, launchMode){
ln.util.validateApp(app);
ln.util.validatePlatform(platform);
let result;
if(launchMode && platform === ln.PLATFORM.IOS && app === ln.APP.APPLE_MAPS){
ln.util.validateLaunchMode(launchMode);
result = launchMode === ln.LAUNCH_MODE.MAPKIT;
}else{
result = ln._supportsStartName(app, platform);
}
return result;
};
// Map directly to public API
ln.supportsStart = function() {
return ln._supportsStart.apply(this, arguments);
};
ln.supportsTransportMode = function() {
return ln._supportsTransportMode.apply(this, arguments);
};
/************
* Export
************/
module.exports = ln;