API Docs for: 1.0.0
Show:

File: js/map.js

/**
 * Wrapper class used to exchange feature objects between the application and the imajnet plugin
 * The feature wrapper must ensure correct access to unique feature identifier through the getFeatureId method
 * 
 * @class FeatureWrapper 
 */
FeatureWrapper.prototype.getFeatureId = function() {
	return this._leaftlet_id.id;
};


/**
 * Imajnet SDK integration example in an Leaflet based application
 *
 * <a href="http://imajnet.net/ImajnetJsDoc/ImajnetJsIntro.html">Imajnet plugin integration</a>
 * @module Imajnet Integration Demo
 * @class ImajnetIntegrationDemo
 * @main ImajnetIntegrationDemo
 */

var jQ = null;
var isLeaflet = true;
var map = null;
var mapScale = null;
var scales = new Array(55467893.20400156, 27733946.60200078, 13866973.30100039, 6933486.650500195, 3466743.3252500976, 1733371.6626250488, 866685.8313125244, 433342.9156562622, 216671.4578281311, 108335.72891406555, 54167.864457032774, 27083.932228516387, 13541.966114258194, 6770.983057129097, 3385.4915285645484, 1692.7457642822742);
//var positionsMarkers = new Array();



	

/**
* Returns the current map scale
*
* @method getMapScale
* @return {Boolean} Returns the current map scale;
*/
function getMapScale() {
	console.warn('getMapScale not implemented');
	return scales[map.getZoom() - 3];
}

/**
* Sets the zIndex for a given map layer
*
* @method setLayerZIndex
* @param {Layer} layer The layer for which to set the zIndex
* @param {zIndex} zIndex The zIndex of the layer
*/
function setLayerZIndex(layer, zIndex) {
	//console.warn('setLayerZIndex not implemented');
	if(typeof layer.setZIndexOffset === 'function') {
		layer.setZIndexOffset(zIndex);
		return;
	}
	
//	if(layer.name == 'Photogrammetry positions') {
//		for(var i = 0, length = positionsMarkers.length; i < length; ++i) {
//			positionsMarkers[i].setZIndexOffset(zIndex);
//		}
//		return;
//	}
	
	for(var i = 0, length = layer.length; i < length; ++i) {
		layer[i].setZIndexOffset(zIndex);
	}
}

/**
* Zooms the underying map to the given zoom level
* @param zoom the desired zoom level
* @method zoomMapTo
*/
function zoomMapTo(zoom) {
	console.warn('zoomMapTo not implememnted');
}

/**
* Zooms the map on top of the feature described by the given feature wrapper
* 
* @param featureWrapper the imajnet wrapper around the feature object
* @param onlyIfNotVisible if true then the zoom operation needs to be performed only if the object is not visible on the map(is outside of the visible extent), otherwise the zoom is performed all the time
* @method zoomMapToFetaureWrapper
*/
function zoomMapToFetaureWrapper(featureWrapper, onlyIfNotVisible) {
	console.warn('zoomMapToFetaureWrapper not implememnted');
}

/**
* Removes a list of features from the given vector layer.
*
* @param vectorLayer the layer from which the objects need to be removed
* @param featuresWrapper the array of features to be removed. The features are wrapped inside imajnet FeatureWrapper objects.
* @method removeFeatures
*/
function removeFeatures(vectorLayer, featuresWrapper) {
	for(var i in vectorLayer._layers) {
		for(var j = 0; j < featuresWrapper.length; j++) {
			if(vectorLayer._layers[i] && vectorLayer._layers[i].feature.geometry == featuresWrapper[j].feature.geometry) {
				map.removeLayer(vectorLayer._layers[i]);
				delete vectorLayer._layers[i];
			}
		}
	}
	//console.warn('removeFeatures function not implemented');
}

/**
* Sets the feature color for a given feature. Used for selection/unselection and highlight of features.
* 
* @param vectorLayer the layer to which the feature belongs to
* @param featureWrapper the feature concerned by the operation
* @param color the desired color
* @method setFeatureColor
*/
function setFeatureColor(vectorLayer, featureWrapper, color) {
	console.error('Set feature color not implememnted');
}

/**
* Returns the current map zoom level.
*
* @method getCurrentZoomLevel
*/
function getCurrentZoomLevel() {
	return map.getZoom();
}

/**
* Centers the map on the given imajnet position
* 
* @param position the imajnet position
* @param onlyIfNotVisible if true then the operation needs to be performed only if the object is not visible on the map(is outside of the visible extent), otherwise the operation is performed all the time
*
* @method centerMapToPosition
*/
function centerMapToPosition(position, onlyIfNotVisible) {
    if(onlyIfNotVisible && !map.getBounds().contains(L.latLng(position.lat, position.lon))) {
    	map.panTo(new L.LatLng(position.lat, position.lon));
    	return;
    }
	map.panTo(new L.LatLng(position.lat, position.lon));
}

