-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLocation4TravelBlog.ts
137 lines (121 loc) · 4.55 KB
/
Location4TravelBlog.ts
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
import { LOG_LEVEL, log, logObject, logJSON } from 'scripts/utils/debug-utils';
import { url_places_search, url_place_details } from 'scripts/utils/google-maps-utils';
import { GPSPoint } from 'scripts/utils/gps';
const logLevel: number = LOG_LEVEL.DEBUG;
const VERSION = '0.1';
const google_api_key = `__google_api_key__`;
const radius_nerby = 100;
const tb_file = 'map-vietnam-2024.yml'
const tb_text = (t: IPOIText) => `- title: ${t.title}
type: ${t.type}
location:
latitude: ${t.gps.latitude}
longitude: ${t.gps.longitude}
`;
export interface IPOIText {
title: string;
type: string;
gps: GPSPoint;
}
log(LOG_LEVEL.INFO, logLevel, 'start script (' + module.filename + '/' + VERSION + '): ' + args.widgetParameter);
const table = new UITable();
function showPlaces(){
const headline = new UITableRow();
headline.isHeader = true;
let cellN = headline.addText('Name');
cellN.widthWeight = 150;
let cellI = headline.addText('Icon');
cellI.widthWeight = 50;
table.addRow(headline)
dataPlacesSearch.results.forEach(r => {
// logJSON(LOG_LEVEL.DEBUG, logLevel, '', r);
let tr = new UITableRow();
tr.dismissOnSelect = false;
tr.onSelect = () => {
loadDetails(r);
};
cellN = tr.addText(r.name, r.types.join(' '));
cellN.widthWeight = 150;
cellI = tr.addImageAtURL(r.icon)
cellI.widthWeight = 50;
table.addRow(tr);
});
table.reload();
}
async function loadDetails (poi){
logJSON(LOG_LEVEL.INFO, logLevel, 'loadDetails: ', poi);
let poi_gps : GPSPoint = {latitude: poi.geometry.location.lat, longitude: poi.geometry.location.lng};
const request = new Request(url_place_details({google_api_key: google_api_key, place_id: poi.place_id}))
const dataPlace = await request.loadJSON().then(r => {return r}, err => console.log('error in loadJSON: '+err));
logJSON(LOG_LEVEL.DEBUG, logLevel, 'place_details: ', dataPlace);
table.removeAllRows();
let headline = new UITableRow();
headline.isHeader = true;
headline.addText(dataPlace.result.name);
table.addRow(headline);
let types = new UITableRow();
types.addText(poi.types.join(' '));
table.addRow(types);
let address = new UITableRow();
address.addText(dataPlace.result.formatted_address);
table.addRow(address);
let websiteRow = new UITableRow();
let websiteButton = websiteRow.addButton(dataPlace.result.website);
websiteButton.onTap = () => {
Safari.openInApp(dataPlace.result.website);
};
table.addRow(websiteRow);
let mapRow = new UITableRow();
let mapButton = mapRow.addButton('Google Maps');
mapButton.onTap = () => {
Safari.openInApp(dataPlace.result.url);
};
table.addRow(mapRow);
table.addRow(new UITableRow());
let okRow = new UITableRow();
table.addRow(okRow);
let okButton = okRow.addButton('Append');
okButton.dismissOnTap = true;
okButton.onTap = () => {
log(LOG_LEVEL.INFO, logLevel, 'append poi to '+ tb_file);
let callback = new CallbackURL('textastic://x-callback-url/append');
callback.addParameter('location', 'iCloud');
callback.addParameter('name', tb_file);
callback.addParameter('text', tb_text({title: dataPlace.result.name, type: poiType[alertR], gps: poi_gps }) )
callback.open();
};
let selAgainRow = new UITableRow();
table.addRow(selAgainRow);
let selAgainButton = okRow.addButton('Select Again');
selAgainButton.onTap = () => {
table.removeAllRows();
showPlaces();
};
let cancelRow = new UITableRow();
table.addRow(cancelRow);
table.reload();
return;
}
/*
let location = await Location.current();
log(LOG_LEVEL.DEBUG, logLevel, 'location: '+ location)
logObject(LOG_LEVEL.DEBUG, logLevel, location)
*/
const alert = new Alert();
const poiType = ['POI', 'POI-ED', 'POI-PT', 'POI-Divers', 'POI-Wine'];
alert.title = 'POI Art';
alert.message = 'Um was für einen POI handelt es sich?';
poiType.forEach(e => {
alert.addAction(e)
});
alert.addCancelAction('Cancel')
let alertR = await alert.present();
const request = new Request(url_places_search({google_api_key: google_api_key, radius: radius_nerby}))
const dataPlacesSearch = await request.loadJSON().then(r => {return r}, err => console.log('error in loadJSON: '+err));
logJSON(LOG_LEVEL.DEBUG, logLevel, 'places_search: ', dataPlacesSearch);
if(alertR != -1){
log(LOG_LEVEL.INFO, logLevel, 'selected ' + alertR + ' value: ' + poiType[alertR])
showPlaces();
table.present(true);
}
Script.complete();