// Hash params manipulation url#arg0@arg1@arg2 function get_args() {return (location.hash.length<2)?[]:decodeURIComponent(location.hash.substr(1)).split('@');} function set_args(args) {location='#'+encodeURIComponent(args.join('@'));} function set_arg(i,val) {var a=get_args();a[i]=val;set_args(a);} function get_arg(i) {return get_args()[i];} function log(msg) { if(window._debug) GLog.write(msg); } var map = null; window.MapSearch = function() { var self = this; this.home = null; this.countryCode = 'CZ'; this.server = ''; this.params = {}; this.useRoadDistances = true; this.animate = null; this.hashArgNum = 0; this.homeMarker = null; this.stdMarker = null; this.map = map; this.clickListener = null; // event handler used during manual selection this.onBeforeRun = null; this.onRenderPlacesFinished = null; this.places = null; this.startLatLng = new GLatLng(49.8, 15.4); this.startZoom = 7; this.text = { multiplePlacesFound : 'Nalezená místa:', searchingAddress : 'Hledám zadanou adresu', gettingData : 'Hledám nejbližší firmy', gettingRoadDistances: 'Získávám silniční vzdálenosti', btnManualSelectionSide:'Zadat místo kliknutím do mapy', addressNotFound : 'Zadané místo nebylo nalezeno.', clickInTheMap : 'Nyní vyberte místo kliknutím do mapy.', error : 'Omlouváme se, došle k chybě aplikace.\nPokud problémy přetrvají, dejte nám prosím vedět na adresu info@betonserver.cz.\nKód chyby: ' }; this.init = function () { if (GBrowserIsCompatible()) { self.map = new GMap2(document.getElementById("ms_map")); self.map.addControl(new GMapTypeControl()); self.map.addControl(new GSmallMapControl()); self.map.setCenter(self.startLatLng, self.startZoom); self.homeMarker = new GIcon(G_DEFAULT_ICON); self.homeMarker.iconSize = new GSize(32,32); self.homeMarker.shadowSize = new GSize(59, 32); self.homeMarker.iconAnchor = new GPoint(16, 31); self.homeMarker.image = 'http://www.google.com/mapfiles/ms/micons/rangerstation.png'; self.homeMarker.shadow= 'http://www.google.com/mapfiles/ms/micons/rangerstation.shadow.png'; self.stdMarker = new GIcon(G_DEFAULT_ICON); self.stdMarker.shadow = "http://www.google.com/mapfiles/shadow50.png"; self.stdMarker.iconSize = new GSize(20, 34); self.stdMarker.shadowSize = new GSize(37, 34); self.stdMarker.iconAnchor = new GPoint(9, 34); self.stdMarker.infoWindowAnchor = new GPoint(9, 34); self.animate = ! (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6); var arg = get_arg(self.hashArgNum); if(arg=='manual') { self.manualSelection(); } else if(arg) { $('#ms_fld').val(arg); self.run(); } } } this.setStatus = function(text) { self.clearRes('ms_Status'); $('#ms_res').text(this.text[text]); } this.clearRes = function(classname) { $('#ms_res').html(''); $('#ms_res').removeClass(); $('.ms_showWhenListReady').hide(); if(classname) $('#ms_res').addClass(classname); } this.onerror = function(code) { self.clearRes(); alert(self.text.error + code) } /* Place * html (includes img.icon and span.dist) * lat * lng * markerTitle * infoWindowHtml * * markerImg * markerImgSel * */ this.renderPlaces = function(places) { log('MS.renderPlaces'); self.places = places; self.clearRes('ms_Results'); var bounds = new GLatLngBounds(); bounds.extend(self.home); $('#ms_res').hide(); for(var i=0;i'); div.attr('id','ms_listitem_'+a.id); a.listDiv = div; div.html(a.html); // init and store icon var img = $('img.ms_icon',div).attr('src',a.markerImg); a.listIcon = img; // update road distance if applicable var distSpan = $('span.ms_distance',div); if(a.route && distSpan) { var title = $('').html(a.route.getSummaryHtml()).text(); // strip html distSpan.html(a.route.getDistance().html).attr('title',title); } $('#ms_res').append(div); div.bind('mouseover',a,function(e) {highlight(e.data)}); div.bind('mouseout',a,function(e) {unhighlight(e.data)}); // 3. ADD TO MAP var latlng = new GLatLng(a.lat,a.lng); bounds.extend(latlng); var markerIcon = new GIcon(self.stdMarker); markerIcon.image = a.markerImg; var marker = new GMarker(latlng,{title:a.markerTitle, icon:markerIcon}) a.marker = marker; if(a.infoWindowHtml) marker.bindInfoWindowHtml(a.infoWindowHtml,{maxWidth:350}); self.map.addOverlay(marker); GEvent.bind(marker,'mouseover',a,function() {highlight(this)}); GEvent.bind(marker,'mouseout',a,function() {unhighlight(this)}); } var zoom = self.map.getBoundsZoomLevel(bounds); var center = bounds.getCenter(); self.map.setCenter(center,zoom); if(this.animate) $('#ms_res,.ms_showWhenListReady').fadeIn(700,function(){$('#ms_res,.ms_showWhenListReady').removeAttr("filter");} /* IE7 bugfix to reenable ClearType*/); else $('#ms_res,.ms_showWhenListReady').show(); if(self.onRenderPlacesFinished) self.onRenderPlacesFinished(); } this.getRoadDistances = function(places) { log('MS.getRoadDistances'); var router = new MultiRoutes(); var from = self.home.lat() + ' ' + self.home.lng(); var to = $.map(places, function(place) {return place.lat+' '+place.lng; } ); var onload = function(routes) { log('MS.getRoadDistances.onload'); for(var i=0;i').attr('value',self.text.btnManualSelectionSide).bind('click',self.manualSelection); $('#ms_res').append($('

