· 6 years ago · Jan 11, 2020, 03:52 PM
1/* Dieses Skript wird ausgeführt, wenn der Browser index.html lädt. */
2
3// Befehle werden sequenziell abgearbeitet ...
4
5/**
6 * "console.log" schreibt auf die Konsole des Browsers
7 * Das Konsolenfenster muss im Browser explizit geöffnet werden.
8 */
9console.log("The script is going to start...");
10
11// Es folgen einige Deklarationen, die aber noch nicht ausgeführt werden ...
12
13// Hier wird die verwendete API für Geolocations gewählt
14// Die folgende Deklaration ist ein 'Mockup', das immer funktioniert und eine fixe Position liefert.
15GEOLOCATIONAPI = {
16 getCurrentPosition: function(onsuccess) {
17 onsuccess({
18 "coords": {
19 "latitude": 49.013790,
20 "longitude": 8.390071,
21 "altitude": null,
22 "accuracy": 39,
23 "altitudeAccuracy": null,
24 "heading": null,
25 "speed": null
26 },
27 "timestamp": 1540282332239
28 });
29 }
30};
31
32// Die echte API ist diese.
33// Falls es damit Probleme gibt, kommentieren Sie die Zeile aus.
34GEOLOCATIONAPI = navigator.geolocation;
35
36/**
37 * GeoTagApp Locator Modul
38 */
39var gtaLocator = (function GtaLocator(geoLocationApi) {
40
41 // Private Member
42
43 /**
44 * Funktion spricht Geolocation API an.
45 * Bei Erfolg Callback 'onsuccess' mit Position.
46 * Bei Fehler Callback 'onerror' mit Meldung.
47 * Callback Funktionen als Parameter übergeben.
48 */
49 var tryLocate = function(onsuccess, onerror) {
50 if (geoLocationApi) {
51 geoLocationApi.getCurrentPosition(onsuccess, function(error) {
52 var msg;
53 switch (error.code) {
54 case error.PERMISSION_DENIED:
55 msg = "User denied the request for Geolocation.";
56 break;
57 case error.POSITION_UNAVAILABLE:
58 msg = "Location information is unavailable.";
59 break;
60 case error.TIMEOUT:
61 msg = "The request to get user location timed out.";
62 break;
63 case error.UNKNOWN_ERROR:
64 msg = "An unknown error occurred.";
65 break;
66 }
67 onerror(msg);
68 });
69 } else {
70 onerror("Geolocation is not supported by this browser.");
71 }
72 };
73
74 // Auslesen Breitengrad aus der Position
75 var getLatitude = function(position) {
76 return position.coords.latitude;
77 };
78
79 // Auslesen Längengrad aus Position
80 var getLongitude = function(position) {
81 return position.coords.longitude;
82 };
83
84 // Hier Google Maps API Key eintragen
85 var apiKey = "nLVatE1lz7t1UPxGVLvBoydGeDyFW68T";
86
87 /**
88 * Funktion erzeugt eine URL, die auf die Karte verweist.
89 * Falls die Karte geladen werden soll, muss oben ein API Key angegeben
90 * sein.
91 *
92 * lat, lon : aktuelle Koordinaten (hier zentriert die Karte)
93 * tags : Array mit Geotag Objekten, das auch leer bleiben kann
94 * zoom: Zoomfaktor der Karte
95 */
96 var getLocationMapSrc = function(lat, lon, tags, zoom) {
97 zoom = typeof zoom !== 'undefined' ? zoom : 10;
98
99 if (apiKey === "YOUR API KEY HERE") {
100 console.log("No API key provided.");
101 return "images/mapview.jpg";
102 }
103
104 var tagList = "&pois=You," + lat + "," + lon;
105 if (tags !== undefined) tags.forEach(function(tag) {
106 tagList += "|" + tag.name + "," + tag.latitude + "," + tag.longitude;
107 });
108
109 var urlString = "https://www.mapquestapi.com/staticmap/v4/getmap?key=" +
110 apiKey + "&size=600,400&zoom=" + zoom + "¢er=" + lat + "," + lon + "&" + tagList;
111
112 console.log("Generated Maps Url: " + urlString);
113 return urlString;
114 };
115
116 return { // Start öffentlicher Teil des Moduls ...
117
118 // Public Member
119
120 readme: "Dieses Objekt enthält 'öffentliche' Teile des Moduls.",
121
122 updateLocation: function() {
123
124if(
125 document.getElementById('latitude').value.length === 0 &&
126 document.getElementById('longitude').value.length === 0
127){
128 tryLocate(function(position){
129 document.getElementById('latitude').value = getLatitude(position);
130 document.getElementById('longitude').value = getLongitude(position);
131 document.getElementById('safeLatitude').value = getLatitude(position);;
132 document.getElementById('safeLongitude').value = getLongitude(position);;
133 var jsonString = document.getElementById('map').dataset.tags;
134 var url = getLocationMapSrc(getLatitude(position),getLongitude(position),JSON.parse(jsonString),17);
135 document.getElementById('result-img').src = url;
136
137 },function (message){
138 alert(message);
139 },)
140}
141else{
142 var jsonString = document.getElementById('map').dataset.tags;
143 var url = getLocationMapSrc(document.getElementById('latitude').value,document.getElementById('longitude').value,JSON.parse(jsonString),17);
144 document.getElementById('result-img').src = url;
145}
146
147
148
149
150
151
152
153 }
154
155 }; // ... Ende öffentlicher Teil
156})(GEOLOCATIONAPI);
157
158/**
159 * $(function(){...}) wartet, bis die Seite komplett geladen wurde. Dann wird die
160 * angegebene Funktion aufgerufen. An dieser Stelle beginnt die eigentliche Arbeit
161 * des Skripts.
162 */
163$(function() {
164 alert("Please change the script 'geotagging.js'");
165
166
167 gtaLocator.updateLocation();
168
169
170});