File: map_control.js

package info (click to toggle)
virtuoso-opensource 7.2.5.1%2Bdfsg1-0.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 285,240 kB
  • sloc: ansic: 641,220; sql: 490,413; xml: 269,570; java: 83,893; javascript: 79,900; cpp: 36,927; sh: 31,653; cs: 25,702; php: 12,690; yacc: 10,227; lex: 7,601; makefile: 7,129; jsp: 4,523; awk: 1,697; perl: 1,013; ruby: 1,003; python: 326
file content (326 lines) | stat: -rw-r--r-- 10,034 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 *  $Id$
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2018 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

/*
  CONSTANTS
*/
// max zoom allowed by Google maps
var MAX_ZOOM_LEVEL = 17;
// Where the "center of the world is"
var BURLINGTON_LNG = -71.19;
var BURLINGTON_LAT = 42.490;
// Icon size for the marker icon (in pixels)
var ICON_WIDTH = 16;
var ICON_HEIGHT = 16;
// Place where the pointing point is inside the marker icon
var ICON_ANCHOR_OFS_X = 7;
var ICON_ANCHOR_OFS_Y = 14;
// Place where to stick the balloon's tip inside the marker icon
var ICON_INFO_ANCHOR_X = 8;
var ICON_INFO_ANCHOR_Y = 5;
// Add that many zoom levels to the autocalculated one (so the gmaps won't drop the corner markers)
var ADDITIONAL_ZOOM_LEVELS = 1;
// no lower than that zoom level for auto zoom
var MIN_ZOOM_LEVEL = 3;
// the initial map type
var MAP_TYPE = G_MAP_TYPE;

/*
FUNCTIONS
*/


/*
 * Util function
 * Puts a marker and adds the event handler to print the balloon on click.
*/
function createMarker(point, icon, excerpt)
{
  var marker = new GMarker(point, icon);

  // Show this markers index in the info window when it is clicked.
  GEvent.addListener(marker, 'click', function() {
      marker.openInfoWindowHtml(excerpt);
      });

  return marker;
}

/*
 * Util function
 * picks a marker icon based on the number of the points in the aggregation.
*/
function pickMarkerIcon (icons, count)
{
  if (count > 1 && icons.length > 1)
    return icons[1];
  else
    return icons[0];
}

