1 | |
---|
2 | /* Déclaration des variables */ |
---|
3 | var geocoder; |
---|
4 | var map; |
---|
5 | var infowindow = new google.maps.InfoWindow(); |
---|
6 | var marker; |
---|
7 | |
---|
8 | function onLoad() |
---|
9 | { |
---|
10 | geocoder = new google.maps.Geocoder(); |
---|
11 | var mapElement = document.getElementById("map"); |
---|
12 | if ( coordinates.LAT <= 90 ) { |
---|
13 | LatLng = new google.maps.LatLng(coordinates.LAT, coordinates.LON); |
---|
14 | zoom = 18 ; |
---|
15 | }else { |
---|
16 | LatLng = new google.maps.LatLng(49, 3); |
---|
17 | zoom = 3; |
---|
18 | } |
---|
19 | |
---|
20 | |
---|
21 | |
---|
22 | /* |
---|
23 | MapTypeId.ROADMAP affiche un plan (de type carte routière). |
---|
24 | MapTypeId.SATELLITE affiche une vue satellite. |
---|
25 | MapTypeId.HYBRID affiche une vue mixte entre les deux précédentes. |
---|
26 | MapTypeId.TERRAIN affiche une vue du relief. |
---|
27 | */ |
---|
28 | var mapOpts = { |
---|
29 | center:LatLng, |
---|
30 | zoom:zoom, |
---|
31 | mapTypeId: google.maps.MapTypeId.SATELLITE |
---|
32 | }; |
---|
33 | |
---|
34 | var map = new google.maps.Map( mapElement, mapOpts ); |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | |
---|
39 | if (coordinates) |
---|
40 | var marker = new google.maps.Marker( { |
---|
41 | position: map.getCenter(), |
---|
42 | map: map, |
---|
43 | draggable: true |
---|
44 | }); |
---|
45 | |
---|
46 | |
---|
47 | // Add dragging event listeners. |
---|
48 | google.maps.event.addListener(marker, |
---|
49 | 'dragstart', |
---|
50 | function () { |
---|
51 | updateMarkerAddress('Dragging...'); |
---|
52 | }); |
---|
53 | |
---|
54 | google.maps.event.addListener(marker, |
---|
55 | 'drag', |
---|
56 | function () { |
---|
57 | updateMarkerStatus('Dragging...'); |
---|
58 | updateMarkerPosition(marker.getPosition()); |
---|
59 | } |
---|
60 | ); |
---|
61 | |
---|
62 | google.maps.event.addListener(marker, |
---|
63 | 'dragend', |
---|
64 | function () { |
---|
65 | updateMarkerStatus('Drag ended'); |
---|
66 | geocodePosition(marker.getPosition()); |
---|
67 | |
---|
68 | } |
---|
69 | ); |
---|
70 | |
---|
71 | |
---|
72 | // Update current position info. |
---|
73 | updateMarkerPosition(LatLng); |
---|
74 | geocodePosition(LatLng); |
---|
75 | |
---|
76 | return false; |
---|
77 | |
---|
78 | |
---|
79 | function blowupUrl(theUrl) { |
---|
80 | var win = new Window({ |
---|
81 | title: document.title, |
---|
82 | url: theUrl, |
---|
83 | classname: "alphacube", |
---|
84 | left: 0, top: 0, |
---|
85 | width: "640", height: "480", |
---|
86 | minimizable: false, |
---|
87 | maximizable: false, |
---|
88 | draggable: false, |
---|
89 | resizable: true, |
---|
90 | destroyOnClose: true, |
---|
91 | hideEffect: Element.hide, |
---|
92 | showEffectOptions: { duration: 1.5 } |
---|
93 | }) |
---|
94 | win.setConstraint(true, { top: 20, left: 20, bottom: 30, right: 20 }); |
---|
95 | win.maximize(); |
---|
96 | win.showCenter(true, 20, 20); |
---|
97 | |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | |
---|
102 | |
---|
103 | |
---|
104 | infowindow = new google.maps.InfoWindow(); |
---|
105 | |
---|
106 | function geocodePosition(pos) { |
---|
107 | m = typeof geocoder; |
---|
108 | if (m == "undefined" ) return; |
---|
109 | geocoder.geocode({ |
---|
110 | 'latLng': pos }, |
---|
111 | function(responses) { |
---|
112 | if (responses && responses.length > 0) { |
---|
113 | updateMarkerAddress(responses[0].formatted_address); |
---|
114 | } else { |
---|
115 | updateMarkerAddress('Cannot determine address at this location.'); |
---|
116 | } |
---|
117 | }); |
---|
118 | } |
---|
119 | |
---|
120 | function updateMarkerStatus(str) { |
---|
121 | document.getElementById('markerStatus').innerHTML = str; |
---|
122 | } |
---|
123 | |
---|
124 | function updateMarkerPosition(latLng) { |
---|
125 | document.Envoie.lat.value=latLng.lat() ; |
---|
126 | document.Envoie.lon.value = latLng.lng(); |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | function updateMarkerAddress(str) { |
---|
131 | message = str; |
---|
132 | document.getElementById('markerAdress').innerHTML = str; |
---|
133 | |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | /* Initialisation de la carte */ |
---|
146 | function initialize() { |
---|
147 | geocoder = new google.maps.Geocoder(); |
---|
148 | var latlng = new google.maps.LatLng(40.730885, -73.997383); |
---|
149 | var myOptions = { |
---|
150 | zoom: 8, |
---|
151 | center: latlng, |
---|
152 | mapTypeId: 'roadmap' |
---|
153 | } |
---|
154 | map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | /* Fonction de géocodage déclenchée en cliquant surle bouton "Geocoder" */ |
---|
159 | function codeAddress() { |
---|
160 | /* Récupération de la valeur de l'adresse saisie */ |
---|
161 | var address = document.getElementById("match").value; |
---|
162 | /* Appel au service de geocodage avec l'adresse en paramètre */ |
---|
163 | geocoder.geocode({ 'address': address }, function (results, status) { |
---|
164 | /* Si l'adresse a pu être géolocalisée */ |
---|
165 | if (status == google.maps.GeocoderStatus.OK) { |
---|
166 | /* Récupération de sa latitude et de sa longitude */ |
---|
167 | document.Envoie.lat.value = results[0].geometry.location.lat(); |
---|
168 | document.Envoie.lon.value = results[0].geometry.location.lng(); |
---|
169 | map.setCenter(results[0].geometry.location); |
---|
170 | /* Affichage du marker */ |
---|
171 | var marker = new google.maps.Marker({ |
---|
172 | map: map, |
---|
173 | position: results[0].geometry.location |
---|
174 | }); |
---|
175 | /* Permet de supprimer le marker précédemment affiché */ |
---|
176 | markers.push(marker); |
---|
177 | if (markers.length > 1) |
---|
178 | markers[(i - 1)].setMap(null); |
---|
179 | i++; |
---|
180 | } else { |
---|
181 | alert("Le geocodage n\'a pu etre effectue pour la raison suivante: " + status); |
---|
182 | } |
---|
183 | }); |
---|
184 | } |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | /* Fonction de géocodage inversé */ |
---|
189 | function codeLatLng() { |
---|
190 | var lat = parseFloat(document.Envoie.lat.value); |
---|
191 | var lng = parseFloat(document.Envoie.lon.value); |
---|
192 | var latlng = new google.maps.LatLng(lat, lng); |
---|
193 | |
---|
194 | geocoder.geocode({ 'latLng': latlng }, |
---|
195 | function(results, status) { |
---|
196 | /* Si le géocodage inversé a réussi */ |
---|
197 | if (status == google.maps.GeocoderStatus.OK) { |
---|
198 | if (results[1]) { |
---|
199 | map.setZoom(11); |
---|
200 | marker = new google.maps.Marker({ |
---|
201 | position: latlng, |
---|
202 | map: map |
---|
203 | }); |
---|
204 | /* Affichage de l'infowindow sur le marker avec l'adresse récupérée */ |
---|
205 | infowindow.setContent(results[1].formatted_address); |
---|
206 | infowindow.open(map, marker); |
---|
207 | } |
---|
208 | } else { |
---|
209 | alert("Le geocodage a echoue pour la raion suivante : " + status); |
---|
210 | } |
---|
211 | }); |
---|
212 | } |
---|
213 | |
---|