-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetCoords.gs
101 lines (92 loc) · 3.28 KB
/
GetCoords.gs
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
function getCoordinatesForEmptyRows() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Final");
const data = sheet.getDataRange().getValues();
const apiKey = PropertiesService.getScriptProperties().getProperty('GOOGLEMAPSAPIKEY');
for (let i = 1; i < data.length; i++) {
const name = data[i][0];
const url = data[i][2];
const lat = data[i][3];
const lng = data[i][4];
if (!lat && !lng) {
try {
let result = parseUrlForCoordinates(url, apiKey);
if (!result && name) {
result = fetchCoordinatesByName(name, apiKey);
}
if (result) {
sheet.getRange(i + 1, 4).setValue(result.lat);
sheet.getRange(i + 1, 5).setValue(result.lng);
Logger.log(`Coords found for line ${i + 1}: ${result.lat}, ${result.lng}`);
} else {
Logger.log(`No coords for line ${i + 1}`);
}
} catch (e) {
Logger.log(`Error for line ${i + 1}: ${e.message}`);
continue;
}
}
}
}
function parseUrlForCoordinates(url, apiKey) {
if (url.includes('/search/')) {
const regex = /search\/([-+]?\d*\.\d+),([-+]?\d*\.\d+)/;
const match = url.match(regex);
if (match) {
return { lat: parseFloat(match[1]), lng: parseFloat(match[2]) };
}
} else if (url.includes('/place/') && url.includes('data=')) {
const dataString = url.split('data=')[1];
const decodedCoordinates = decodeDataParameter(dataString);
if (decodedCoordinates) {
return decodedCoordinates;
}
} else if (url.includes('/place/') && url.includes('1s')) {
const regex = /1s([^:]+:[^&]+)/;
const match = url.match(regex);
if (match) {
const placeId = match[1];
return fetchCoordinatesFromGoogle(placeId, apiKey);
}
}
return null;
}
function decodeDataParameter(dataString) {
try {
const segments = dataString.split('!');
for (let i = 1; i < segments.length; i++) {
const segment = segments[i];
if (segment[1] === 'x') {
const lat = parseInt(segments[i + 1].slice(2), 10) / 1e7;
const lng = parseInt(segments[i + 2].slice(2), 10) / 1e7;
return { lat, lng };
}
}
} catch (e) {
Logger.log(`Error while decoding data= : ${e.message}`);
}
return null;
}
function fetchCoordinatesFromGoogle(placeId, apiKey) {
const baseUrl = 'https://maps.googleapis.com/maps/api/place/details/json';
const url = `${baseUrl}?place_id=${placeId}&key=${apiKey}`;
const response = UrlFetchApp.fetch(url);
const json = JSON.parse(response.getContentText());
if (json.result && json.result.geometry && json.result.geometry.location) {
return json.result.geometry.location;
}
return null;
}
function fetchCoordinatesByName(name, apiKey) {
try {
const baseUrl = 'https://maps.googleapis.com/maps/api/geocode/json';
const url = `${baseUrl}?address=${encodeURIComponent(name)}&key=${apiKey}`;
const response = UrlFetchApp.fetch(url, { muteHttpExceptions: true });
const json = JSON.parse(response.getContentText());
if (json.results && json.results[0] && json.results[0].geometry && json.results[0].geometry.location) {
return json.results[0].geometry.location;
}
} catch (e) {
Logger.log(`Error while requesting the name: ${name} - ${e.message}`);
}
return null;
}