/*
 * Util function
 * The ready handler for the http request to the ajax data server.
 * parses the incoming XML, puts the markers on the map and
 * calculates and sets the zoom level to fit all the markers
*/
function ProcessMarkerXML (map, icons, request, do_center, custom_zoom_level)
{
  try
    {
      if (request.readyState == 4)
	{
	  if (request.status == 200)
	    {
	      var xmlDoc = request.responseXML;
	      var markers = xmlDoc.documentElement.getElementsByTagName("marker");
	      var max_bounds = null;
	      var center = null;
	      var lng;
	      var lat;
	      var count;
	      for (var i = 0; i < markers.length; i++)
		{
		  lng = parseFloat (markers[i].getAttribute("lng"));
		  lat = parseFloat (markers[i].getAttribute("lat"));
		  count = parseInt (markers[i].getAttribute("count"));
		  if (do_center == true)
		    {
	//	      alert ('lat=' + lat + ' lng=' + lng);
		      if (max_bounds == null)
			{
			  max_bounds = new GBounds (lng,lat,lng,lat);
	//		  alert ('NEW !minX=' + max_bounds.minX + ' maxX=' + max_bounds.maxX +
	//		      ' minY=' + max_bounds.minY + ' maxY=' + max_bounds.maxY);
			}
		      else
			{
			  if (max_bounds.minX > lng)
			    max_bounds.minX = lng;
			  else if (max_bounds.maxX < lng)
			    max_bounds.maxX = lng;
			  if (max_bounds.minY > lat)
			    max_bounds.minY = lat;
			  else if (max_bounds.maxY < lat)
			    max_bounds.maxY = lat;
	//		  alert ('SET minX=' + max_bounds.minX + ' maxX=' + max_bounds.maxX +
	//		      ' minY=' + max_bounds.minY + ' maxY=' + max_bounds.maxY);
			}
		    }
                  else
                    {
		      map.addOverlay (createMarker(
			 new GPoint(lng, lat),
			 pickMarkerIcon (icons, count),
			 GXml.value (markers[i])
		      ));
                    }

		  if (do_center == true && markers[i].getAttribute("center") == "true")
		    {
		      center = new GPoint (lng, lat);
		    }
		}
	      if (do_center == true && max_bounds != null)
		{
	//	  alert ('minX=' + max_bounds.minX + ' maxX=' + max_bounds.maxX +
	//	      ' minY=' + max_bounds.minY + ' maxY=' + max_bounds.maxY);
		  if (center == null)
		    center = new GPoint(
			(max_bounds.maxX - max_bounds.minX)/2,
			(max_bounds.maxY - max_bounds.minY)/2 );
		  var delta = new GSize(max_bounds.maxX - max_bounds.minX, max_bounds.maxY - max_bounds.minY);
		  var minZoom = map.spec.getLowestZoomLevel(center, delta, map.viewSize) + ADDITIONAL_ZOOM_LEVELS;
                  if (minZoom < MIN_ZOOM_LEVEL)
                    minZoom = MIN_ZOOM_LEVEL;
      if (custom_zoom_level<0){
          map.centerAndZoom(center, minZoom);
      }else{
         if (custom_zoom_level<MIN_ZOOM_LEVEL) custom_zoom_level=MIN_ZOOM_LEVEL;
         if (custom_zoom_level>MAX_ZOOM_LEVEL) custom_zoom_level=MIN_ZOOM_LEVEL;
         map.centerAndZoom(center, custom_zoom_level);
      }

//		  GEvent.addListener(map, "moveend", onMapMoved);
		}
	    }
	  else
	    {
	      throw ("HTTP error " + request.status);
	    }
	}
    }
  catch (e)
    {
      alert ("Cannot retrieve data for the map from the server : " + e);
    }
}

/*
 * Util function
 * Calculates lat/lng out of screen pixels
 * Returns a GPoint
*/
function getLatLongFromBitmapSize ( map, x, y )
{
  var type = map.getCurrentMapType();
  var bord = map.getBoundsLatLng();
  var zoom = map.getZoomLevel();
  var at = type.getBitmapCoordinate( bord.minY, bord.minX, zoom );
  var offset = type.getLatLng( at.x + x, at.y + y, zoom );
/*  alert ('bord.minX=' + bord.minX + ' bord.minY=' + bord.minY + '\n' +
       "x=" + x + ' y=' + y + '\n' +
  'at.x=' + at.x + ' at.y=' + at.y + '\n' +
  'offset.x=' + offset.x + ' offset.y=' + offset.y);*/
  return new GPoint (offset.x - bord.minX, Math.abs (offset.y - bord.minY));
}

