source: extensions/edit_gmaps/admin/js/JScript.js @ 9377

Last change on this file since 9377 was 9377, checked in by cljosse, 13 years ago

[edit_gmaps] adding support to the altitude.

File size: 12.7 KB
Line 
1
2/* Déclaration des variables  */
3var geocoder;
4var map;
5var marker;
6var markers = new Array;
7var detect = navigator.userAgent.toLowerCase();
8
9function onLoad() {
10    var reg = new RegExp('/', "g");
11   
12    if (coordinates.ADRESSE)
13        coordinates.ADRESSE = coordinates.ADRESSE.replace("/", ", ");
14
15          document.getElementById("match").value = coordinates.ADRESSE;
16                geocoder = new google.maps.Geocoder();
17                    var mapElement = document.getElementById("map");
18                    if (coordinates.LAT <= 90 && coordinates.LAT != " " && coordinates.LAT != "" ) {
19
20                        } else {
21                            coordinates.LAT=49 ;coordinates.LON = 3;
22                            coordinates.ZOOM = coordinates.ZOOM | 18;
23                            geocoder.geocode({ 'address': coordinates.ADRESSE }, function (results, status) {
24                                /* Si l'adresse a pu être géolocalisée */
25                                if (status == google.maps.GeocoderStatus.OK) {
26                                    /* Récupération de sa latitude et de sa longitude */
27                                    coordinates.LAT = results[0].geometry.location.lat();
28                                    coordinates.LON = results[0].geometry.location.lng();
29                                    LatLng = new google.maps.LatLng(coordinates.LAT, coordinates.LON);
30                                    updateMarkerPosition(LatLng);
31                                    geocodePosition(LatLng);
32                                }
33                            });
34
35                           
36                        }
37  zoom = coordinates.ZOOM;
38   LatLng = new google.maps.LatLng(coordinates.LAT, coordinates.LON);
39 
40
41            /*
42             MapTypeId.ROADMAP affiche un plan (de type carte routière).
43             MapTypeId.SATELLITE affiche une vue satellite.
44             MapTypeId.HYBRID affiche une vue mixte entre les deux précédentes.
45             MapTypeId.TERRAIN affiche une vue du relief.
46            */
47 var mapOpts = {     
48                    center:LatLng,
49                    zoom:zoom,
50                            mapTypeId: google.maps.MapTypeId.SATELLITE 
51                    };
52       
53                    map = new google.maps.Map( mapElement, mapOpts );
54
55             
56
57
58         if (coordinates) 
59                   marker = new google.maps.Marker( { 
60                            position: map.getCenter(),
61                            map: map,
62                    draggable: true 
63                            });
64
65
66                // Add dragging event listeners.   
67                google.maps.event.addListener(marker,
68        'dragstart',
69        function () {
70            updateMarkerAddress('Dragging...');
71        });
72
73        google.maps.event.addListener(marker,
74            'drag',
75            function () {
76                updateMarkerStatus('Dragging...');
77                updateMarkerPosition(marker.getPosition());
78            }
79            );
80
81            google.maps.event.addListener(marker,
82                                            'dragend',
83                                            function () {
84
85                                             
86                                     
87                                              // getElevation({ latLng: marker.getPosition() });
88                                                geocodePosition(marker.getPosition());
89                                                updateMarkerStatus('Drag ended');
90                                                getElevation({ 'latLng': marker.getPosition() });
91                                                document.Envoie.alt.value = coordinates.ALT;
92
93                                            }
94        );
95
96       // styleIcon = new StyledIcon(StyledIconTypes.BUBBLE, { color: "#cccccc", text: "Drag Me!" });
97       // var styleMaker1 = new StyledMarker({ styleIcon: styleIcon, position: new google.maps.LatLng(37.383477473067, -121.880502070713), map: map, draggable: true });
98
99       // google.maps.event.addDomListener(styleMaker1, "dragend", getElevation);
100                                            // Update current position info.   
101
102                       updateMarkerPosition(LatLng);
103                       geocodePosition(LatLng);
104                       getElevation({ 'latLng': marker.getPosition() });
105                       document.Envoie.alt.value = coordinates.ALT;
106                  return false;
107   
108
109 
110
111
112 }
113
114       function updateMarkerStatus(str) {   
115                document.getElementById('markerStatus').innerHTML = str; 
116        }   
117       
118        function updateMarkerPosition(latLng) {   
119                document.Envoie.lat.value=latLng.lat() ;
120                document.Envoie.lon.value = latLng.lng();
121                dms_frac = dec2dms(latLng.lng());
122                if (dms_frac.Degre < 0)
123                    document.Envoie.lonRef.value = "W";
124                else
125                    document.Envoie.lonRef.value = "E";
126                document.Envoie.lonDMS.value = Math.abs(dms_frac.Degre) +"/1" + " " + dms_frac.Minute +"/1" + " " +    dms_frac.Numerator + "/" + dms_frac.Denominator;
127                dms_frac = dec2dms(latLng.lat());
128                if (dms_frac.Degre < 0)
129                    document.Envoie.latRef.value = "S";
130                else
131                    document.Envoie.latRef.value = "N";
132                document.Envoie.latDMS.value = Math.abs(dms_frac.Degre) + "/1" + " " + dms_frac.Minute + "/1" + " " + dms_frac.Numerator + "/" + dms_frac.Denominator;
133                update_datas();
134            }
135
136
137                function update_datas() {
138                n= jQuery(document.EditJPEG);
139
140                jQuery.each(n[0], function (i) {
141
142
143                    n00 = this;
144                    n0 = n00.id;
145                    n1 = n00.name;
146                    n2 = jQuery(n00).val();
147                    try {
148                        if (n0 == "alt") {
149                            n3 = dec2frac(document.Envoie[n0].value);
150                            n3 = n3.Numerator + "/" + n3.Denominator;
151                            jQuery(this).val(n3);
152                        }
153                        else {
154                            n3 = document.Envoie[n0].value;
155                            jQuery(this).val(n3);
156                        }
157                    }
158                    catch (e) {
159                        n3 = n0;
160                    }
161
162
163
164                }
165                )
166
167                   
168
169                }
170           
171           function updateMarkerAddress(str) {
172               document.getElementById('markerAdress').innerHTML = str;
173       
174               jQuery("#match").val(str);
175            }
176
177            function getElevation(event) {
178                var elevator = new google.maps.ElevationService();
179                var locations = [];
180
181                // Retrieve the clicked location and push it on the array
182                var clickedLocation = event.latLng;
183                locations.push(clickedLocation);
184
185                // Create a LocationElevationRequest object using the array's one value
186                var positionalRequest = {
187                    'locations': locations
188                }
189
190                // Initiate the location request
191                if (elevator) {
192                    elevator.getElevationForLocations(positionalRequest, function (results, status) {
193                        if (status == google.maps.ElevationStatus.OK) {
194
195                            // Retrieve the first result
196                            if (results[0]) {
197                                coordinates.ALT = results[0].elevation;
198                                document.Envoie.alt.value = coordinates.ALT;
199                                update_datas();
200                                   //styleIcon.set("text", "Elevation: " + results[0].elevation + "m");
201                            } else {
202                               // alert("No results found");
203                            }
204                        } else {
205                            alert("Elevation service failed due to: " + status);
206                        }
207                    });
208                }
209            }
210
211
212
213            function geocodePosition(pos) {
214                     geocoder.geocode({ 'latLng': pos },
215                    function (results, status) {
216                   /* Si le géocodage inversé a réussi */
217                   if (status == google.maps.GeocoderStatus.OK) {
218                       updateMarkerAddress(results[0].formatted_address);
219                   } else {
220                       updateMarkerAddress('Cannot determine address at this location.');
221                   }
222               });
223        }
224
225 
226        /* Fonction de géocodage déclenchée en cliquant surle bouton "Geocoder"  */
227        function codeAddress() {
228            /* Récupération de la valeur de l'adresse saisie */
229         
230            /* Appel au service de geocodage avec l'adresse en paramètre */
231            if (typeof map == "undefined") onLoad();
232
233            var address = document.getElementById("match").value;
234
235  geocoder.geocode({ 'address': address }, function (results, status) {
236      /* Si l'adresse a pu être géolocalisée */
237      if (status == google.maps.GeocoderStatus.OK) {
238          /* Récupération de sa latitude et de sa longitude */
239          coordinates.LAT = results[0].geometry.location.lat();
240          coordinates.LON = results[0].geometry.location.lng();
241          onLoad();
242         
243           
244
245      } else {
246          alert("Cannot determine location at this adress.: " + status);
247      }
248  });
249}
250
251//=======================================================
252function On_Click(a, b, c) {
253    document.Envoie.lat.value = b;
254    document.Envoie.lon.value = c;
255
256    coordinates = {LAT:a,
257     LON : b} ;
258    LatLng = new google.maps.LatLng(coordinates.LAT, coordinates.LON);
259    updateMarkerPosition(LatLng);
260
261
262}
263
264jQuery(window).load(
265
266      function () {
267//Récupérer tous les inputs radios appartenant à un même groupe
268 /*         a = jQuery('input[type=hidden][name=Selectthumbnail]');
269          b = a.length;
270          c = jQuery('input[type=hidden]:checked').length;
271          d = jQuery('input[type=hidden][name="Selectthumbnail"]').attr('value');
272*/
273
274        //  onLoad();
275      }); 
276
277jQuery(document).ready(function () {
278
279
280    jQuery('.cluetip').cluetip({
281        width: 300, height: 100,
282        splitTitle: '|'
283    }
284  );
285  jQuery("#search").click(function (e) {
286
287        coordinates = { ADRESSE: jQuery("#match").val(),
288                        LAT:0,LON:0,ZOOM: 18 ,ALT:0
289                       };
290        codeAddress();
291        return false;
292    });
293
294    jQuery("#match").keydown(function (e) {
295        // e.target         pour FireFox
296        // event.srcElement pour IE
297        var Obj = e ? e.target : event.srcElement;
298        if (Obj.tagName != "INPUT") return false;
299        var Code = e.keyCode || e.which;
300        if (Code != 13) return true;
301        coordinates = { ADRESSE: jQuery("#match").val(),
302            LAT: 0, LON: 0, ZOOM: 18, ALT: 0
303                       };
304        codeAddress();
305        return false;
306    });
307
308
309});
310
311
312
313
314  // DECIMAL TO FRACTION
315  //
316  function dec2frac( valeur_decimal ) {
317     decimal = valeur_decimal.toString() ;
318      num = '';
319      den = 1;
320      dec = false;
321
322      // find least reduced fractional form of number
323      for( i = 0, ix =   decimal.length  ; i < ix; i++ )
324      {
325      // build the denominator as we 'shift' the decimal to the right
326      if( dec ) den *= 10;
327
328      // find the decimal place/ build the numberator
329      if( decimal.charAt(i) == '.' ) dec = true;
330      else num += decimal.charAt(i);
331      }
332      num = parseInt(num);
333 
334      // whole number, just return it
335      if( den == 1 ) return num;
336
337      num2 = num;
338      den2 = den;
339      rem  = 1;
340      // Euclid's Algorithm (to find the gcd)
341      while( num2 % den2 ) {
342      rem = num2 % den2;
343      num2 = den2;
344      den2 = rem;
345      }
346      if( den2 != den ) rem = den2;
347      ret1 = { Numerator: (num / rem), Denominator: (den / rem) };
348      // new Array()
349    // ret1->Numerator =(num / rem ) ;
350    // ret1->Denominator=(den / rem);
351
352      // now $rem holds the gcd of the numerator and denominator of our fraction
353      return ret1;
354     
355  }
356
357function dec2dms(l) {
358    sign = 1;
359    if (l < 0) { sign = -1; }
360    l = Math.abs(Math.round(l * 1000000.))
361    d = Math.floor(l / 1000000) * sign;
362    m = Math.floor(((l / 1000000) - Math.floor(l / 1000000)) * 60);
363    s = Math.floor(((((l / 1000000) - Math.floor(l / 1000000)) * 60) - Math.floor(((l / 1000000) - Math.floor(l / 1000000)) * 60)) * 100000) * 60 / 100000;
364    frac_s = dec2frac(s);
365    s2 = (frac_s.Numerator / frac_s.Denominator);
366    return { dms: d + "° " + m + "' " + s + "''",
367        Degre:d, Minute:m,Sec:s,
368        Numerator: frac_s.Numerator,
369        Denominator:frac_s.Denominator};
370}
371
Note: See TracBrowser for help on using the repository browser.