PageRenderTime 27ms CodeModel.GetById 11ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/pigeoncms/Plugins/fckeditor/editor/plugins/googlemaps/dialog/polyline.js

http://pigeoncms.googlecode.com/
JavaScript | 335 lines | 262 code | 62 blank | 11 comment | 33 complexity | 39a8aa1993c7dd8c1d5ec6e17951c86a MD5 | raw file
  1var points = [];
  2var highlighted_marker = null;
  3var point_markers = [];
  4var currentIndex = -1;
  5
  6// Returns the index of the marker in the polyline.
  7function findMarkerIndex(point_marker) {
  8  var index = -1;
  9
 10  for (var  i = 0; i < point_markers.length; ++i) {
 11    if (point_markers[i] == point_marker) {
 12      index = i;
 13      break;
 14    }
 15  }
 16
 17  return index;
 18}
 19
 20// Creates a point and adds it to both the polyline and the list.
 21function createPoint(lat, lng, pLevel) {
 22  var newPoint = {
 23    lat: lat,
 24    lon: lng,
 25    Level: pLevel
 26  };
 27
 28	if (currentIndex>-1) 
 29		points.splice(currentIndex+1, 0, newPoint) ;
 30	else
 31	  points.push(newPoint);
 32
 33  var point_marker = createPointMarker(new GLatLng(lat, lng), false);
 34	point_marker.focusable = true ; // To signal that the map must get the focus.
 35  map.addOverlay(point_marker);
 36
 37	if (currentIndex>-1) 
 38		point_markers.splice(currentIndex+1, 0, point_marker) ;
 39	else
 40	  point_markers.push(point_marker);
 41
 42	highlight(currentIndex+1);
 43}
 44
 45// Creates a marker representing a point in the polyline.
 46function createPointMarker(point, highlighted) {
 47  var clr = highlighted ? "yellow" : "blue";
 48
 49  var point_marker = createColorMarker(point, clr);
 50
 51  GEvent.addListener(point_marker, "drag", function() {
 52    var index = findMarkerIndex(point_marker);
 53
 54    if (index >= 0) {
 55      var nLat = point_marker.getPoint().lat();
 56      var nLng = point_marker.getPoint().lng();
 57
 58      var pLevel = points[index].Level;
 59
 60      var modifiedPoint = {
 61        lat: nLat,
 62        lon: nLng,
 63        Level: pLevel
 64      };
 65
 66      points[index] = modifiedPoint;
 67      createEncodings();
 68    }
 69  });
 70
 71  GEvent.addListener(point_marker, "click", function() {
 72    highlight(findMarkerIndex(point_marker));
 73  });
 74
 75  return point_marker;
 76}
 77
 78// Highlights the point specified by index in both the map and the point list.
 79function highlight(index) {
 80
 81  if (point_markers[index] != null
 82      && point_markers[index] != highlighted_marker) {
 83    map.removeOverlay(point_markers[index]);
 84  }
 85
 86  if (highlighted_marker != null) {
 87    var oldIndex = findMarkerIndex(highlighted_marker);
 88    map.removeOverlay(highlighted_marker);
 89
 90    if (oldIndex != index) {
 91      point_markers[oldIndex]
 92        = createPointMarker(highlighted_marker.getPoint(), false);
 93      map.addOverlay(point_markers[oldIndex]);
 94    }
 95  }
 96
 97  highlighted_marker = createPointMarker(point_markers[index].getPoint(),
 98                                         true);
 99  point_markers[index] = highlighted_marker;
100  map.addOverlay(highlighted_marker);
101
102	currentIndex = index ;
103}
104
105// Encode a signed number in the encode format.
106function encodeSignedNumber(num) {
107  var sgn_num = num << 1;
108
109  if (num < 0) {
110    sgn_num = ~(sgn_num);
111  }
112
113  return(encodeNumber(sgn_num));
114}
115
116// Encode an unsigned number in the encode format.
117function encodeNumber(num) {
118  var encodeString = "";
119
120  while (num >= 0x20) {
121    encodeString += (String.fromCharCode((0x20 | (num & 0x1f)) + 63));
122    num >>= 5;
123  }
124
125  encodeString += (String.fromCharCode(num + 63));
126  return encodeString;
127}
128
129// Delete a point from the polyline.
130function deletePoint() {
131  if (points.length > 0) {
132    var point_index = currentIndex;
133
134    if (point_index >= 0 && point_index < points.length) {
135      points.splice(point_index, 1);
136
137      if (highlighted_marker == point_markers[point_index]) {
138        highlighted_marker = null;
139				currentIndex=-1;
140      }
141
142      map.removeOverlay(point_markers[point_index]);
143      point_markers.splice(point_index, 1);
144      createEncodings();
145    }
146
147    if (points.length > 0) {
148      if (point_index == 0) {
149        point_index++;
150      }
151
152      highlight(point_index - 1);
153    }
154  }
155}
156
157
158// Create the encoded polyline and level strings. 
159function createEncodings() {
160	if (points.length==0) 
161	{
162		document.getElementById('encodedLevels').value = '';
163		document.getElementById('encodedPolyline').value = '';
164		if (document.overlay) {
165			map.removeOverlay(document.overlay);
166		}
167		return;
168	}
169
170	var encoded_levels='';
171  var encoded_points='';
172	var vZoom, vLevels;
173
174		vLevels = 4;
175		vZoom = 32;
176
177		var plat = 0;
178		var plng = 0;
179
180		for(var i = 0; i < points.length; ++i) {
181			var point = points[i];
182			var lat = point.lat;
183			var lng = point.lon;
184			var level = point.Level;
185
186			var late5 = Math.floor(lat * 1e5);
187			var lnge5 = Math.floor(lng * 1e5);
188
189			dlat = late5 - plat;
190			dlng = lnge5 - plng;
191
192			plat = late5;
193			plng = lnge5;
194
195			encoded_points += encodeSignedNumber(dlat) + encodeSignedNumber(dlng);
196			encoded_levels += encodeNumber(level);
197		}
198
199  document.getElementById('encodedLevels').value = encoded_levels.replace(/\\/g, "\\\\");
200  document.getElementById('encodedPolyline').value = encoded_points.replace(/\\/g, "\\\\");
201
202  if (document.overlay) {
203    map.removeOverlay(document.overlay);
204  }
205
206  if (points.length > 1) {
207    document.overlay = GPolyline.fromEncoded({color: "#3333cc",
208                                              weight: 5,
209                                              points: encoded_points,
210                                              zoomFactor: vZoom,
211                                              levels: encoded_levels,
212                                              numLevels: vLevels
213                                             });
214
215    map.addOverlay(document.overlay);
216  }
217}
218
219
220// Decode an encoded polyline into a list of lat/lng tuples.
221function decodeLine (encoded) {
222  var len = encoded.length;
223  var index = 0;
224  var array = [];
225  var lat = 0;
226  var lng = 0;
227
228  while (index < len) {
229    var b;
230    var shift = 0;
231    var result = 0;
232    do {
233      b = encoded.charCodeAt(index++) - 63;
234      result |= (b & 0x1f) << shift;
235      shift += 5;
236    } while (b >= 0x20);
237    var dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
238    lat += dlat;
239
240    shift = 0;
241    result = 0;
242    do {
243      b = encoded.charCodeAt(index++) - 63;
244      result |= (b & 0x1f) << shift;
245      shift += 5;
246    } while (b >= 0x20);
247    var dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
248    lng += dlng;
249
250    array.push([lat * 1e-5, lng * 1e-5]);
251  }
252
253  return array;
254}
255
256// Decode an encoded levels string into a list of levels.
257function decodeLevels(encoded) {
258  var levels = [];
259
260  for (var pointIndex = 0; pointIndex < encoded.length; ++pointIndex) {
261    var pointLevel = encoded.charCodeAt(pointIndex) - 63;
262    levels.push(pointLevel);
263  }
264
265  return levels;
266}
267
268// Decode the supplied encoded polyline and levels.
269function decodePolyline() {
270  var encoded_points = document.getElementById('encodedPolyline').value;
271  encoded_points = encoded_points.replace(/\\\\/g, "\\");
272
273  if (encoded_points.length==0) {
274    return;
275  }
276
277  var enc_points = decodeLine(encoded_points);
278
279  if (enc_points.length==0) {
280    return;
281  }
282
283  points = [];
284
285  for (var i = 0; i < enc_points.length; ++i) {
286    createPoint(enc_points[i][0], enc_points[i][1], 3);
287  }
288
289  createEncodings();
290}
291
292function ShowLinePoints()
293{
294	if (points.length==0)
295		return;
296
297	for (var i=0; i<points.length ; i++)
298	{
299		var point = points[i] ;
300		var point_marker = createPointMarker(new GLatLng(point.lat, point.lon), false);
301		map.addOverlay(point_marker);
302	  point_markers.push(point_marker);
303	}
304
305	highlight(points.length-1);
306}
307
308function HideLinePoints()
309{
310	for (var i=point_markers.length -1;i>=0 ; i--)
311	{
312      map.removeOverlay(point_markers[i]);
313	}
314	point_markers = [] ;
315	highlighted_marker = null;
316	currentIndex=-1;
317}
318
319function createColorMarker(point, color) {
320  var f = new GIcon();
321  f.image = "http://labs.google.com/ridefinder/images/mm_20_" + color
322            + ".png";
323  f.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
324  f.iconSize = new GSize(12,20);
325  f.shadowSize = new GSize(22,20);
326  f.iconAnchor = new GPoint(6,20);
327  f.infoWindowAnchor = new GPoint(6,1);
328  f.infoShadowAnchor = new GPoint(13,13);
329
330  newMarker = new GMarker(point,
331    {icon: f, draggable: true});
332
333  return newMarker;
334}
335