// OSRM.Marker class // + sub-classes // base class OSRM.Marker = function( label, style, position ) { this.label = label ? label : "marker"; this.position = position ? position : new L.LatLng(0,0); this.marker = new L.MouseMarker( this.position, style ); this.marker.parent = this; this.shown = false; this.hint = undefined; }; OSRM.extend( OSRM.Marker,{ show: function() { map.addLayer(this.marker); this.shown = true; }, hide: function() { map.removeLayer(this.marker); this.shown = false; }, setPosition: function( position ) { this.position = position; this.marker.setLatLng( position ); this.hint = undefined; }, getPosition: function() { return this.position; }, getLat: function() { return this.position.lat; }, getLng: function() { return this.position.lng; }, isShown: function() { return this.shown; }, centerView: function() { map.setView( new L.LatLng( this.position.lat, this.position.lng-0.02), OSRM.DEFAULTS.ZOOM_LEVEL); // dirty hack }, toString: function() { return "OSRM.Marker: \""+this.label+"\", "+this.position+")"; }, }); // highlight marker OSRM.HighlightMarker = function( label, style, position) { OSRM.HighlightMarker.prototype.base.constructor.apply( this, arguments ); this.label = label ? label : "highlight_marker"; }; OSRM.inheritFrom( OSRM.HighlightMarker, OSRM.Marker ); OSRM.extend( OSRM.HighlightMarker, { toString: function() { return "OSRM.HighlightMarker: \""+this.label+"\", "+this.position+")"; }, }); // route marker OSRM.RouteMarker = function ( label, style, position ) { OSRM.RouteMarker.prototype.base.constructor.apply( this, arguments ); this.label = label ? label : "route_marker"; this.marker.on( 'click', this.onClick ); this.marker.on( 'drag', this.onDrag ); this.marker.on( 'dragstart', this.onDragStart ); this.marker.on( 'dragend', this.onDragEnd ); }; OSRM.inheritFrom( OSRM.RouteMarker, OSRM.Marker ); OSRM.extend( OSRM.RouteMarker, { onClick: function(e) { if(!e.ctrlKey) return; for( var i=0; i this.route.length-2 ) return -1; this.route.splice(id+1,0, new OSRM.RouteMarker("via", {draggable:true,icon:OSRM.icons['marker-via']}, position)); return id+1; }, removeMarker: function(id) { if( id >= this.route.length ) return; // also remove vias if source or target are removed if( id==0 && this.route[0].label == OSRM.SOURCE_MARKER_LABEL ) this.removeVias(); else if( id == this.route.length-1 && this.route[ this.route.length-1 ].label == OSRM.TARGET_MARKER_LABEL ) { this.removeVias(); id = this.route.length-1; } this.route[id].hide(); this.route.splice(id, 1); } });