var map = null; var physicalMaxLevel; var xmlData = null; var geocoder = null; var overlays = new Array(); overlays["newmarker"] = null; overlays["ruler"] = new Array(); function initialize(lat, lng, res) { if (!checkBrowser()) { return; } map = new GMap2(document.getElementById("map")); map.setCenter(new GLatLng(lat, lng), res); map.disableDoubleClickZoom(); map.enableScrollWheelZoom(); map.removeMapType(G_HYBRID_MAP); map.removeMapType(G_SATELLITE_MAP); map.addMapType(G_PHYSICAL_MAP); map.setMapType(G_PHYSICAL_MAP); physicalMaxLevel = map.getCurrentMapType().getMaximumResolution(); if(res > physicalMaxLevel) { map.setMapType(G_NORMAL_MAP); map.setZoom(res); } map.addControl(new GSmallMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,20))); map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(50,10))); map.addControl(new RulerControl()); GEvent.addListener(map, "addoverlay", onAddOverlay); GEvent.addListener(map, "zoomend", onZoom); GEvent.addListener(map, "click", onMapClick); var mt = map.getMapTypes(); for (var i=0; i physicalMaxLevel) { map.setMapType(G_NORMAL_MAP); } } function onMapClick(overlay, point) { if(!overlay) { setNewMarker(point); } } function onData(data, responseCode) { if (responseCode < 200 || responseCode > 299) { return alert("Fehler: Konnte die Daten nicht laden!\n(Server Antwort-Code: " + responseCode + ")"); } xmlData = GXml.parse(data); // TODO: add data import and comment in GDownloadUrl() at initialize() } function addNewNodeText(point) { var html = "adding location at:
\n" + point; return html; } function makeIcon(type) { var icon = new GIcon(); switch(type) { case 'ruler': icon.image = "./img/marker_blue.png"; break; case 'newnode': icon.image = "./img/marker_darkred.png"; } icon.iconSize = new GSize(12,20); icon.iconAnchor = new GPoint(6,20); icon.infoWindowAnchor = new GPoint(5,1); return icon } function setNewMarker(point) { var marker = new GMarker(point,{icon: makeIcon('newnode'), draggable: true}); GEvent.addListener(marker, "dragstart", function() { this.autoclosed = true; this.closeInfoWindow(); }); GEvent.addListener(marker, "dragend", function(point) { this.openInfoWindowHtml(addNewNodeText(point)); }); GEvent.addListener(marker, "infowindowclose", function () { if(this.autoclosed == true) { this.autoclosed = false; } else { map.removeOverlay(overlays["newmarker"]); overlays["newmarker"] = null; } }); marker.overlaytype = "newmarker"; map.addOverlay(marker); marker.openInfoWindowHtml(addNewNodeText(point)); } function showAddress(address) { if (geocoder) { address += ", Graz, Austria"; geocoder.getLatLng(address, function(point) { if (!point) { alert(address + " nicht gefunden"); } else { map.setCenter(point); setNewMarker(point); } }); } } function RulerControl() { } RulerControl.prototype = new GControl(); RulerControl.prototype.initialize = function(map) { var btnText; var distBoxOuter; var distBox; var distText; function measure() { if(overlays["ruler"]["marker1"] && overlays["ruler"]["marker2"]) { var line = [overlays["ruler"]["marker1"].getPoint(), overlays["ruler"]["marker2"].getPoint()]; var dist = overlays["ruler"]["marker1"].getPoint().distanceFrom(overlays["ruler"]["marker2"].getPoint()); dist = dist.toFixed(0)+" m"; if(parseInt(dist) > 10000) { dist = (parseInt(dist)/1000).toFixed(1) + " km"; } distText.data = "Entfernung: " + dist; var poly = new GPolyline(line,'#FFFF00', 8, 1) poly.overlaytype = "ruler"; poly.overlaysubtype = "line"; map.addOverlay(poly); } } function clr(){ for(var o in overlays["ruler"]) { map.removeOverlay(overlays["ruler"][o]); } overlays["ruler"] = []; distBox.style.visibility = "hidden"; btnText.data = "Entfernung messen"; } var container = document.createElement("div"); var btnDiv = document.createElement("div"); btnDiv.className = "btn"; btnDiv.id = "rulerbtn"; container.appendChild(btnDiv); btnText = document.createTextNode("Entfernung messen"); btnDiv.appendChild(btnText); GEvent.addDomListener(btnDiv, "click", function() { for(var o in overlays["ruler"]) { if(overlays["ruler"][o]) { clr(); return; } } btnText.data = "Messung beenden"; distText.data = "Entfernung: 0 m"; distBox.style.visibility = "visible"; var pnt = map.getCenter(); marker1 = new GMarker(pnt, {icon: makeIcon('ruler'), draggable: true}); marker1.overlaytype = "ruler"; marker1.overlaysubtype = "marker1"; map.addOverlay(marker1); marker2 = new GMarker(pnt, {icon: makeIcon('ruler'), draggable: true}); marker2.overlaytype = "ruler"; marker2.overlaysubtype = "marker2"; map.addOverlay(marker2); GEvent.addListener(marker1, "drag", function() { measure(); }); GEvent.addListener(marker2, "drag", function() { measure(); }); }); distBoxOuter = document.createElement("div"); distBoxOuter.id = "distboxouter"; distBox = document.createElement("div"); distBox.className = "box"; distBox.id = "distbox"; distBox.style.visibility = "hidden"; distText = document.createTextNode("Entfernung: ? m"); distBox.appendChild(distText); distBoxOuter.appendChild(distBox); document.getElementById("map").appendChild(distBoxOuter); map.getContainer().appendChild(container); return container; } RulerControl.prototype.getDefaultPosition = function() { return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(7, 7)); }