/**
* Adds the imajnet tile layer to the map.
*
* @returns the added layer object
* @method addImajnetLayerToMap
*/
function addImajnetLayerToMap() {
	var imajnetLayer = L.tileLayer(ImajnetMap.getImajnetTileUrl(), {
	    layers: 'Imajnet layer',
	    format: 'image/png',
	    transparent: true
	    
	    //tms: true,
	    //crs: L.CRS.EPSG4326
	});
	map.addLayer(imajnetLayer);
	return imajnetLayer;
}

/**
* Removes a layer from the map
*
* @method removeLayerFromMap
*/
function removeLayerFromMap(layer) {
    map.removeLayer(layer); 
}

/**
* Hook callback called after the imajnet layers are added to the map. It is expected that the application will perform here any additional intialization required by its business logic.
*
* @method afterImajnetLayersAddedToMap
*/
function afterImajnetLayersAddedToMap() {
}

/**
* Adds a vector layer to the map.
* 
* @param name the name of the vector layer to be created and added to the map
* @param opacity the opacity of the layer
* @param options initialization ofptions for the layer
* @method addVectorLayerToMap
*/
function addVectorLayerToMap(name, opacity, options) {
	return L.geoJson().addTo(map);
}

/**
* Adds a feature to the given vector layer
* 
* If featureOptions.type is LineString or Polygon pointsArray will be an array of objects({x, y})
* If featureOptions.type is MultiPolygon pointsArray will be an array of arrays(objects({x, y}).
* 
* @param pointsArray the points that compose the feature geometry
* @param featureOptions the parameter options for creating the feature
* @returns FeatureWrapper object that contains added feature object
* ex: featureOptions(type: 'Point, LineString, Polygon', 'Multipolygon', zIndex, fillColor: color, fillOpacity: 0.5).
* @method addFeature
*/
function addFeature(vectorLayer, pointsArray, featureOptions) {
//	var coordinates = new Array();
//	for(var i = 0, length = pointsArray; i < length; i++) {
//		coordinates.push(new Array());
//	}
	
	if(pointsArray[0] && pointsArray[0][0]) { // TODO implement multipolygon
		pointsArray = pointsArray[0];
		if(featureOptions.type == 'MultiPolygon') {
			featureOptions.type = 'Polygon';
		}
	}
	
	var points = new Array();
	for(var i = 0, length = pointsArray.length; i < length; ++i) {
		points.push(new Array(pointsArray[i].x, pointsArray[i].y));
	}
	var geojsonFeature = new Object({
	    "type": "Feature",
	    /*"properties": {
	        "name": "Coors Field",
	        "amenity": "Baseball Stadium",
	        "popupContent": "This is where the Rockies play!"
	    },*/
	    "geometry": {
	        "type": featureOptions.type,
	        "coordinates": featureOptions.type == 'LineString' ? points : new Array(points)
	    }
	}, {style: {
		weight: 20,
        opacity: 1,
        color: 'white',
		fillColor: '#ff0000'
	}});
	vectorLayer.addData(geojsonFeature);
	var featureWrapper = new FeatureWrapper();
	featureWrapper.setFeature(geojsonFeature);
	return featureWrapper;
}

/**
* Removes all the features contained by the given vector layer.
*
* @param vectorLayer the vector layer
* @method removeAllFeatures
*/
function removeAllFeatures(vectorLayer) {
	vectorLayer.clearLayers();
}

/**
* Selects a feature on the map
*
* @param vectorLayer the vector layer to which the feature belongs to
* @param featureWrapper the feature to be selected.
* @method selectFeature
*/
function selectFeature(vectorLayer, featureWrapper) {
	setFeatureColor(vectorLayer, featureWrapper, '#EE9900');
}

/**
* Unselects a map feature
*
* @param vectorLayer the vector layer to which the feature belongs to
* @param featureWrapper the feature to be un-selected.
* @method unselectFeature
*/
function unselectFeature(vectorLayer, featureWrapper) {
	setFeatureColor(vectorLayer, featureWrapper, '#EE9900');
}

/**
* Adds a marker layer to the map.
* 
* @param name the name of the layer
* @method addMarkerLayerToMap
*/
function addMarkerLayerToMap(name) {
	return new Array();
}

