-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMaps.js
More file actions
163 lines (138 loc) · 4.75 KB
/
Maps.js
File metadata and controls
163 lines (138 loc) · 4.75 KB
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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// always-run-in-app: true; icon-color: blue;
// icon-glyph: location-arrow; share-sheet-inputs: url;
/*
* Copyright 2018 Paul Sidnell
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// V1.0.0
// https://github.com/psidnell/Scriptable
const ROW_HEIGHT = 50;
function decodeUrlParams(url) {
let lines = url.split(/[\?\&]/);
let dict = {};
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
let attribValue = line.split('=');
if (attribValue.length == 2) {
dict[attribValue[0]] = decodeURI(attribValue[1]);
}
}
return dict;
}
function processAppleUrl(appleMapsUrl, dict) {
let address = '';
if (dict['address']) {
let lines = dict['address'].split(',');
for (let i = 0; i < lines.length; i++) {
lines[i] = lines[i].trim();
}
address = lines.join(',\n');
}
let name = address.length > 0 ? address.split('\n')[0]: 'Pin';
let latLong = dict['ll'];
return {
ll: latLong,
address: address,
appleMapsUrl: 'https://maps.apple.com/?ll=' + latLong + '&q=' + encodeURIComponent(name),
googleMapsUrl: 'https://www.google.com/maps/search/?api=1&query=' + latLong,
wazeUrl: 'https://waze.com/ul?ll=' + latLong
};
}
function addRow(uiTable, text, isHeader) {
let lines = text.split('\n');
for (let i = 0; i < lines.length; i++) {
let line = lines[i];
let uiTableRow = new UITableRow();
let cell = uiTableRow.addText(isHeader ? line.toUpperCase() : line);
cell.leftAligned();
cell.widthWeight = 100;
uiTableRow.height = ROW_HEIGHT;
uiTableRow.cellSpacing = 10;
uiTableRow.dismissOnSelect = false;
uiTableRow.isHeader = isHeader;
uiTable.addRow(uiTableRow);
}
}
function addButton(uiTable, text, fn) {
let row = new UITableRow();
let cell = row.addButton(text);
cell.onTap = fn;
cell.widthWeight = 100;
row.height = ROW_HEIGHT;
row.cellSpacing = 10;
row.dismissOnSelect = false;
row.isHeader = false;
uiTable.addRow(row);
}
function display(info) {
let uiTable = new UITable();
let pasteData = [
info.address,
'[Apple Maps](' + info.appleMapsUrl + ')',
'[Google Maps](' + info.googleMapsUrl + ')',
'[Waze](' + info.wazeUrl + ')'
];
addRow(uiTable, "Location Details Extracted", true);
addRow(uiTable, pasteData.join('\n'), false);
addRow(uiTable, "Actions", true);
addButton(uiTable, 'Open in Apple Maps', () => {
Safari.open(info.appleMapsUrl);
});
addButton(uiTable, 'Open in Google Maps', () => {
Safari.open(info.googleMapsUrl);
});
addButton(uiTable, 'Open in Waze', () => {
Safari.open(info.wazeUrl);
});
addButton(uiTable, 'Share Formatted Location', () => {
ShareSheet.presentAndWait([pasteData.join('\n\n')]);
});
addButton(uiTable, 'Copy Formatted Location', () => {
Pasteboard.copyString(pasteData.join('\n\n'));
let alert = new Alert();
alert.title = 'Information in Paste Buffer';
alert.addCancelAction('OK');
alert.present();
});
QuickLook.present(uiTable);
}
if (args.urls && args.urls.length > 0) {
url = args.urls[0];
let dict = decodeUrlParams(url);
if (url.startsWith('https://maps.apple.com/')) {
info = processAppleUrl(url, dict);
display(info);
}
} else {
let alert = new Alert();
alert.title = 'Use Current Location?';
alert.message = 'Will take several seconds';
alert.addAction('Yes');
alert.addCancelAction('Cancel');
alert.presentAlert().then((opt) => {
if (opt == 0) {
console.log('Fetching location...');
let location = Location.current();
location.then((locData) => {
let ll = locData.latitude + ',' + locData.longitude;
let url = 'https://maps.apple.com/?sll=' + ll + '&t=m';
let dict = decodeUrlParams(url);
info = processAppleUrl(url, dict);
display(info);
});
}
});
}