').append(btn)); } this.onGeocodingFinishedSingle = function(place) { log('MS.onGeocodingFinishedSingle'); var point = new GLatLng(place.Point.coordinates[1],place.Point.coordinates[0]); var marker = new GMarker(point,{icon:self.homeMarker}); self.map.clearOverlays(); self.map.addOverlay(marker); self.startDownload(point); } this.onGeocodingFinishedMultiple = function(places) { log('MS.onGeocodingFinishedMultiple'); self.clearRes('ms_Multiple'); $('#ms_res').html('

'+self.text.multiplePlacesFound+'

'); self.map.clearOverlays(); for(var i=0;i').html(letter + '. ' + address); a.bind('click',{place:places[i]},function(e) {onclick(e.data.place); return false;}); $('#ms_res').append( a ); $('#ms_res').append('
'); } // manual selection button var btn = $('').attr('value',self.text.btnManualSelectionSide).bind('click',self.manualSelection); $('#ms_res').append($('

').append(btn)); //map.setCenter(point, 6); } this.manualSelectionEnd = function() { if(self.clickListener != null) GEvent.removeListener(self.clickListener); } this.manualSelection = function() { if(self.onBeforeRun) self.onBeforeRun(); self.clearRes('ms_Manual'); $('#ms_res').html(self.text.clickInTheMap); self.map.clearOverlays(); self.clickListener = GEvent.addListener(self.map, "click", function(marker, point) { place = { Point : {coordinates: [point.lng(),point.lat() ] }}; self.manualSelectionEnd(); var s = point.lat() + ' ' + point.lng(); $('#ms_fld').val(s); set_arg(self.hashArgNum, s); self.onGeocodingFinishedSingle(place); /* if (marker) { self.map.removeOverlay(marker); } else { self.map.addOverlay(new GMarker(point,{icon:new GIcon(self.homeMarker)})); } */ }); } this.run = function() { if(self.onBeforeRun) self.onBeforeRun(); var s = $('#ms_fld').val(); if(s=='') return false; /* nebo hlaska: musite neco zadat */ if(s=='debug') { window._debug = true; log('Debugging mode initialized.'); return false; } log('MS.run'); self.manualSelectionEnd(); self.setStatus('searchingAddress') // Initialize map for next search self.map.clearOverlays(); self.map.setCenter(self.startLatLng, 7); // Run geocoding var geocoder = new Geocoder(); geocoder.onerror = self.onerror; geocoder.countryCode = this.countryCode; set_arg(self.hashArgNum, s); // coordinates - don't geocode var r = /(\d+\.\d+)\s(\d+\.\d+)/.exec(s); if(r) { self.onGeocodingFinishedSingle( { Point : {coordinates: [r[2],r[1] ] } } ); return; } geocoder.onFinishedSingle = this.onGeocodingFinishedSingle; geocoder.onFinishedMultiple = this.onGeocodingFinishedMultiple; geocoder.onFinishedNone = this.onGeocodingFinishedNone; geocoder.run(s); } } /** Geocoder - uses Google geocoding and filters the result to a particular country * */ function Geocoder() { this.countryCode = 'CZ'; this.appendCC = false; this.setBC = true; this.onFinishedSingle = null; this.onFinishedMultiple = null; this.onFinishedNone = null; this.onerror = null; this.address = ''; var self = this; this.onload = function(response) { log('GeoCoder.onload'); var j=0;places=[]; for(var i in response.Placemark) if(response.Placemark[i].AddressDetails.Country && response.Placemark[i].AddressDetails.Country.CountryNameCode==self.countryCode) places[j++] = response.Placemark[i]; if (response.Status.code != 200 || places.length == 0) { if(response.Status.code == 200 && !self.setBC) { self.setBC = true; self.run(self.address); } else if (response.Status.code == 200 && !self.appendCC) { self.appendCC = true; self.run(self.address); } else if (response.Status.code == 200 || response.Status.code == 602) { self.onFinishedNone(); } else self.onerror("GEO"+response.Status.code); } else if(places.length == 1) { self.onFinishedSingle(places[0]); } else { self.onFinishedMultiple(places); } } this.run = function(address) { this.address = address; var geocoder = new GClientGeocoder(); geocoder.reset(); if(this.setBC) geocoder.setBaseCountryCode(this.countryCode); if(this.appendCC) address += ', '+this.countryCode; log('Gecoding: '+address); //alert(address); geocoder.getLocations(address, this.onload); } } /** MultiRoutes - load multiple routes from one point to multiple points with * min requests possible * * routes = new MultiRoutes(); * routes.timeout: int // max alowed time before throwing onerror, default 5000 * routes.onerror: function(msg) // error handler, default alert(msg) * routes.getRoutes(string from, string[] to, function(GRoute[] routes) onload) */ function MultiRoutes() { this.onload = null; this.onerror = function(msg) { alert('Error while roaddist: '+msg); }; this.gdir = []; this.result = []; this.finished = -1; // number of finished requests this.qlen = 12; // max query length this.timeout = 5000; this.timeoutId = null; var me = this; // gdir's load event handler this.GDirLoad = function(gdir) { var html = '


'; j=0; for(i=0;i