Join Now
Quality Rating:
  • Currently 2.0 / 5
(2.0 / 5 - 1 vote cast)
Expertise Level:
  • Currently 3.0 / 5
(3.0 / 5 - 1 vote cast)

This page was last modified 04:56, 15 April 2008.

How to calculate distance between two landmarks

From Forum Nokia Wiki

This example demonstrates:

  • how to chose landmark from database
  • how to calculate the distance between two landmarks, using own algorithm

There are used following header files:

#include <clmklandmarkselectordlg.h>  
#include <tlmkitemiddbcombiinfo.h>
#include <epos_cposlandmarkdatabase.h>
 
#include <e32math.h> // class Math

MMP-file must include following libraries:

LIBRARY lmkcommonui.lib 
LIBRARY eposlandmarks.lib 
LIBRARY lbs.lib

Class TLocality contains own methods (TLocality::Distance) for calculating distance between two landmarks - it is preferable to use own methods. But in some situations, You may need own algorithm to calculate distance. The following formula can be used for calculating the distance between two points on a sphere surface.

Haversinus formula:

Image:HaversinusFormula.PNG

The final distance can be calculated after multiplying angle to the radius of the sphere (radius of the Earth).

The calculation of the distance according to the formula given above:

const TInt KEarthRadius = 6372795; // in meters
TReal64 YourClassName :: CalcDistance( TReal64 aLat1, TReal64 aLong1, 
                                       TReal64 aLat2, TReal64 aLong2 )
{
   TReal64 result = -1,
           lat1Sin, lat2Sin, lat1Cos, lat2Cos, deltaLongCos, deltaLongSin;
 
   if( Math :: Sin( lat1Sin,    aLat1 * KPi / 180) == KErrNone &&
       Math :: Sin( lat2Sin,    aLat2 * KPi / 180) == KErrNone &&
       Math :: Cos( lat1Cos,    aLat1 * KPi / 180) == KErrNone &&
       Math :: Cos( lat2Cos,    aLat2 * KPi / 180) == KErrNone &&
       Math :: Cos( deltaLongCos, ( aLong2 - aLong1 ) * KPi / 180 ) == KErrNone &&
       Math :: Sin( deltaLongSin, ( aLong2 - aLong1 ) * KPi / 180 ) == KErrNone )
   {
           TReal64 a = lat2Cos * deltaLongSin,
	           b = lat1Cos * lat2Sin - lat1Sin * lat2Cos * deltaLongCos,
	           c = lat1Sin * lat2Sin,
	           d = lat1Cos * lat2Cos * deltaLongCos,
	           e = a * a + b * b, 
	           f;
	   if( Math :: Sqrt( f, e  ) == KErrNone )
	   {
		   e = f / ( c + d );
		   if( Math :: ATan( f, e ) == KErrNone ) 
		   {
		      while( f < 0 )
		         f += KPi;
		      result = f * KEarthRadius;
		   }
	   }	           
   }
   return result;
}

The following function shows how to select one landmark:

TInt YourClassName::SelectOneLandmark(TLmkItemIdDbCombiInfo& aItem)
{
   // this dialog allows to select landmark
   CLmkLandmarkSelectorDlg* selectLandmarkDlg = CLmkLandmarkSelectorDlg::NewL();
   // dlg is deleted automatically
   return selectLandmarkDlg->ExecuteLD(aItem); 	
}

The following code fragment shows how to select two landmarks and calculate distance between them:

TLmkItemIdDbCombiInfo item, item2;
if( SelectOneLandmark( item ) != 0 && SelectOneLandmark( item2 ) != 0 )
{
   // get item ID
   TPosLmItemId itemId  = item.GetItemId(), 
                item2Id = item2.GetItemId();                   
 
   // get item info
   CPosLandmark *lmItemPos  = item.GetLmDb()->ReadLandmarkLC( itemId ),
                *lmItem2Pos = item2.GetLmDb()->ReadLandmarkLC( item2Id );
				        
   // reading item position
   TLocality pos1, pos2;   
   if( lmItemPos->GetPosition(pos1) == KErrNone && 
       lmItem2Pos->GetPosition(pos2) == KErrNone )
   {
	  // calc. distance
      TReal64 dist = CalcDistance( pos1.Latitude(), pos1.Longitude(), 
                                   pos2.Latitude(), pos2.Longitude() );
      if( dist != -1 )
      {
         TRealFormat format( 16,4 );
         TBuf<64> buf;
         _LIT( KDist, "Distance is:\n" );
         buf.Append( KDist );
         buf.AppendNum(dist, format);
				            
         CAknInformationNote* note = new (ELeave) CAknInformationNote;
         note->ExecuteLD(buf);                			   
      }                		   
   }                                                         
}
delete item.GetLmDb();
delete item2.GetLmDb();
Related Discussions
Thread Thread Starter Forum Replies Last Post
Should I really be using a form? Enfors Python 10 2007-08-03 14:28
Image size basarim General Symbian C++ 1 2004-02-24 08:30
Transmission Power of a 3650 phone for BT? asabh Bluetooth Technology 2 2003-08-20 13:12
Provisioning "data" (bookmarks, landmarks,, ...) on device without SMS or WAP Push tfrivold OMA DM/DS/CP 3 2007-10-17 11:21
How is that? SUv20 General Messaging 3 2004-01-28 08:03
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX