web/data/cubicweb.gmap.js
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Wed, 24 Feb 2010 15:00:37 +0100
branchstable
changeset 4694 c19366a12281
parent 553 c69eac8298c0
child 5658 7b9553a9db65
child 5767 1d811df051c2
permissions -rw-r--r--
simplejson may not be available with python 2.4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
/*
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
 *  :organization: Logilab
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
 *  :copyright: 2003-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     4
 *  :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
 *
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
 *
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
 */
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
Widgets.GMapWidget = defclass('GMapWidget', null, {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
  __init__: function(wdgnode) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
    // Assume we have imported google maps JS
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
    if (GBrowserIsCompatible()) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
      var uselabelstr = wdgnode.getAttribute('cubicweb:uselabel');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    14
      var uselabel = true;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
      if (uselabelstr){
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
	if (uselabelstr == 'True'){
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    17
	  uselabel = true;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
	}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
	else{
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
	  uselabel = false;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
	}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
      }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
      var map = new GMap2(wdgnode);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
      map.addControl(new GSmallMapControl());
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
      var jsonurl = wdgnode.getAttribute('cubicweb:loadurl');
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
      var self = this; // bind this to a local variable
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
      jQuery.getJSON(jsonurl, function(geodata) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
	if (geodata.center) {
553
c69eac8298c0 [gmap] zoom level is now a url parameter
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 78
diff changeset
    29
	  var zoomLevel = geodata.zoomlevel;
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
	  map.setCenter(new GLatLng(geodata.center.latitude, geodata.center.longitude),
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
		        zoomLevel);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
	}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
	for (var i=0; i<geodata.markers.length; i++) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    34
	  var marker = geodata.markers[i];
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
	  self.createMarker(map, marker, i+1, uselabel);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
	}
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
      });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
      jQuery(wdgnode).after(this.legendBox);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
    } else { // incompatible browser
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
      jQuery.unload(GUnload);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
    }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    42
  },
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    43
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    44
  createMarker: function(map, marker, i, uselabel) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    45
    var point = new GLatLng(marker.latitude, marker.longitude);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    46
    var icon = new GIcon();
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    47
    icon.image = marker.icon[0];
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    48
    icon.iconSize = new GSize(marker.icon[1][0], marker.icon[1][1]) ;
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    49
    icon.iconAnchor = new GPoint(marker.icon[2][0], marker.icon[2][1]);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    50
    if(marker.icon[3]){
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    51
      icon.shadow4 =  marker.icon[3];
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    52
    }
78
079c4d11a616 [javascript][gmap] only use LabeledMarker in google maps when the extension is loaded
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    53
    if (typeof LabeledMarker == "undefined") {
079c4d11a616 [javascript][gmap] only use LabeledMarker in google maps when the extension is loaded
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    54
	var gmarker = new GMarker(point, {icon: icon,
079c4d11a616 [javascript][gmap] only use LabeledMarker in google maps when the extension is loaded
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    55
	title: marker.title});
079c4d11a616 [javascript][gmap] only use LabeledMarker in google maps when the extension is loaded
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    56
    } else {
079c4d11a616 [javascript][gmap] only use LabeledMarker in google maps when the extension is loaded
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    57
        var gmarker = new LabeledMarker(point, {
079c4d11a616 [javascript][gmap] only use LabeledMarker in google maps when the extension is loaded
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    58
          icon: icon,
079c4d11a616 [javascript][gmap] only use LabeledMarker in google maps when the extension is loaded
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    59
          title: marker.title,
079c4d11a616 [javascript][gmap] only use LabeledMarker in google maps when the extension is loaded
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    60
          labelText: uselabel?'<strong>' + i + '</strong>':'',
079c4d11a616 [javascript][gmap] only use LabeledMarker in google maps when the extension is loaded
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    61
          labelOffset: new GSize(2, -32)
079c4d11a616 [javascript][gmap] only use LabeledMarker in google maps when the extension is loaded
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    62
        });
079c4d11a616 [javascript][gmap] only use LabeledMarker in google maps when the extension is loaded
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents: 0
diff changeset
    63
    }
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
    map.addOverlay(gmarker);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
    GEvent.addListener(gmarker, 'click', function() {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
      jQuery.post(marker.bubbleUrl, function(data) {
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
	map.openInfoWindowHtml(point, data);
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    68
      });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
    });
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
  }
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
});