mypackage_wrt/preview/script/lib/sapi/Location.js
author nithyar
Mon, 20 Sep 2010 13:47:38 +0100
changeset 61 3c5e0637d4d3
parent 42 20be4dd42b12
permissions -rwxr-xr-x
Integration plan updates, week37
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     1
/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     2
 * Location.js
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     3
 * 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     4
 * Nokia Web Runtime Service API emulation 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     5
 * WRT v1.1
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     6
 * 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     7
 * Copyright 2009 Nokia Corporation. All rights reserved.
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     8
*/
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
     9
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    10
 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    11
(function(){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    12
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    13
	var provider = 'Service.Location',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    14
		Interface = 'ILocation';
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    15
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    16
	/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    17
	 * Landmark service
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    18
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    19
	var LocationService = function(){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    20
		this.GetLocation 			= __GetLocation;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    21
		this.Trace 					= __Trace;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    22
		this.Calculate				= __Calculate;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    23
		this.CancelNotification		= __CancelNotification;		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    24
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    25
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    26
	device.implementation.extend(provider, Interface, new LocationService() );
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    27
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    28
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    29
	/******************************************************/	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    30
	/******************************************************/	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    31
	/******************************************************/	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    32
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    33
	var	context = device.implementation.context,
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    34
		_t = context._t,
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    35
		method = '',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    36
		result = false,
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    37
		DBase = null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    38
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    39
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    40
	var transactionIds = new Array();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    41
	var tTransactionId = -1;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    42
	var isTraceInProgress = false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    43
	var criteriaTrace;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    44
	var callbackTrace;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    45
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    46
	/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    47
	 * Landmarks: GetLocation
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    48
	 * @param {Object} criteria
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    49
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    50
	function __GetLocation(criteria, callback, flag){	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    51
		method = "GetLocation";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    52
		//	Async call
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    53
		flag = flag || false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    54
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    55
		if (!criteria) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    56
			criteria = new Object();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    57
		}	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    58
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    59
		if(typeof criteria.LocationInformationClass == "undefined")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    60
			criteria.LocationInformationClass = "BasicLocationInformation"; // Default value of LocationInformationClass is "BasicLocationInformation" if not provided
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    61
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    62
		var result = validateArgument('GetLocation',criteria);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    63
		if(result.ErrorCode != 0)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    64
			return result;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    65
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    66
		if (typeof callback == 'function') {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    67
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    68
			var retVal = context.callAsync(this, arguments.callee, criteria, callback,true);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    69
			transactionIds.push(retVal.TransactionID);  // all transaction ids are pushed on this variable, because CancelNotification function of SAPI doesn't take TransactioID as input
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    70
			return retVal;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    71
		}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    72
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    73
		if(flag)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    74
		{
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    75
			transactionIds.shift();  // Remove oldest TransactionID(FIFO) (Async call)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    76
		}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    77
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    78
		DBase = context.getData(provider);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    79
		var returnValue = DBase[criteria.LocationInformationClass];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    80
		locationNotify(criteria.Updateoptions);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    81
		return context.Result(returnValue);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    82
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    83
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    84
	/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    85
	 * Location: Trace
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    86
	 * @param {Object} criteria
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    87
	 * @param {Function} callback function for async call
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    88
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    89
	function __Trace(criteria, callback){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    90
		method = "Trace";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    91
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    92
		if (!criteria) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    93
			criteria = new Object();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    94
		}	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    95
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    96
		if(typeof criteria.LocationInformationClass == "undefined")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    97
			criteria.LocationInformationClass = "BasicLocationInformation"; // Default value of LocationInformationClass is "BasicLocationInformation" if not provided
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    98
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
    99
		if (typeof callback != 'function') { // callback should be valid function
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   100
			return error(device.implementation.ERR_SERVICE_NOT_SUPPORTED,msg.msgCommandNotFound); 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   101
		}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   102
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   103
		var result = validateArgument('Trace',criteria);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   104
		if(result.ErrorCode != 0)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   105
			return result;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   106
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   107
		criteriaTrace = criteria;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   108
		callbackTrace = callback;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   109
		isTraceInProgress = true;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   110
		locationNotify(criteria.Updateoptions);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   111
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   112
		return traceCall(criteria,callback);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   113
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   114
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   115
	/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   116
	 * Location: Calculate
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   117
	 * @param {Object} criteria
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   118
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   119
	function __Calculate(criteria){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   120
		method = "Calculate";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   121
		if(!criteria || !criteria.MathRequest)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   122
			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgCalcMissingMathReq);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   123
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   124
		if(typeof criteria.MathRequest != "string" || (criteria.MathRequest != "FindDistance" && criteria.MathRequest != "FindBearingTo" && criteria.MathRequest != "MoveCoordinates")) // Error check for wrong MathRequest criteria
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   125
			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgCalcWrongTypeMathReq);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   126
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   127
		if(typeof criteria.DistanceParamSource != "object" || (typeof criteria.DistanceParamSource.Latitude != "number" || typeof criteria.DistanceParamSource.Longitude != "number" || typeof criteria.DistanceParamSource.Altitude != "number"))
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   128
			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgCalcMissingArgLocCord);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   129
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   130
		if(criteria.MathRequest == "FindDistance" || criteria.MathRequest == "FindBearingTo")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   131
		{
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   132
			if(typeof criteria.DistanceParamSource != "object" || (typeof criteria.DistanceParamDestination.Latitude != "number" || typeof criteria.DistanceParamDestination.Longitude != "number" || typeof criteria.DistanceParamDestination.Altitude != "number"))
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   133
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgCalcMissingArgLocCord);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   134
			if (criteria.MathRequest == "FindDistance") {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   135
				var dist = LatLon.distHaversine(criteria.DistanceParamDestination.Latitude, criteria.DistanceParamDestination.Longitude, criteria.DistanceParamSource.Latitude, criteria.DistanceParamSource.Longitude)*1000;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   136
				if (typeof criteria.DistanceParamDestination.Altitude == "number" && typeof criteria.DistanceParamSource.Altitude == "number") {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   137
					var delta = criteria.DistanceParamDestination.Altitude - criteria.DistanceParamSource.Altitude
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   138
					dist = Math.sqrt(dist * dist + delta * delta);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   139
				}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   140
				return context.Result(dist);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   141
			}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   142
			else if (criteria.MathRequest == "FindBearingTo"){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   143
				var bearing = LatLon.bearing( criteria.DistanceParamSource.Latitude, criteria.DistanceParamSource.Longitude,criteria.DistanceParamDestination.Latitude, criteria.DistanceParamDestination.Longitude);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   144
				return context.Result(bearing);				
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   145
			}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   146
		}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   147
		else if(criteria.MathRequest == "MoveCoordinates"){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   148
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   149
			if(typeof criteria.MoveByThisDistance == "undefined")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   150
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgCalcMissingArgMoveDist);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   151
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   152
			if(typeof criteria.MoveByThisBearing == "undefined")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   153
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgCalcMissingArgMoveBear);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   154
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   155
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   156
			if(typeof criteria.MoveByThisDistance != "number")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   157
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgCalcWrongTypeMoveDist);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   158
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   159
			if(typeof criteria.MoveByThisBearing != "number")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   160
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgCalcWrongTypeMoveBear);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   161
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   162
			var latLon = new LatLon(criteria.DistanceParamSource.Latitude, criteria.DistanceParamSource.Longitude);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   163
			var dlatLon = latLon.destPoint(criteria.MoveByThisBearing, criteria.MoveByThisDistance/1000);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   164
			var retVal = new Object();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   165
			retVal.Longitude = dlatLon.lon;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   166
			retVal.Latitude = dlatLon.lat;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   167
			retVal.Altitude = criteria.DistanceParamSource.Altitude;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   168
			return context.Result(retVal);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   169
		}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   170
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   171
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   172
	/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   173
	 * Location: CancelNotification
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   174
	 * @param {Object} criteria
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   175
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   176
	function __CancelNotification(criteria){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   177
		method = "Cancel";
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   178
		if(!criteria)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   179
				return error(device.implementation.ERR_MISSING_ARGUMENT,msg.msgCancelMissingType);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   180
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   181
		var arr = new Array();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   182
		var i = 0;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   183
		var key
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   184
		for(key in criteria);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   185
			arr[i++] = key;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   186
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   187
		if(!criteria.CancelRequestType && arr.length)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   188
				return error(device.implementation.ERR_NOT_FOUND,msg.msgCancelMissingType);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   189
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   190
		if(criteria.CancelRequestType != "GetLocCancel" && criteria.CancelRequestType != "TraceCancel")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   191
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgCancelWrongType);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   192
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   193
		if (criteria.CancelRequestType == "GetLocCancel") {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   194
			for (var i = 0; i < transactionIds.length; i++) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   195
				clearTimeout(transactionIds[i])
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   196
			}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   197
		}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   198
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   199
		if (criteria.CancelRequestType == "TraceCancel")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   200
		{
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   201
			isTraceInProgress = false;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   202
			tTransactionId = -1;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   203
		}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   204
		return context.ErrorResult(device.implementation.ERR_SUCCESS);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   205
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   206
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   207
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   208
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   209
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   210
	/*******************************
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   211
	 * helper functions
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   212
	 *******************************/
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   213
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   214
	/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   215
	 * Location: traceCall
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   216
	 * @param {} 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   217
	 * This function emulates repetitive trace calls,It calls specified callback function after every UpdateInterval untill 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   218
	 * CancelNotification is called
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   219
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   220
	function traceCall(){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   221
		var tid = setTimeout(function(){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   222
		if(!isTraceInProgress)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   223
			return;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   224
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   225
		DBase = context.getData(provider);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   226
		var returnValue = DBase[criteriaTrace.LocationInformationClass];
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   227
		var result,
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   228
			eventCode = {completed:2, error:4, progress:9},
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   229
		code = eventCode.completed;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   230
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   231
		callbackTrace(tTransactionId,code,context.Result(returnValue,0));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   232
		traceCall();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   233
		}, criteriaTrace.Updateoptions.UpdateInterval/1000);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   234
		if(tTransactionId == -1)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   235
			tTransactionId = tid;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   236
		return context.AsyncResult(tTransactionId);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   237
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   238
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   239
	/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   240
	 * Location: validateArgument
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   241
	 * @param {string,object} callingMethod and criteria
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   242
	 * Validates arguments
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   243
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   244
	function validateArgument(fun,criteria)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   245
	{
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   246
		method = fun;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   247
		if(typeof criteria.Updateoptions != "undefined")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   248
		{
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   249
			if(typeof criteria.Updateoptions != "object") // Checking for error in UpdateOptions criteria
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   250
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgGetLocationBadArg);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   251
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   252
			if(typeof criteria.Updateoptions.UpdateInterval != "undefined" && typeof criteria.Updateoptions.UpdateInterval != "number")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   253
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgGetLocationWrongType);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   254
			
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   255
			if(typeof criteria.Updateoptions.UpdateTimeOut != "undefined" && typeof criteria.Updateoptions.UpdateTimeOut != "number")	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   256
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgGetLocationWrongType);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   257
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   258
			if(typeof criteria.Updateoptions.UpdateMaxAge != "undefined" && typeof criteria.Updateoptions.UpdateMaxAge != "number")	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   259
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgGetLocationWrongType);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   260
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   261
			if(typeof criteria.Updateoptions.PartialUpdates != "undefined" && typeof criteria.Updateoptions.PartialUpdates != "boolean")	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   262
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgGetLocationWrongType);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   263
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   264
			if((typeof criteria.Updateoptions.UpdateInterval != "undefined" && criteria.Updateoptions.UpdateInterval  < 0) || 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   265
					(typeof criteria.Updateoptions.UpdateTimeOut != "undefined" && criteria.Updateoptions.UpdateTimeOut  < 0) ||
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   266
					(typeof criteria.Updateoptions.UpdateMaxAge != "undefined" && criteria.Updateoptions.UpdateMaxAge  < 0))
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   267
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgGetLocationNegInt);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   268
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   269
			if(typeof criteria.Updateoptions.UpdateTimeOut != "undefined" && typeof criteria.Updateoptions.UpdateInterval != "undefined" && criteria.Updateoptions.UpdateInterval > criteria.Updateoptions.UpdateTimeOut)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   270
			{
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   271
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgNone);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   272
			}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   273
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   274
			/*if((typeof criteria.Updateoptions.UpdateTimeOut != "undefined" && criteria.Updateoptions.UpdateTimeOut <= 1000000))// || (typeof criteria.Updateoptions.UpdateInterval != "undefined" && criteria.Updateoptions.UpdateInterval <= 1000000))
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   275
			{
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   276
				return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgNone);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   277
			}*/				
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   278
		}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   279
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   280
		if(typeof criteria.LocationInformationClass != "undefined" && criteria.LocationInformationClass != "BasicLocationInformation" && criteria.LocationInformationClass != "GenericLocationInfo") // checking for errors in LocationInformationClass criteria
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   281
			return error(device.implementation.ERR_BAD_ARGUMENT_TYPE,msg.msgGetLocationWrongCat);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   282
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   283
		if (/^Trace$/i.test(fun)&&(!criteria.Updateoptions || typeof criteria.Updateoptions.UpdateInterval == "undefined")) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   284
			if(!criteria.Updateoptions)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   285
			{
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   286
				criteria.Updateoptions = new Object();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   287
			}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   288
			criteria.Updateoptions.UpdateInterval = 1000000;  // Emulation only!! for convenience UpdateInterval is set to 1 second is not specified or if it less than 1 second
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   289
			context.notify("Using default UpdateInterval(1000000 micro seconds)"); 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   290
		}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   291
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   292
		return context.ErrorResult(device.implementation.ERR_SUCCESS, "");
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   293
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   294
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   295
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   296
	/**
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   297
	 * Location: error
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   298
	 * @param {number,string} ErrorCode and ErrorString
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   299
	 * Replaces Error String with method name
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   300
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   301
	function error(code, msg /*, args...*/){
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   302
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   303
		var args = ['location',method].concat([].slice.call(arguments,2));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   304
		msg = msg ? _t().arg.apply(msg,args) : undefined;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   305
		return context.ErrorResult(code, msg);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   306
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   307
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   308
	function locationNotify(updateoptions) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   309
		if(!updateoptions)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   310
			return;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   311
		if(typeof updateoptions.UpdateTimeOut != "undefined")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   312
			context.notify(_t("%s:: %s : Updateoptions.UpdateTimeOut not implemented in preview").arg(provider, method));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   313
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   314
		if(typeof updateoptions.UpdateMaxAge != "undefined")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   315
			context.notify(_t("%s:: %s : Updateoptions.UpdateMaxAge not implemented in preview").arg(provider, method));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   316
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   317
		if(typeof updateoptions.PartialUpdates != "undefined")
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   318
			context.notify(_t("%s:: %s : Updateoptions.PartialUpdates not implemented in preview").arg(provider, method));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   319
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   320
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   321
	/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   322
	/*  Latitude/longitude spherical geodesy formulae & scripts (c) Chris Veness 2002-2009            */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   323
	/*	http://www.movable-type.co.uk/scripts/latlong.html											  */  
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   324
	/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   325
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   326
	/*
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   327
	 * Use Haversine formula to Calculate distance (in km) between two points specified by 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   328
	 * latitude/longitude (in numeric degrees)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   329
	 *
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   330
	 * example usage from form:
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   331
	 *   result.value = LatLon.distHaversine(lat1.value.parseDeg(), long1.value.parseDeg(), 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   332
	 *                                       lat2.value.parseDeg(), long2.value.parseDeg());
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   333
	 * where lat1, long1, lat2, long2, and result are form fields
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   334
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   335
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   336
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   337
	LatLon.distHaversine = function(lat1, lon1, lat2, lon2) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   338
	  var R = 6371; // earth's mean radius in km
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   339
	  var dLat = toRad(lat2-lat1);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   340
	  var dLon = toRad(lon2-lon1);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   341
	  lat1 = toRad(lat1), lat2 = toRad(lat2);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   342
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   343
	  var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   344
	          Math.cos(lat1) * Math.cos(lat2) * 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   345
	          Math.sin(dLon/2) * Math.sin(dLon/2);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   346
	  var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   347
	  var d = R * c;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   348
	  return d;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   349
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   350
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   351
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   352
	/*
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   353
	 * ditto using Law of Cosines
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   354
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   355
	LatLon.distCosineLaw = function(lat1, lon1, lat2, lon2) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   356
	  var R = 6371; // earth's mean radius in km
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   357
	  var d = Math.acos(Math.sin(toRad(lat1))*Math.sin(toRad(lat2)) +
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   358
	                    Math.cos(toRad(lat1))*Math.cos(toRad(lat2))*Math.cos(toRad(lon2-lon1))) * R;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   359
	  return d;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   360
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   361
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   362
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   363
	/*
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   364
	 * calculate (initial) bearing between two points
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   365
	 *   see http://williams.best.vwh.net/avform.htm#Crs
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   366
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   367
	LatLon.bearing = function(lat1, lon1, lat2, lon2) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   368
	  lat1 = toRad(lat1); lat2 = toRad(lat2);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   369
	  var dLon = toRad(lon2-lon1);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   370
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   371
	  var y = Math.sin(dLon) * Math.cos(lat2);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   372
	  var x = Math.cos(lat1)*Math.sin(lat2) -
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   373
	          Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   374
	  return toBrng(Math.atan2(y, x));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   375
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   376
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   377
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   378
	/*
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   379
	 * calculate destination point given start point, initial bearing (deg) and distance (km)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   380
	 *   see http://williams.best.vwh.net/avform.htm#LL
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   381
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   382
	LatLon.prototype.destPoint = function(brng, d) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   383
	  var R = 6371; // earth's mean radius in km
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   384
	  var lat1 = toRad(this.lat), lon1 = toRad(this.lon);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   385
	  brng = toRad(brng);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   386
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   387
	  var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) + 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   388
	                        Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   389
	  var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1), 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   390
	                               Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   391
	  lon2 = (lon2+Math.PI)%(2*Math.PI) - Math.PI;  // normalise to -180...+180
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   392
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   393
	  if (isNaN(lat2) || isNaN(lon2)) return null;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   394
	  return new LatLon(toDeg(lat2), toDeg(lon2));
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   395
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   396
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   397
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   398
	/*
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   399
	 * construct a LatLon object: arguments in numeric degrees
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   400
	 *
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   401
	 * note all LatLong methods expect & return numeric degrees (for lat/long & for bearings)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   402
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   403
	function LatLon(lat, lon) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   404
	  this.lat = lat;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   405
	  this.lon = lon;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   406
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   407
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   408
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   409
	/*
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   410
	 * represent point {lat, lon} in standard representation
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   411
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   412
	LatLon.prototype.toString = function() {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   413
	  return this.lat.toLat() + ', ' + this.lon.toLon();
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   414
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   415
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   416
	/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   417
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   418
	// extend String object with method for parsing degrees or lat/long values to numeric degrees
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   419
	//
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   420
	// this is very flexible on formats, allowing signed decimal degrees, or deg-min-sec suffixed by 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   421
	// compass direction (NSEW). A variety of separators are accepted (eg 3º 37' 09"W) or fixed-width 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   422
	// format without separators (eg 0033709W). Seconds and minutes may be omitted. (Minimal validation 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   423
	// is done).
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   424
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   425
	function parseDeg (str) {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   426
	  if (!isNaN(str)) return Number(str);                 // signed decimal degrees without NSEW
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   427
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   428
	  var degLL = str.replace(/^-/,'').replace(/[NSEW]/i,'');  // strip off any sign or compass dir'n
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   429
	  var dms = degLL.split(/[^0-9.]+/);                     // split out separate d/m/s
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   430
	  for (var i in dms) if (dms[i]=='') dms.splice(i,1);    // remove empty elements (see note below)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   431
	  switch (dms.length) {                                  // convert to decimal degrees...
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   432
	    case 3:                                              // interpret 3-part result as d/m/s
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   433
	      var deg = dms[0]/1 + dms[1]/60 + dms[2]/3600; break;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   434
	    case 2:                                              // interpret 2-part result as d/m
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   435
	      var deg = dms[0]/1 + dms[1]/60; break;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   436
	    case 1:                                              // decimal or non-separated dddmmss
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   437
	      if (/[NS]/i.test(str)) degLL = '0' + degLL;       // - normalise N/S to 3-digit degrees
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   438
	      var deg = dms[0].slice(0,3)/1 + dms[0].slice(3,5)/60 + dms[0].slice(5)/3600; break;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   439
	    default: return NaN;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   440
	  }
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   441
	  if (/^-/.test(str) || /[WS]/i.test(str)) deg = -deg; // take '-', west and south as -ve
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   442
	  return deg;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   443
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   444
	// note: whitespace at start/end will split() into empty elements (except in IE)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   445
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   446
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   447
	/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   448
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   449
	// extend Number object with methods for converting degrees/radians
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   450
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   451
	function toRad (deg) {  // convert degrees to radians
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   452
	  return deg * Math.PI / 180;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   453
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   454
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   455
	function toDeg (rad) {  // convert radians to degrees (signed)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   456
	  return rad * 180 / Math.PI;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   457
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   458
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   459
	function toBrng (rad) {  // convert radians to degrees (as bearing: 0...360)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   460
	  return (toDeg(rad)+360) % 360;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   461
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   462
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   463
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   464
	/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   465
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   466
	// extend Number object with methods for presenting bearings & lat/longs
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   467
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   468
	function toDMS (num) {  // convert numeric degrees to deg/min/sec
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   469
	  var d = Math.abs(num);  // (unsigned result ready for appending compass dir'n)
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   470
	  d += 1/7200;  // add ½ second for rounding
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   471
	  var deg = Math.floor(d);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   472
	  var min = Math.floor((d-deg)*60);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   473
	  var sec = Math.floor((d-deg-min/60)*3600);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   474
	  // add leading zeros if required
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   475
	  if (deg<100) deg = '0' + deg; if (deg<10) deg = '0' + deg;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   476
	  if (min<10) min = '0' + min;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   477
	  if (sec<10) sec = '0' + sec;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   478
	  return deg + '\u00B0' + min + '\u2032' + sec + '\u2033';
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   479
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   480
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   481
	function toLat (deg) {  // convert numeric degrees to deg/min/sec latitude
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   482
	  return toDMS(deg).slice(1) + (deg<0 ? 'S' : 'N');  // knock off initial '0' for lat!
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   483
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   484
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   485
	function toLon (deg) {  // convert numeric degrees to deg/min/sec longitude
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   486
	  return toDMS(deg) + (deg>0 ? 'E' : 'W');
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   487
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   488
	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   489
	function toPrecision (num,fig) {  // override toPrecision method with one which displays 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   490
	  if (num == 0) return 0;                      // trailing zeros in place of exponential notation
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   491
	  var scale = Math.ceil(Math.log(num)*Math.LOG10E);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   492
	  var mult = Math.pow(10, fig-scale);
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   493
	  return Math.round(num*mult)/mult;
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   494
	}
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   495
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   496
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   497
	/** 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   498
	 * error messages
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   499
	 * order of %s args: Service name, method name, parameter name 
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   500
	 */
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   501
	var msg = {
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   502
		msgCommandNotFound			: '%s : Command Not found',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   503
		msgGetLocationWrongCat		: '%s : %s : wrong category info should be BasicLocationInformation/GenericLocationInfo ',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   504
		msgGetLocationBadArg		: '%s : %s : BadArgument - Updateoptions',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   505
		msgGetLocationNegInt		: '%s : %s : Negative Time Interval',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   506
		msgGetLocationWrongType 	: '%s : %s : UpdateOptions Type mismatch',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   507
		msgTraceWrongCat			: '%s : %s : Invalid LocationInformationClass',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   508
		msgCalcMissingMathReq 		: '%s : %s : Missing argument - MathRequest',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   509
		msgCalcWrongTypeMathReq 	: '%s : %s : Wrong argument - MathRequest',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   510
		msgCalcMissingArgLocCord 	: '%s : %s : Missing argument - locationcoordinate',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   511
		msgCalcMissingArgMoveDist 	: '%s : %s : Missing argument - MoveByThisDistance',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   512
		msgCalcMissingArgMoveBear 	: '%s : %s : Missing argument - MoveByThisBearing',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   513
		msgCalcWrongTypeMoveDist  	: '%s : %s : TypeMismatch - MoveByThisDistance',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   514
		msgCalcWrongTypeMoveBear 	: '%s : %s : TypeMismatch - MoveByThisBearing',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   515
		msgCancelBadArg 			: '%s : %s : BadArgument – cancel type',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   516
		msgCancelMissingType 		: '%s : %s : Missing cancel type',
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   517
		msgCancelWrongType 			: '%s : %s : Wrong cancel type'	,
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   518
		msgNone						: ''	
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   519
	};
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   520
		
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   521
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   522
}) ()
20be4dd42b12 My package widget code
victorp@symbian.org
parents:
diff changeset
   523