/*
 * Util function
 * Constructs the query URL to the ajax server, clears the map and sends the request
*/
function FillIconList(map, ajax_server_url, icons, inst, is_initial, custom_zoom_level)
{
  try
    {
      // center the map if it is the first call to the function
      var do_center = is_initial;
      var bounds = map.getBoundsLatLng();

      var lat_min = bounds.minY;
      var lat_max = bounds.maxY;
      var lng_min = bounds.minX;
      var lng_max = bounds.maxX;

      // zero means no binding
      var lat_step = 0;
      var lng_step = 0;
      if (is_initial == false)
        {
	  // calculates the width and height of the icon in geo coordinates
	  // and use that as a step for binding the points together server side
          var size = getLatLongFromBitmapSize (map, ICON_WIDTH, ICON_HEIGHT);
	  lat_step = size.y;
	  lng_step = size.x;
          /*alert ('lat_min=' + lat_min + ' lat_max=' + lat_max + ' lat_step=' + lat_step + '\n' +
          'lng_min=' + lng_min + ' lng_max=' + lng_max + ' lng_step=' + lng_step);*/
        }

      var url =
	  ajax_server_url +
	  "?inst=" + inst +
	  "&al=" + lat_min +
	  "&ah=" + lat_max +
	  "&nl=" + lng_min +
	  "&nh=" + lng_max +
	  "&as=" + lat_step +
	  "&ns=" + lng_step;
      var request = GXmlHttp.create();

      request.open("GET", url, true);
      request.onreadystatechange = function () {
         ProcessMarkerXML (map, icons, request, do_center, custom_zoom_level);
      };

      map.clearOverlays ();
      request.send (null);
    }
  catch (e)
    {
      alert ("Cannot retrieve data for the map from the server : " + e);
    }
}


/*
 * The Top level function :
 * Inits the map into the given ID, sets the callbacks and
 * asks the ajax server for points
 * Params :
 *   div_id : the DIV to put the map in
 *   ajax_server_url : The url to ajax_server.vsp
 *   icon_base_url : The url to the icon(s)
 *   inst_id : the id of the search SQL stored in the ajax server search table
 *
*/
function initMap (div_id,ajax_server_url,icon_base_url,inst_id, custom_zoom_level)
{
  if (typeof(custom_zoom_level)=='undefined') custom_zoom_level=-1;

  var map = new GMap(document.getElementById(div_id));
  var infoOpened = false;

  map.setMapType (MAP_TYPE);
  map.addControl(new GSmallMapControl());
  map.addControl(new GMapTypeControl());

  // start with all of the world centered on Burlington, MA
  // needed so the ajax server can get all of the points and
  // autoscale correctly
  map.centerAndZoom (
      new GPoint (BURLINGTON_LNG, BURLINGTON_LAT), // on Burlington, MA
      MAX_ZOOM_LEVEL // max zoom : to see all of the world
      );

  // Create our marker icons
  var icons = [ new GIcon(), new GIcon() ];

  // for the normal markers
  icons[0] = new GIcon();
  icons[0].image = icon_base_url + "/user_16.png";
  icons[0].iconSize = new GSize(ICON_WIDTH, ICON_HEIGHT);
  icons[0].iconAnchor = new GPoint(ICON_ANCHOR_OFS_X, ICON_ANCHOR_OFS_Y);
  icons[0].infoWindowAnchor = new GPoint(ICON_INFO_ANCHOR_X, ICON_INFO_ANCHOR_Y);

  // for aggregated markers
  icons[1] = new GIcon();
  icons[1].image = icon_base_url + "/group_24.png";
  icons[1].iconSize = new GSize(ICON_WIDTH, ICON_HEIGHT);
  icons[1].iconAnchor = new GPoint(ICON_ANCHOR_OFS_X, ICON_ANCHOR_OFS_Y);
  icons[1].infoWindowAnchor = new GPoint(ICON_INFO_ANCHOR_X, ICON_INFO_ANCHOR_Y);

  // stop the redrawing when a balloon is to be shown
  var onInfoWindowOpen = function ()
    {
      infoOpened = true;
    };

  // if no balloon redraw on move.
  // note that we dont need the onZoom handler since the
  // gmaps will issue onMove even on zoom.
  var onMapMoved = function ()
    {
      if( infoOpened == true )
	{
	  infoOpened = false;
	  return;
	}

      FillIconList(map, ajax_server_url, icons, inst_id, false, null, custom_zoom_level);
    };

  // init the map
  GEvent.addListener(map, "moveend", onMapMoved);
  FillIconList(map, ajax_server_url, icons, inst_id, true, custom_zoom_level);
  GEvent.addListener(map, "infowindowopen", onInfoWindowOpen);
}