function TGMap(elem, settings) {
this.map = null;
this.points = new TGPoints();
this.linkTarget = null;
this.defaultZoom = 5;
this.defaultIconZoom = 15;
this.defaultGeocodeZoom = 12;
this.defaultLat = 43.46;
this.defaultLng = 11.41;
if (settings.lat != undefined) {
this.defaultLat = settings.lat;
}
if (settings.lng != undefined) {
this.defaultLng = settings.lng;
}
if (settings.zoom != undefined) {
this.defaultZoom = settings.zoom;
}
if (settings.linkTarget != undefined) {
this.linkTarget = settings.linkTarget;
}
if (!GBrowserIsCompatible()) {
alert("Dieser Browser kann leider keine Google Maps darstellen");
return false;
}
// additional map layers (OSM, T@H, ...)
var copyOSM = new GCopyrightCollection("OpenStreetMap");
copyOSM.addCopyright(new GCopyright(1, new GLatLngBounds(new GLatLng(-90,-180), new GLatLng(90,180)), 0, " "));
var tilesMapnik = new GTileLayer(copyOSM, 1, 17, {tileUrlTemplate: 'http://tile.openstreetmap.org/{Z}/{X}/{Y}.png'});
var tilesOsmarender = new GTileLayer(copyOSM, 1, 17, {tileUrlTemplate: 'http://tah.openstreetmap.org/Tiles/tile/{Z}/{X}/{Y}.png'});
var mapMapnik = new GMapType([tilesMapnik], G_NORMAL_MAP.getProjection(), "OSM");
var mapOsmarender = new GMapType([tilesOsmarender], G_NORMAL_MAP.getProjection(), "T@H");
// create map instance, add controls
this.map = new GMap2(document.getElementById(elem));
this.mapTypeControl = new GHierarchicalMapTypeControl();
this.mapTypeControl.clearRelationships();
this.mapTypeControl.addRelationship(G_SATELLITE_MAP, G_HYBRID_MAP, "Straßen", false);
this.map.addControl(this.mapTypeControl);
this.map.addControl(new GLargeMapControl3D());
this.map.addControl(new GScaleControl());
this.map.addMapType(G_PHYSICAL_MAP);
this.map.addMapType(mapMapnik);
this.map.addMapType(mapOsmarender);
this.map.setMapType(G_NORMAL_MAP);
// tweak some map settings
this.map.enableContinuousZoom();
this.map.enableScrollWheelZoom();
new GKeyboardHandler(this.map);
// the Clusterer class manages a large number of markers
this.clusterer = new Clusterer(this.map);
// our default marker icon
this.icon = new GIcon();
this.icon.image = "http://tourguide.mtb-news.de/img/icon/mm_20_" + this.color + ".png";
this.icon.shadow = "http://tourguide.mtb-news.de/img/icon/mm_20_shadow.png";
this.icon.iconSize = new GSize(12, 20);
this.icon.shadowSize = new GSize(22, 20);
this.icon.iconAnchor = new GPoint(6, 20);
this.icon.infoWindowAnchor = new GPoint(5, 1);
};
TGMap.prototype.drawTourIcons = function(url, settings) {
if (!url) {
return;
}
var self = this;
var settings = settings;
this.points = new TGPoints();
$.getJSON(url, function(data, textStatus) {
if (textStatus != 'success') {
return;
}
// draw icons
for (var i = 0; i < data.length; i++) {
self._createTourIcon(data[i]);
}
// zoom/center?
if (settings.zoomToBounds == true) {
self.zoomToBoundsPoints();
} else if (settings.centerTo != undefined) {
self.centerTo(settings.centerTo);
} else {
self.zoomToDefault();
}
});
};
TGMap.prototype.drawTourIcon = function(tour) {
this._createTourIcon(tour);
this.centerTo({ lat: tour.Tour.latitude, lng: tour.Tour.longitude, zoom: this.defaultIconZoom });
};
TGMap.prototype._createTourIcon = function(tour) {
if (tour.Tour.gpstrack == 1) {
var gpsicn = 'si ';
} else {
var gpsicn = "no";
}
if (this.linkTarget != null) {
var target = ' target="' + this.linkTarget + '"';
} else {
var target = '';
}
var icn = new GIcon(this.icon, "http://tourguide.mtb-news.de/img/icon/mm_20_" + tour.Category.mapiconcolor + ".png");
var ltlng = new GLatLng(tour.Tour.latitude, tour.Tour.longitude);
this.points.add(ltlng);
var mrkr = new GMarker(ltlng, { icon: icn, title: tour.Tour.title });
var scope = this;
GEvent.addListener(mrkr, "click", function() {
var myHtml = ' \
\
\
di ' + tour.User.name + '
\
\
\
Tipologia: | \
' + tour.Category.title + ' | \
\
\
Località: | \
' + tour.Tour.city + ' | \
\
\
Traccia GPS: | \
' + gpsicn + ' | \
\
\
Inserito: | \
' + tour.Tour.created + ' | \
\
\
\
';
scope.map.openInfoWindowHtml(ltlng, myHtml);
});
this.clusterer.AddMarker(mrkr, tour.Tour.title);
};
TGMap.prototype.editableIcon = function(tour, options) {
if (options.zoom != undefined) {
var zoom = options.zoom;
} else {
var zoom = this.defaultIconZoom;
}
this.centerTo({ lat: tour.Tour.latitude, lng: tour.Tour.longitude, zoom: zoom })
var icn = new GIcon(this.icon, "http://tourguide.mtb-news.de/img/icon/mm_20_" + tour.Category.mapiconcolor + ".png");
var ltlng = new GLatLng(tour.Tour.latitude, tour.Tour.longitude);
var mrkr = new GMarker(ltlng, { draggable: true, icon: icn, title: tour.Tour.title, bouncy: true });
this.map.addOverlay(mrkr);
var self = this;
updateFormFieldsOptions = {
latfield: options.latfield,
lngfield: options.lngfield,
lat: ltlng.lat(),
lng: ltlng.lng()
};
if (options.geocodeCityField != undefined) {
updateFormFieldsOptions.cityfield = options.geocodeCityField;
}
// handling of marker dragging
GEvent.addListener(mrkr, 'dragend', function(ll) {
updateFormFieldsOptions.lat = ll.lat();
updateFormFieldsOptions.lng = ll.lng();
self.updateFormFields(updateFormFieldsOptions);
});
// forward geocoder handling (sets marker position, centers map, and updates form fields)
if (options.forwardGeocodeField != undefined) {
self.forwardGeocoder(options.forwardGeocodeField, { 'marker' : mrkr, 'updateFormFieldsOptions': updateFormFieldsOptions });
}
};
TGMap.prototype.updateFormFields = function(options) {
$("#" + options.latfield).val(options.lat);
$("#" + options.lngfield).val(options.lng);
if (options.cityfield != undefined) {
this.tmpCityFieldId = options.cityfield;
var self = this;
var geocoder = new GClientGeocoder();
geocoder.getLocations(new GLatLng(options.lat, options.lng), function(response) {
if (!response || response.Status.code != 200) {
$("#" + self.tmpCityFieldId).val("");
} else {
var place = response.Placemark[0];
$("#" + self.tmpCityFieldId).val(place.address);
}
});
};
};
TGMap.prototype.forwardGeocoder = function(addressFieldName, options) {
var self = this;
$("#" + addressFieldName + "Submit").click(function() {
var geocoder = new GClientGeocoder();
var updateFormFieldsOptions = options.updateFormFieldsOptions;
geocoder.getLatLng($("#" + addressFieldName).val(), function(ll) {
if (ll != null) {
if (options.marker != undefined) {
options.marker.setLatLng(ll);
}
self.centerTo({ lat: ll.lat(), lng: ll.lng(), zoom: self.defaultGeocodeZoom });
updateFormFieldsOptions.lat = ll.lat();
updateFormFieldsOptions.lng = ll.lng();
self.updateFormFields(updateFormFieldsOptions);
}
});
});
};
TGMap.prototype.drawTrack = function(tourid) {
var self = this;
$.getJSON('/tours/track/' + tourid + '.json', function(data, textStatus) {
// getting TGMap scope
$this = self;
// drawing track
var track = new JSONParser(data).getGMapOverlay();
$this.map.addOverlay(track.polyline);
$this.zoomToBoundsPolyline(track.polyline);
});
};
TGMap.prototype.zoomToBoundsPoints = function() {
this.map.setCenter(this.points.getBounds().getCenter(), this.map.getBoundsZoomLevel(this.points.getBounds()));
};
TGMap.prototype.zoomToBoundsPolyline = function(polyline) {
this.map.setCenter(polyline.getBounds().getCenter(), this.map.getBoundsZoomLevel(polyline.getBounds()));
};
TGMap.prototype.zoomToDefault = function() {
this.map.setCenter(new GLatLng(this.defaultLat, this.defaultLng), this.defaultZoom);
};
TGMap.prototype.centerTo = function(options) {
this.map.setCenter(new GLatLng(options.lat, options.lng), options.zoom);
};
function TGPoints() {
this.points = [];
this.bounds = new GLatLngBounds();
};
TGPoints.prototype.add = function(latlng) {
this.points.push(latlng);
this.bounds.extend(latlng);
};
TGPoints.prototype.getPoints = function() {
return this.points;
};
TGPoints.prototype.getBounds = function() {
return this.bounds;
};
function JSONParser(json) {
this.json = json;
};
JSONParser.prototype.getGMapOverlay = function() {
var jdata = this.json;
var points = [];
var track_starting_point = null;
var track_finish_point = null;
var trackwidth = 4;
var trackcolor = "#FF0080";
if (jdata.tracks.color != undefined) {
trackcolor = jdata.tracks.color;
}
if (jdata.tracks.width != undefined) {
trackwidth = jdata.tracks.width;
}
if (jdata.tracks.points != undefined) {
track_starting_point = new GLatLng(parseFloat(jdata.tracks.points[0].lat), parseFloat(jdata.tracks.points[0].lng));
track_finish_point = new GLatLng(parseFloat(jdata.tracks.points[jdata.tracks.points.length - 1].lat), parseFloat(jdata.tracks.points[jdata.tracks.points.length - 1].lng));
for (var j = 0; j < jdata.tracks.points.length; j++) {
points[j] = new GLatLng(parseFloat(jdata.tracks.points[j].lat), parseFloat(jdata.tracks.points[j].lng));
}
}
return {
polyline: new GPolyline(points, trackcolor, trackwidth, 0.66),
track_starting_point: track_starting_point,
track_finish_point: track_finish_point
};
};