/**
* Adds a feature to the given marker layer
*
* @param markerLayer the layer 
* @param markerData the marker options
* @method addMarkerFeature
*/
function addMarkerFeature(markerLayer, markerData) {	
	return addMarker(markerLayer, markerData);
}

/**
* Removes a list of features(markers) from the given layer.
* 
* @param vectorLayer the layer from which the features need to be removed
* @param markersWrapper the list of markers
*
* @method removeMarkerFeatures
*/
function removeMarkerFeatures(vectorLayer, markersWrapper) {
	for(var i = 0, length = markersWrapper.length; i < length; ++i) {
		removeMarker(vectorLayer, markersWrapper[i]);
	}
}

/**
* Adds a marker to the markerLayer, 
* @param markerData object with properties: lon(wgs84 longitude), lat: wgs84 latitude, imagePath: path to the marker image, size: {width, height}, objectId: unique identifier for the marker(add this property to marker object), 
* onMouseOver handler function for mouse over, 
* onMouseOut: handler function for mouseout, onClick handler function for click event. 
*
* This function will add to map imajbox marker at the specified lon, lat coordinates. 
* Example: 
* function addMarker(markerLayer, markerData) {
* 	var marker = L.marker([markerData.lat, markerData.lon], {icon: L.icon({iconUrl: markerData.imagePath, iconSize: [markerData.size.width, markerData.size.height]})}).addTo(map);
* 	marker.objectId = markerData.objectId;	
*     marker.on('mouseover', function(event) {
* 	    markerData.onMouseOver(event, marker.objectId);
*     });
*     marker.on('mouseout', function(event) {
*     	markerData.onMouseOut(event, marker.objectId);
*     });
*     marker.on('click', function(event) {
*     	markerData.onClick(event, marker.objectId);
*     });
*    
* 	return marker;
* }
*
* @method addMarker
*/
function addMarker(markerLayer, markerData) {
	var marker = L.marker([markerData.lat, markerData.lon], {icon: L.icon({iconUrl: markerData.imagePath, iconSize: [markerData.size.width, markerData.size.height]})}).addTo(map);
    
	var featureWrapper = new FeatureWrapper();
	featureWrapper.setFeature(marker);
	
    marker.on('mouseover', function(event) {
	    markerData.onMouseOver(event, featureWrapper);
    });
    marker.on('mouseout', function(event) {
    	markerData.onMouseOut(event, featureWrapper);
    });
    marker.on('click', function(event) {
    	markerData.onClick(event, featureWrapper);
    });
//    if(markerLayer.name == 'Photogrammetry positions') {
//    	positionsMarkers.push(marker);
//    }
    if(typeof markerLayer.push === 'function') {
    	markerLayer.push(marker);
    }
	return featureWrapper;
}

/**
* 
*
* @method removeMarker
*/
function removeMarker(markerLayer, markerWrapper) {
//	var a = '';
//	return;
	for(var i = 0; i < markerLayer.length; i++) {
		if(markerLayer[i]._leaflet_id == markerWrapper.feature._leaflet_id) {
			markerLayer.splice(i, 1);
			break;
		}
	}
	map.removeLayer(markerWrapper.feature);
}

/**
* 
*
* @method removeAllMarkersFromLayer
*/
function removeAllMarkersFromLayer(markerLayer) {
	for(var i = 0; i < markerLayer.length; i++) {
		map.removeLayer(markerLayer[i]);
	}
	markerLayer = new Array();
	//map.removeLayer(markerLayer);
}

/**
* 
*
* @method setMarkerOpacity
*/
function setMarkerOpacity(markerLayer, marker, opacity) {
	for(var i in map._layers) {
		if(map._layers[i]._leaflet_id == marker.feature._leaflet_id) {
			map._layers[i].setOpacity(opacity);
		}
	}
}

/**
* 
*
* @method selectMarker
*/
function selectMarker(markerLayer, markerWrapper) {
	setMarkerOpacity(markerLayer, markerWrapper, 0.6);
}

/**
* 
*
* @method unselectMarker
*/
function unselectMarker(markerLayer, markerWrapper) {
	setMarkerOpacity(markerLayer, markerWrapper, 1);
}
//End map functions

/**
* Makes visible the imajnet image component container
* @param id the id of the html element that will contain the imajnet container
* @param width the desired width of the imajnet container
* @param height the desired height of the imajnet container
* @method showImajnetItem
*/
function showImajnetItem(id, width, height) {
	jQuery('#' + id).show();
	//$(id).MooDialog();
}

/**
* Hides the imajnet image container given by it's id
* @param id the id of the html element that contains the imajnet container
* @method hideImajnetItem
*/
function hideImajnetItem(id) {
	jQuery('#' + id).hide();
}

