| 1 |
/*
|
| 2 |
* This file is based on :
|
| 3 |
* jQuery googleMap Copyright Dylan Verheul <dylan@dyve.net>
|
| 4 |
* Licensed like jQuery, see http://docs.jquery.com/License
|
| 5 |
*
|
| 6 |
* Some initial modifications made by Peter Brownell for Code Positive and
|
| 7 |
* School of Everything.
|
| 8 |
*
|
| 9 |
* We have made some modifications to the original file, but not that many.
|
| 10 |
* There is a good amount of work to do to tie this into Dupal and make
|
| 11 |
* it configurable and themeable.
|
| 12 |
*
|
| 13 |
* For now, unless you want to patch the source, there is not all that
|
| 14 |
* much to can do to change things.
|
| 15 |
*
|
| 16 |
* Ideas and plans welcome.
|
| 17 |
*/
|
| 18 |
|
| 19 |
$(document).ready(function()
|
| 20 |
{
|
| 21 |
$("#map").googleMap(null,null,13 , { markers: $(".geo"),controls: ["GSmallZoomControl"]});
|
| 22 |
});
|
| 23 |
|
| 24 |
$.googleMap = {
|
| 25 |
maps: {},
|
| 26 |
marker: function(m) {
|
| 27 |
if (!m) {
|
| 28 |
return null;
|
| 29 |
}
|
| 30 |
else if (m.lat == null && m.lng == null) {
|
| 31 |
return $.googleMap.marker($.googleMap.readFromGeo(m));
|
| 32 |
}
|
| 33 |
else {
|
| 34 |
// Instantiate icon with default image & shadow
|
| 35 |
var tinyIcon = new GIcon(G_DEFAULT_ICON);
|
| 36 |
var dg = new Boolean();
|
| 37 |
dg = false;
|
| 38 |
if (m.draggable) {
|
| 39 |
if (m.draggable == "true"){
|
| 40 |
dg = true;
|
| 41 |
}
|
| 42 |
}
|
| 43 |
///======================HACKKKKKK
|
| 44 |
tinyIcon.iconSize = new GSize(32, 32);
|
| 45 |
tinyIcon.shadowSize = new GSize(32, 38);
|
| 46 |
tinyIcon.image = 'http://schoolofeverything.com/sites/default/themes/everything2/images/star_pointer.png';
|
| 47 |
tinyIcon.shadow = 'http://schoolofeverything.com/sites/default/themes/everything2/images/star_shadow.png';
|
| 48 |
|
| 49 |
//use custom icon if set
|
| 50 |
if (m.gicon == 'null') {
|
| 51 |
tinyIcon.image = null;
|
| 52 |
}
|
| 53 |
else if (m.gicon) {
|
| 54 |
tinyIcon.image = m.gicon;
|
| 55 |
}
|
| 56 |
//use custom shadow if set
|
| 57 |
if (m.gshadow == 'null'){
|
| 58 |
tinyIcon.shadow = null;
|
| 59 |
}
|
| 60 |
else if (m.gshadow){
|
| 61 |
tinyIcon.shadow = m.gshadow;
|
| 62 |
}
|
| 63 |
//if icon is set but no shadow is set, display no shadow
|
| 64 |
else if (m.icon && !m.gshadow){
|
| 65 |
tinyIcon.shadow = "";
|
| 66 |
}
|
| 67 |
|
| 68 |
tinyIcon.iconAnchor = new GPoint(15, 15); // set to centre
|
| 69 |
tinyIcon.infoWindowAnchor = new GPoint(5, 1);
|
| 70 |
|
| 71 |
markerOptions = { icon:tinyIcon, draggable:dg };
|
| 72 |
|
| 73 |
var marker = new GMarker(new GLatLng(m.lat, m.lng), markerOptions );
|
| 74 |
if (m.bind) {
|
| 75 |
GEvent.addListener(marker, "click", function() {
|
| 76 |
marker.openInfoWindowHtml('<div class="geomap-infowindow">'+$(m.bind).html()+'</div>');
|
| 77 |
});
|
| 78 |
}
|
| 79 |
else if (m.txt) {
|
| 80 |
GEvent.addListener(marker, "click", function() {
|
| 81 |
marker.openInfoWindowHtml(m.txt);
|
| 82 |
});
|
| 83 |
}
|
| 84 |
if (m.link) {
|
| 85 |
GEvent.addListener(marker, "dblclick", function() {
|
| 86 |
window.location=m.link;
|
| 87 |
});
|
| 88 |
}
|
| 89 |
// set plain title for clusters
|
| 90 |
marker.titleplain=m.titleplain;
|
| 91 |
if (dg) {
|
| 92 |
|
| 93 |
GEvent.addListener(marker, "dragend", function() {
|
| 94 |
//confirm whether to register a new location
|
| 95 |
if (confirm('Pointer icon has been relocated.\nWould you like to update the location information?')){
|
| 96 |
var newpoint = marker.getLatLng();
|
| 97 |
//alert ("services/location/geonames/latlng/"+m.nid+"/"+m.cckfield+"/"+m.cckfieldindex+"/"+newpoint.lat()+"/"+newpoint.lng()+"/background");
|
| 98 |
var domain_name = document.domain;
|
| 99 |
//for testing
|
| 100 |
if (domain_name == "localhost"){
|
| 101 |
domain_name = "localhost/map";
|
| 102 |
}
|
| 103 |
$.ajax({
|
| 104 |
type: "GET",
|
| 105 |
url: "http://"+domain_name+"/services/location/geonames/latlng/"+m.nid+"/"+m.cckfield+"/"+m.cckfieldindex+"/"+newpoint.lat()+"/"+newpoint.lng()+"/background",
|
| 106 |
error: function(msg){
|
| 107 |
alert( "Sorry, relocation failed");
|
| 108 |
},
|
| 109 |
success: function(msg){
|
| 110 |
alert("Relocation successful");
|
| 111 |
}
|
| 112 |
});
|
| 113 |
}
|
| 114 |
//cancel
|
| 115 |
else {
|
| 116 |
alert("Relocation cancelled by user:\n\nPointer icon was moved, but location information was not updated.\nReloading the page will place the icon back to its original location.");
|
| 117 |
}
|
| 118 |
});
|
| 119 |
|
| 120 |
|
| 121 |
}
|
| 122 |
return marker;
|
| 123 |
}
|
| 124 |
},
|
| 125 |
readFromGeo: function(elem) {
|
| 126 |
var latElem = $(".latitude", elem)[0];
|
| 127 |
var lngElem = $(".longitude", elem)[0];
|
| 128 |
var title = '';
|
| 129 |
var titleplain = '';
|
| 130 |
var link = '';
|
| 131 |
if (latElem && lngElem) {
|
| 132 |
titleplain = $(elem).attr("title");
|
| 133 |
if ( $(elem).attr("title") ) {
|
| 134 |
if ( $(elem).attr("nid") > 0 ) {
|
| 135 |
link = '/node/'+$(elem).attr("nid");
|
| 136 |
title = '<h4 class="maptitle"><a href="'+link+'">'+$(elem).attr("title")+'</a></h4>';
|
| 137 |
if ( $(elem).attr("info") ) { title += $(elem).attr("info"); }
|
| 138 |
}
|
| 139 |
else {
|
| 140 |
title = $(elem).attr("title");
|
| 141 |
}
|
| 142 |
}
|
| 143 |
|
| 144 |
return { lat:parseFloat($(latElem).attr("title")), lng:parseFloat($(lngElem).attr("title")),
|
| 145 |
txt:title, link: link, bind: $(elem).attr('bind'),
|
| 146 |
gicon: $(elem).attr('gicon'), gshadow: $(elem).attr('gshadow'), draggable: $(elem).attr('draggable'),
|
| 147 |
cckfield: $(elem).attr('cckfield'), cckfieldindex: $(elem).attr('cckfieldindex'), nid: $(elem).attr("nid"),
|
| 148 |
titleplain: $(elem).attr("title")
|
| 149 |
}
|
| 150 |
}
|
| 151 |
else {
|
| 152 |
return null;
|
| 153 |
}
|
| 154 |
},
|
| 155 |
mapNum: 1
|
| 156 |
};
|
| 157 |
|
| 158 |
|
| 159 |
$.fn.googleMap = function(lat, lng, zoom, options) {
|
| 160 |
|
| 161 |
// If we aren't supported, we're done
|
| 162 |
if (!window.GBrowserIsCompatible || !GBrowserIsCompatible()) return this;
|
| 163 |
// Default values make for easy debugging
|
| 164 |
if (lat == null) lat = 51.52177;
|
| 165 |
if (lng == null) lng = -0.20101;
|
| 166 |
if (!zoom) zoom = 1;
|
| 167 |
// Sanitize options
|
| 168 |
if (!options || typeof options != 'object') options = {};
|
| 169 |
options.mapOptions = options.mapOptions || {};
|
| 170 |
options.markers = options.markers || [];
|
| 171 |
options.controls = options.controls || {};
|
| 172 |
|
| 173 |
// Map all our elements
|
| 174 |
return this.each(function() {
|
| 175 |
// Make sure we have a valid id
|
| 176 |
if (!this.id) this.id = "gMap" + $.googleMap.mapNum++;
|
| 177 |
// find our markers
|
| 178 |
var marker = null;
|
| 179 |
var markers = new Array();
|
| 180 |
var bounds = null;
|
| 181 |
for (var i = 0; i < options.markers.length; i++) {
|
| 182 |
if (marker = $.googleMap.marker(options.markers[i])) {
|
| 183 |
markers[i] = marker;
|
| 184 |
if (!bounds) {
|
| 185 |
var bounds = new GLatLngBounds(marker.getPoint());
|
| 186 |
}
|
| 187 |
else {
|
| 188 |
// extend bounds with marker.getPoint();
|
| 189 |
bounds.extend(marker.getPoint());
|
| 190 |
}
|
| 191 |
}
|
| 192 |
}
|
| 193 |
// we only want a map if we have markers
|
| 194 |
if (markers.length) {
|
| 195 |
$(this).css('display','block');
|
| 196 |
// Create a map and a shortcut to it at the same time
|
| 197 |
var map = $.googleMap.maps[this.id] = new GMap2(this, options.mapOptions);
|
| 198 |
var clusterer = new Clusterer(map);
|
| 199 |
clusterer.SetMaxVisibleMarkers(20);
|
| 200 |
clusterer.SetMaxLinesPerInfoBox(5);
|
| 201 |
|
| 202 |
ClustererIcon = new GIcon();
|
| 203 |
ClustererIcon.image = '/sites/default/themes/everything2/images/star_pointer_multiple.png';
|
| 204 |
ClustererIcon.shadow = '/sites/default/themes/everything2/images/star_shadow_multiple.png';
|
| 205 |
ClustererIcon.iconSize = new GSize( 74, 74 );
|
| 206 |
ClustererIcon.shadowSize = new GSize( 74, 79 );
|
| 207 |
ClustererIcon.iconAnchor = new GPoint( 37, 37 );
|
| 208 |
ClustererIcon.infoWindowAnchor = new GPoint( 50, 4 );
|
| 209 |
ClustererIcon.infoShadowAnchor = new GPoint( 50, 0 );
|
| 210 |
|
| 211 |
clusterer.SetIcon(ClustererIcon);
|
| 212 |
|
| 213 |
// Center and zoom the map
|
| 214 |
map.setCenter(new GLatLng(lat, lng), zoom);
|
| 215 |
// Add controls to our map
|
| 216 |
for (var i = 0; i < options.controls.length; i++) {
|
| 217 |
var c = options.controls[i];
|
| 218 |
eval("map.addControl(new " + c + "());");
|
| 219 |
}
|
| 220 |
for (var i = 0; i < markers.length; i++) {
|
| 221 |
//map.addOverlay(markers[i]);
|
| 222 |
clusterer.AddMarker( markers[i], markers[i].titleplain );
|
| 223 |
}
|
| 224 |
// time to zoom the map
|
| 225 |
var distance = 0.015;
|
| 226 |
if ( markers.length == 1 ) {
|
| 227 |
// if we only have one marker, we move out a bit more
|
| 228 |
distance = 0.5;
|
| 229 |
}
|
| 230 |
// Moving the map to show our markers
|
| 231 |
// We have to set the centre to get the bounds properly
|
| 232 |
|
| 233 |
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
|
| 234 |
var southWest=bounds.getSouthWest();
|
| 235 |
var northEast=bounds.getNorthEast();
|
| 236 |
bounds.extend(new GLatLng(southWest.lat() - distance, southWest.lng() - distance ));
|
| 237 |
bounds.extend(new GLatLng(northEast.lat() + distance, northEast.lng() + distance ));
|
| 238 |
map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
|
| 239 |
map.enableScrollWheelZoom();
|
| 240 |
}
|
| 241 |
});
|
| 242 |
};
|