Source: widgets/SpwPosition.js

Retour à la documentation
/**
 * @class spw.widgets.SpwPosition
 */
define(["dojo/_base/declare", "spw/api/SpwBaseWidget", "dojo/dom-construct", "dojo/_base/array", "dojo/_base/lang",
        "esri/geometry/Point", "esri/SpatialReference", "spw/api/MessageManager", "dojo/i18n!./nls/SpwPosition"],
		function(declare, SpwBaseWidget, domConstruct, array, lang, Point, SpatialReference, MessageManager, labels){

	return declare("spw.widgets.SpwPosition", [SpwBaseWidget], /** @lends spw.widgets.SpwPosition.prototype */{

		/**
		 * La recherche de position a-t'elle déjà été tentée?
	     * @private
	     * @type Boolean
	     */
		_alreadyTried: false,
		labels: labels,
			
        /**
         * @constructs
         * @param config
         */
		constructor: function() {
			this.iconClass = "SpwPositionWidgetIcon";
			this.widgetTitle = this.labels.geolocalisationButtonTitle;
	    },
	    
		postCreate: function() {
			this.inherited(arguments);
		},
	
		onActivate: function(){
			this.inherited(arguments);

			if(navigator.geolocation) {
				navigator.geolocation.getCurrentPosition(
					lang.hitch(this, this.getPositionSucceed),
		        	lang.hitch(this, this.onGeolocalisationButtonError)
	        	);
			}
			else {
				MessageManager.getInstance().notifyWarning(this.labels.geolocalisationNotSupported);
			}
		},
		
		getPositionSucceed: function(position) {
			this.spwViewer.trackEvent("spw.widgets.SpwPositionWidget", "onClickGeolocalisationButton", "getCurrentPosition");
			var gsvc = this.spwViewer.get('geometryService');
			var point = new Point(position.coords.longitude, position.coords.latitude, new SpatialReference({wkid:4326}));
			gsvc.project([point], this.spwViewer.get('spatialReference'), lang.hitch(this, function(projectedPoint) {
				this.spwViewer.get('spwMap').zoomToPoint(projectedPoint[0].x, projectedPoint[0].y, 250);
			}));
    	},
		
		onDeactivate: function(){
			
		},
		
		onGeolocalisationButtonError: function(error) {
			console.warn(error);
			if(this._alreadyTried) {
				//2 tries by click. Avoid systematic errors when not ready
				this._alreadyTried = false;
				MessageManager.getInstance().notifyWarning(this.labels.geolocalisationButtonError,null,10000);
			}
			else {
				this._alreadyTried = true;
				setTimeout(this.onActivate, 1000);
			}
		}
	});
});