/**
* Callback to the application called when the imajnet plugin is activated
*
* @method onImajnetIsActive
*/
function onImajnetIsActive() {
	//activateImajnetButton();
}

/**
* Callback fired on each map click, the calls need to be forwarded to the ImajnetMap module click handler. 
* This is intended to be seen as wrapper call used to convert between the underlying mapping component event data structure and imajnet lat/lon.
* Here is where any coordinate projection is done(from map projection system to imajnet WGS84)   
* @param event the click event that contains the map coordinates, its contents need to be   
* @method onMapClick
*/
function onMapClick(event) {
	ImajnetMap.mapClickHandler({lon: event.latlng.lng, lat: event.latlng.lat});
}

/**
* 
*
* @method onDragEnd
*/
function onDragEnd(event) {}

/**
* 
*
* @method onZoomEnd
*/
function onZoomEnd(event) {
	ImajnetMap.mapZoomEndHandler();
}

/**
* Registers the imajnet map events to the underlying map component.
* @method registerMapEvents
*/
function registerMapEvents() {
	map.on('click', onMapClick);
	map.on('dragend', onDragEnd);
	map.on('zoomend', onZoomEnd);
}

/**
* Unregisters the imajnet map event handlers
*
* @method unregisterMapEvents
*/
function unregisterMapEvents() {
	map.off('click', onMapClick);
	map.off('dragend', onDragEnd);
	map.off('zoomend', onZoomEnd);
}

/**
 * Optional hook callback provided for implementing actions to execute after imajnet login is performed.
 * @method onImajnetLogin
 */
function onImajnetLogin() {
	Imajnet.activateImajnetControl(jQuery('#closestImageButton'), 'closestImage');
	jQuery('#imajnetActiveButtons').show();
}

/**
* Sets the active state for a map tool. Customizable according to the application needs, usually this is similar to a button press.
* @param container the html container of the map tool
*
* @method addActiveState
*/
function addActiveState(container) {
	container.addClass('opacity60');
}

/**
* Sets the active state for a map tool. Customizable according to the application needs, usually this is similar to a button un-press.
* @param container the html container of the map tool
*
* @method removeActiveState
*/
function removeActiveState(container) {
	container.removeClass('opacity60');
}

/**
* Optional hook callback provided for implementing actions to execute after imajnet logout is performed/plugin is deactivated.
* @method onImajnetClose
*/
function onImajnetClose() {
	jQuery('#greenHandler').hide();
	jQuery('#imajnetActiveButtons').hide();
}

/*Application functions*/
/**
* 
*
* @method onImajnetControlPressed
*/
function onImajnetControlPressed(buttonElement, controlName) {
	if(buttonElement.hasClass('opacity60')) {
		Imajnet.deactivateImajnetControl(buttonElement, controlName);
	} else {
		Imajnet.activateImajnetControl(buttonElement, controlName);
	}
}


/**
* Initializes the map component and configures imajnet.
*
* @method initMap
*/
function initMap() {
	var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
	osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
	osm = L.tileLayer(osmUrl, {maxZoom: 18, attribution: osmAttrib});

	map = L.map('map').setView([47.269690074389906, 2.0422868305621957], 12).addLayer(osm);

	mapScale = L.control.scale();
	mapScale.addTo(map);
	
	Imajnet.imajnetPath = 'ImajnetLib/';
	Imajnet.setLanguage('en');
}

/**
* Activates the imajnet plugin
*
* @method activateImajnet
*/
function activateImajnet(buttonElement) {
	if(buttonElement.hasClass('opacity30')) {
		return;
	}
	var options = {
    	serverUrl: '/service',
    	//imajnetLibraryPath: 'http://10.1.1.102/platform-web/resources/imajnet/',
    	username: 'demo',
    	password: 'demo',
    	containerId: 'imajnetContainer',
    	//language: 'en',
    	map: map,
    	activateImajnet: true,
    	metadata: 'JsSDKDemo'
    };
    Imajnet.init(options).done(function() {
    	Imajnet.activateImajnetControl(jQuery('#closestImageButton'), 'closestImage');
    	jQuery('#imajnetActiveButtons').show();
    });
    jQuery('#greenHandler').show();
    jQuery('#deactivateImajnetButton').removeClass('opacity30');
    buttonElement.addClass('opacity30');
}

/**
* Deactivates the imajnet plugin
*
* @method deactivateImajnet
*/
function deactivateImajnet(buttonElement) {
	if(buttonElement.hasClass('opacity30')) {
		return;
	}
	Imajnet.deactivateImajnet();
    jQuery('#activateImajnetButton').removeClass('opacity30');
    buttonElement.addClass('opacity30');
}