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

Last change on this file since 9412 was 9412, checked in by cljosse, 13 years ago
File size: 12.9 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                                               
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                     
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 (document.Envoie.lon.value < 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 (document.Envoie.lat.value < 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                                altDMS = dec2frac(coordinates.ALT);
200                                document.Envoie.altRef.value = "0";
201                                document.Envoie.altDMS.value =  altDMS.Numerator + "/" +  altDMS.Denominator;
202                                //styleIcon.set("text", "Elevation: " + results[0].elevation + "m");
203                            } else {
204                                // alert("No results found");
205                            }
206                        } else {
207                            alert("Elevation service failed due to: " + status);
208                        }
209                    });
210                }
211            }
212
213
214
215            function geocodePosition(pos) {
216                     geocoder.geocode({ 'latLng': pos },
217                    function (results, status) {
218                   /* Si le géocodage inversé a réussi */
219                   if (status == google.maps.GeocoderStatus.OK) {
220                       updateMarkerAddress(results[0].formatted_address);
221                   } else {
222                       updateMarkerAddress('Cannot determine address at this location.');
223                   }
224               });
225        }
226
227 
228        /* Fonction de géocodage déclenchée en cliquant surle bouton "Geocoder"  */
229        function codeAddress() {
230            /* Récupération de la valeur de l'adresse saisie */
231         
232            /* Appel au service de geocodage avec l'adresse en paramètre */
233            if (typeof map == "undefined") onLoad();
234
235            var address = document.getElementById("match").value;
236
237  geocoder.geocode({ 'address': address }, function (results, status) {
238      /* Si l'adresse a pu être géolocalisée */
239      if (status == google.maps.GeocoderStatus.OK) {
240          /* Récupération de sa latitude et de sa longitude */
241          coordinates.LAT = results[0].geometry.location.lat();
242          coordinates.LON = results[0].geometry.location.lng();
243          onLoad();
244         
245           
246
247      } else {
248          alert("Cannot determine location at this adress.: " + status);
249      }
250  });
251}
252
253//=======================================================
254function On_Click(a, b, c) {
255    document.Envoie.lat.value = b;
256    document.Envoie.lon.value = c;
257
258    coordinates = {LAT:a,
259     LON : b} ;
260    LatLng = new google.maps.LatLng(coordinates.LAT, coordinates.LON);
261    updateMarkerPosition(LatLng);
262
263
264}
265
266jQuery(window).load(
267
268      function () {
269//Récupérer tous les inputs radios appartenant à un même groupe
270 /*         a = jQuery('input[type=hidden][name=Selectthumbnail]');
271          b = a.length;
272          c = jQuery('input[type=hidden]:checked').length;
273          d = jQuery('input[type=hidden][name="Selectthumbnail"]').attr('value');
274*/
275
276        //  onLoad();
277      }); 
278
279jQuery(document).ready(function () {
280
281
282    jQuery('.cluetip').cluetip({
283        width: 300, height: 100,
284        splitTitle: '|'
285    }
286  );
287  jQuery("#search").click(function (e) {
288
289        coordinates = { ADRESSE: jQuery("#match").val(),
290                        LAT:0,LON:0,ZOOM: 18 ,ALT:0
291                       };
292        codeAddress();
293        return false;
294    });
295
296    jQuery("#match").keydown(function (e) {
297        // e.target         pour FireFox
298        // event.srcElement pour IE
299        var Obj = e ? e.target : event.srcElement;
300        if (Obj.tagName != "INPUT") return false;
301        var Code = e.keyCode || e.which;
302        if (Code != 13) return true;
303        coordinates = { ADRESSE: jQuery("#match").val(),
304            LAT: 0, LON: 0, ZOOM: 18, ALT: 0
305                       };
306        codeAddress();
307        return false;
308    });
309
310
311});
312
313
314
315
316  // DECIMAL TO FRACTION
317  //
318  function dec2frac( valeur_decimal ) {
319     decimal = valeur_decimal.toString() ;
320      num = '';
321      den = 1;
322      dec = false;
323
324      // find least reduced fractional form of number
325      for( i = 0, ix =   decimal.length  ; i < ix; i++ )
326      {
327      // build the denominator as we 'shift' the decimal to the right
328      if( dec ) den *= 10;
329
330      // find the decimal place/ build the numberator
331      if( decimal.charAt(i) == '.' ) dec = true;
332      else num += decimal.charAt(i);
333      }
334      num = parseInt(num);
335 
336      // whole number, just return it
337      if( den == 1 ) return num;
338
339      num2 = num;
340      den2 = den;
341      rem  = 1;
342      // Euclid's Algorithm (to find the gcd)
343      while( num2 % den2 ) {
344      rem = num2 % den2;
345      num2 = den2;
346      den2 = rem;
347      }
348      if( den2 != den ) rem = den2;
349      ret1 = { Numerator: (num / rem), Denominator: (den / rem) };
350      // new Array()
351    // ret1->Numerator =(num / rem ) ;
352    // ret1->Denominator=(den / rem);
353
354      // now $rem holds the gcd of the numerator and denominator of our fraction
355      return ret1;
356     
357  }
358
359function dec2dms(l) {
360    sign = 1;
361    if (l < 0) { sign = -1; }
362    l = Math.abs(Math.round(l * 1000000.))
363    d = Math.floor(l / 1000000) * sign;
364    m = Math.floor(((l / 1000000) - Math.floor(l / 1000000)) * 60);
365    s = Math.floor(((((l / 1000000) - Math.floor(l / 1000000)) * 60) - Math.floor(((l / 1000000) - Math.floor(l / 1000000)) * 60)) * 100000) * 60 / 100000;
366    frac_s = dec2frac(s);
367    s2 = (frac_s.Numerator / frac_s.Denominator);
368    return { dms: d + "° " + m + "' " + s + "''",
369        Degre:d, Minute:m,Sec:s,
370        Numerator: frac_s.Numerator,
371        Denominator:frac_s.Denominator};
372}
373
Note: See TracBrowser for help on using the repository browser.