locationdataharvester/mylocationsengine/src/maptileinterface.cpp
branchRCL_3
changeset 17 1fc85118c3ae
equal deleted inserted replaced
16:8173571d354e 17:1fc85118c3ae
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: Maptile interface source implementation for maptile
       
    15 *              service.
       
    16 *
       
    17 */
       
    18 
       
    19 #include <EPos_CPosLandmark.h>
       
    20 #include <EPos_CPosLandmarkDatabase.h>
       
    21 #include <EPos_CPosLmItemIterator.h>
       
    22 #include <EPos_CPosLmOperation.h>
       
    23 #include <f32file.h>
       
    24 #include <bautils.h>
       
    25 #include "maptileinterface.h"
       
    26 #include "mylocationlogger.h"
       
    27 #include "mylocationsdefines.h"
       
    28 #include "lookupmaptiledb.h"
       
    29 #include <math.h>
       
    30 
       
    31 //Invalid latitude & longitude value
       
    32 const TReal KInvalidLatitudeLongitude =  200.0f;
       
    33 const TInt KStreetLvelZoom = 16;
       
    34 const TInt KCityLevelZoom = 11;
       
    35 const TInt KImagePathSize=36;
       
    36 const TInt KMapTileWidthHeight = 256 ;
       
    37 _LIT(KFileExtn, ".png");
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CMapTileInterface::NewL()
       
    41 // Creates and returns the instance of CMapTileInterface
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 CMapTileInterface* CMapTileInterface::NewL()
       
    45 {
       
    46     CMapTileInterface* self = new (ELeave) CMapTileInterface();
       
    47     CleanupStack::PushL(self);
       
    48     self->ConstructL();
       
    49     CleanupStack::Pop(self);
       
    50     return self;
       
    51 }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CMapTileInterface::~CMapTileInterface()
       
    55 // Destructor
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CMapTileInterface::~CMapTileInterface()
       
    59 {
       
    60     __TRACE_CALLSTACK;
       
    61    
       
    62     delete iFilePath;
       
    63     
       
    64     delete iMaptileGeocoder;
       
    65     
       
    66     delete iLandmark;
       
    67 }    
       
    68 
       
    69 
       
    70 // -----------------------------------------------------------------------------
       
    71 // CMapTileInterface::CMapTileInterface()
       
    72 // Constructor
       
    73 // -----------------------------------------------------------------------------
       
    74 //
       
    75 CMapTileInterface::CMapTileInterface() :
       
    76            iMaptileGeocoder( NULL ), 
       
    77            iFilePath( NULL ),
       
    78            iObserver(NULL),
       
    79            iLandmark(NULL)
       
    80 {
       
    81 }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // CMapTileInterface::ConstructL()
       
    85 // Two phase constructor
       
    86 // -----------------------------------------------------------------------------
       
    87 //
       
    88 void CMapTileInterface::ConstructL()
       
    89 {
       
    90     __TRACE_CALLSTACK;
       
    91 
       
    92     iMaptileGeocoder = CMaptileGeocoderPlugin::NewL();
       
    93     
       
    94     iMaptileGeocoder->SetMaptileGeocoderObserver( *this );
       
    95 }
       
    96 // -----------------------------------------------------------------------------
       
    97 // CMapTileInterface::GetGeoCodeFromAddressL()
       
    98 // Interface for requesting GEO fields for the given address
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 void CMapTileInterface::GetGeoCodeFromAddressL(const TDesC& aAddressDetails,
       
   102         const TDesC& aFilePath, MMapTileObserver* aObserver)
       
   103 {
       
   104     __TRACE_CALLSTACK;
       
   105     iObserver = aObserver;
       
   106     delete iFilePath;
       
   107     iFilePath = NULL;    
       
   108     iFilePath = HBufC::NewL(aFilePath.Length());
       
   109     iFilePath->Des().Copy(aFilePath);
       
   110     iStreetAvailable = EFalse;
       
   111     iMaptileGeocoder->GetCoordinateByAddressL(aAddressDetails, ESilent);
       
   112      
       
   113 }
       
   114 // -----------------------------------------------------------------------------
       
   115 // CMapTileInterface::GetGeoCodeFromAddressL()
       
   116 // Interface for requesting GEO fields for the given landmark adress
       
   117 // -----------------------------------------------------------------------------
       
   118 //
       
   119 void CMapTileInterface::GetGeoCodeFromAddressL(CPosLandmark* aLandmark,
       
   120         const TDesC& aFilePath, MMapTileObserver* aObserver)
       
   121 {
       
   122     __TRACE_CALLSTACK;
       
   123     iObserver = aObserver;
       
   124     delete iFilePath;
       
   125     iFilePath = NULL;    
       
   126     iFilePath = HBufC::NewL(aFilePath.Length());
       
   127     iFilePath->Des().Copy(aFilePath);
       
   128 
       
   129     //Reset the street level available flag
       
   130 	iStreetAvailable = EFalse;
       
   131 	
       
   132     TPtrC getStreet;
       
   133     aLandmark->GetPositionField(EPositionFieldStreet, getStreet);
       
   134 
       
   135     if (getStreet.Length() > 0)
       
   136    {
       
   137         iStreetAvailable = ETrue;
       
   138     }
       
   139     iMaptileGeocoder->GetCoordinateByAddressL(*aLandmark, ESilent);
       
   140 }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // CMapTileInterface::GetMapTileL()
       
   144 // Function for Retrieving latitude & longitude information
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 void CMapTileInterface::GetMapTileL( const TReal& aLatitude, const TReal& aLongitude)
       
   148 {
       
   149     __TRACE_CALLSTACK;//Notification to observer , to update contact db,
       
   150     TInt zoom = KCityLevelZoom;
       
   151     if (iStreetAvailable)
       
   152     {
       
   153         zoom = KStreetLvelZoom;
       
   154     }
       
   155     iStreetAvailable = EFalse;
       
   156     TMapTileParam mapTileparam(aLatitude, aLongitude, zoom, MapTileWidth,MapTileHeight );
       
   157     iMaptileGeocoder->GetMapTileByGeoCodeL( mapTileparam, *iFilePath );
       
   158 }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // CMapTileInterface::MapComplete()
       
   162 // Maptile retrievel notification
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 void CMapTileInterface::MapTileFetchingCompleted( TInt aErrCode, const TDesC& aMapTilePath )
       
   166 {
       
   167     __TRACE_CALLSTACK;
       
   168     MYLOCLOGSTRING1("MapComplete() status-%d ",aErrCode );
       
   169     iObserver->MapTilefetchingCompleted( aErrCode, aMapTilePath );
       
   170 }
       
   171 
       
   172 // ----------------------------------------------------------------------------
       
   173 // CMapTileInterface::UpdateFilePathL()
       
   174 // Converts the geocoordinate to maptile pixel coordinate and updates the
       
   175 // file path to Maptilefolder\RowCol.png
       
   176 // ----------------------------------------------------------------------------
       
   177 //
       
   178 void CMapTileInterface::UpdateFilePathL( const TReal& aLatitude, const TReal& aLongitude )
       
   179 {
       
   180     TInt iZoomLvl = KCityLevelZoom;
       
   181     if ( iStreetAvailable )
       
   182         iZoomLvl = KStreetLvelZoom;
       
   183     
       
   184     TInt totalTilesHorizontal = 1 << iZoomLvl;
       
   185     TInt totalTilesVertical = 1 << iZoomLvl;
       
   186 
       
   187     TInt totalMapWidth = totalTilesHorizontal * KMapTileWidthHeight;
       
   188     TInt totalMapHeight = totalTilesVertical * KMapTileWidthHeight;
       
   189     
       
   190     TReal pi = 3.14159265;
       
   191 
       
   192     TInt convertedx = (( aLongitude + 180.0) * totalMapWidth)
       
   193             / 360.0;
       
   194     TReal convertedtemp = log(tan(( aLatitude + 90) * pi
       
   195             / 360.0));
       
   196     int convertedy = (1 - convertedtemp / pi) * totalMapHeight / 2.0;
       
   197 
       
   198     //Get the image row,col
       
   199     TInt iMapTileImageRow = ( convertedy / 256.0 ) * 1000;
       
   200     TInt iMapTileImageCol = ( convertedx / 256.0 ) * 1000;
       
   201     
       
   202     TBuf<KImagePathSize> mImagePath;
       
   203 
       
   204     mImagePath.AppendNum(iMapTileImageRow);
       
   205     mImagePath.AppendNum(iMapTileImageCol);
       
   206     mImagePath.Append(KFileExtn);   
       
   207     
       
   208     if( iFilePath )
       
   209     {
       
   210         iFilePath = iFilePath->ReAllocL(iFilePath->Length() + mImagePath.Length() );
       
   211         iFilePath->Des().Append(mImagePath);    
       
   212     }
       
   213 }
       
   214 
       
   215 // -----------------------------------------------------------------------------
       
   216 // CMapTileInterface::GeocodeComplete()
       
   217 // Geocode completion  notification
       
   218 // -----------------------------------------------------------------------------
       
   219 //
       
   220 void CMapTileInterface::GeocodingCompleted(TInt aErrorcode,
       
   221         MAddressInfo& aAddressInfo)
       
   222 {
       
   223     __TRACE_CALLSTACK;
       
   224     TReal latitude, longitude;
       
   225     latitude=aAddressInfo.GetLatitude();
       
   226     longitude=aAddressInfo.GetLongitude();
       
   227     
       
   228     MYLOCLOGSTRING1("GeocodeCompleted() status-%d",aErrorcode);
       
   229     MYLOCLOGSTRING1("GeocodeCompleted() latitude-%f",latitude );
       
   230     MYLOCLOGSTRING1("GeocodeCompleted() longitude-%f",longitude );
       
   231 	
       
   232     if (aErrorcode == KErrNone)
       
   233     {
       
   234         if ( latitude != KInvalidLatitudeLongitude
       
   235                 && longitude != KInvalidLatitudeLongitude)
       
   236         {           
       
   237             TRAP_IGNORE( SetLandMarkDetailsL(aAddressInfo) );
       
   238 			      TRAP_IGNORE( UpdateFilePathL( latitude, longitude ) );
       
   239 	      
       
   240             iObserver->GeoCodefetchingCompleted(KErrNone,  latitude, longitude, iFilePath->Des());
       
   241 
       
   242         }
       
   243         else
       
   244         {
       
   245             iObserver->GeoCodefetchingCompleted(KErrGeneral,
       
   246 		      KInvalidLatitudeLongitude, KInvalidLatitudeLongitude, KNullDesC);
       
   247         }        
       
   248     }
       
   249     else
       
   250     {
       
   251         iObserver->GeoCodefetchingCompleted(aErrorcode,
       
   252 		       KInvalidLatitudeLongitude, KInvalidLatitudeLongitude, KNullDesC );
       
   253     }
       
   254 }
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // CMapTileInterface::GetLandMarkDetails()
       
   258 // return pointer to CPosLandmark 
       
   259 // -----------------------------------------------------------------------------
       
   260 //
       
   261 CPosLandmark* CMapTileInterface::GetLandMarkDetails()
       
   262 {
       
   263     return iLandmark;  
       
   264                
       
   265 }
       
   266 // -----------------------------------------------------------------------------
       
   267 // CMapTileInterface::SetLandMarkDetails()
       
   268 // create CPosLandmark details from MAddressInfo 
       
   269 // -----------------------------------------------------------------------------
       
   270 //
       
   271 void CMapTileInterface::SetLandMarkDetailsL(MAddressInfo& aAddressInfo)
       
   272 {
       
   273     if(iLandmark)
       
   274     {
       
   275         delete iLandmark;
       
   276         iLandmark=NULL;
       
   277     }
       
   278     TReal latitude,longitude;
       
   279     latitude=aAddressInfo.GetLatitude();
       
   280     longitude=aAddressInfo.GetLongitude();    
       
   281     TLocality position(TCoordinate(latitude,longitude),0);
       
   282     iLandmark=CPosLandmark::NewL();
       
   283     //latitude and longitude
       
   284     iLandmark->SetPositionL(position);
       
   285     
       
   286     //street
       
   287     TPtrC tempText=aAddressInfo.GetThoroughfareName();
       
   288     if(tempText.Length()>0)
       
   289     {
       
   290         iStreetAvailable=ETrue;
       
   291     }
       
   292     iLandmark->SetPositionFieldL(EPositionFieldStreet, tempText);
       
   293     //postal code
       
   294     tempText.Set(aAddressInfo.GetPincode());
       
   295     iLandmark->SetPositionFieldL(EPositionFieldPostalCode, tempText);
       
   296     //city
       
   297     tempText.Set(aAddressInfo.GetCity());
       
   298     iLandmark->SetPositionFieldL(EPositionFieldCity, tempText);
       
   299     //state
       
   300     tempText.Set(aAddressInfo.GetState());
       
   301     iLandmark->SetPositionFieldL(EPositionFieldState, tempText);
       
   302     //country
       
   303     tempText.Set(aAddressInfo.GetCountryName());
       
   304     iLandmark->SetPositionFieldL(EPositionFieldCountry, tempText); 
       
   305     
       
   306 }
       
   307 //end of line