diff options
author | Christian Pointner <equinox@ffgraz.net> | 2008-12-05 13:38:31 +0000 |
---|---|---|
committer | Christian Pointner <equinox@ffgraz.net> | 2008-12-05 13:38:31 +0000 |
commit | c8e8a9404112b449a546249b6ead075feb031f82 (patch) | |
tree | 1159faae63f6b5043f3c07c80e0252cf9044872e /map.js | |
parent | 42aa8e9d62ede2e663badcbd7f9a34ae0f0f889e (diff) |
initial checkin
- geocoding works
- zoom with auto map type change works
- ruler works
Diffstat (limited to 'map.js')
-rw-r--r-- | map.js | 236 |
1 files changed, 236 insertions, 0 deletions
@@ -0,0 +1,236 @@ +var map = null; +var physicalMaxLevel; +var xmlData = null; +var geocoder = null; +var nodes = new Array(); +var links = new Array(); +var overlays = new Array(); +overlays["nodes"] = new Array(); +overlays["links"] = 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.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()); + 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<mt.length; i++) { + mt[i].getMinimumResolution = function() {return 0;} + mt[i].getMaximumResolution = function() {return 17;} + } + + geocoder = new GClientGeocoder(); + +// GDownloadUrl("data.php", onData); +} + +function checkBrowser() { + if (GBrowserIsCompatible()) { + return true; + } else { + var html = '<center><h3 style="color:red">'; + html += 'Dein Browser ist leider nicht kompatibel mit der Google Maps API!</h3>'; + html += 'Ich w�rde vorschlagen du surfst auf: ' + html += '<a href="http://www.mozilla.com/firefox/">http://www.mozilla.com/firefox/</a></center>'; + document.getElementById("map").innerHTML = html; + return false; + } +} + +function onAddOverlay(overlay) { + switch(overlay.overlaytype) { + case "node": overlays["nodes"].push(overlay); break; + case "link": overlays["links"].push(overlay); break; + case "newmarker": { + if(overlays["newmarker"]) { + map.removeOverlay(overlays["newmarker"]); + } + overlays["newmarker"] = overlay; + break; + } + case "ruler": { + if(overlays["ruler"][overlay.overlaysubtype]) { + map.removeOverlay(overlays["ruler"][overlay.overlaysubtype]); + } + overlays["ruler"][overlay.overlaysubtype] = overlay; + break; + } + } +} + +function onZoom(oldLevel, newLevel) +{ + if(newLevel > 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(fetchData); +} + +function addNewNodeText(point) { + var html = "adding location at: \n" + point; + return html; +} + +function setNewMarker(point) { + var marker = new GMarker(point,{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); + map.setZoom(16); + setNewMarker(point); + } + }); + } +} + +function RulerControl() { +} + +RulerControl.prototype = new GControl(); + +RulerControl.prototype.initialize = function(map) { + 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 = dist; + 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"][o] = null; + } + distText.data = "Entfernung messen"; + } + + var container = document.createElement("div"); + var zoomInDiv = document.createElement("div"); + this.setButtonStyle_(zoomInDiv); + container.appendChild(zoomInDiv); + distText = document.createTextNode("Entfernung messen"); + zoomInDiv.appendChild(distText); + GEvent.addDomListener(zoomInDiv, "click", function() { + for(var o in overlays["ruler"]) { + if(overlays["ruler"][o]) { + clr(); + return; + } + } + + var pnt = map.getCenter(); +// marker1 = new GMarker(pnt, {icon: makeIcon('messen'), draggable: true}); + marker1 = new GMarker(pnt, {draggable: true}); + marker1.overlaytype = "ruler"; + marker1.overlaysubtype = "marker1"; + map.addOverlay(marker1); + +// marker2 = new GMarker(pnt, {icon: makeIcon('messen'), draggable: true}); + marker2 = new GMarker(pnt, {draggable: true}); + marker2.overlaytype = "ruler"; + marker2.overlaysubtype = "marker2"; + map.addOverlay(marker2); + + GEvent.addListener(marker1, "drag", function() { measure(); }); + GEvent.addListener(marker2, "drag", function() { measure(); }); + }); + + map.getContainer().appendChild(container); + return container; +} + +RulerControl.prototype.getDefaultPosition = function() { + return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(7, 7)); +} + +RulerControl.prototype.setButtonStyle_ = function(button) { + button.style.textDecoration = "underline"; + button.style.color = "#0000cc"; + button.style.backgroundColor = "white"; + button.style.font = "small Arial"; + button.style.border = "1px solid black"; + button.style.padding = "2px"; + button.style.marginBottom = "3px"; + button.style.textAlign = "center"; + button.style.width = "10em"; + button.style.cursor = "pointer"; +} + |