locationmanager/locationtrail/src/clocationrecord.cpp
changeset 54 a3cc46f37772
parent 36 aa5a574040a4
--- a/locationmanager/locationtrail/src/clocationrecord.cpp	Tue Jul 06 14:44:37 2010 +0300
+++ b/locationmanager/locationtrail/src/clocationrecord.cpp	Fri Sep 17 16:54:49 2010 +0300
@@ -21,6 +21,9 @@
 #include <ecom.h>
 #include <centralrepository.h>
 #include <hwrmpowerstatesdkpskeys.h>
+#ifdef LOC_GEOTAGGING_CELLID
+#include <lbslocationinfo.h>
+#endif //LOC_GEOTAGGING_CELLID
 
 #include "rlocationtrail.h"
 #include "clocationrecord.h"
@@ -29,10 +32,7 @@
 #include "locationtraildefs.h"
 #include "locationtrailpskeys.h"
 #include "mdeconstants.h"
-#ifdef LOC_REVERSEGEOCODE
-#include "reversegeocoderplugin.h"
-const TUid KReverseGeoCodeUid = {0x2002DD12}; 
-#endif	
+
 
 
 using namespace MdeConstants;
@@ -101,6 +101,7 @@
 	,iImageQuery(NULL)
 	,iTagQuery(NULL)
     ,iTagCreator( NULL )
+    ,iLastReverseGeocodeFails(EFalse)
 	,iRevGeocoderPlugin( NULL )
 #endif
     {
@@ -132,17 +133,13 @@
 #ifdef LOC_REVERSEGEOCODE
     iTagCreator = CTagCreator::NewL();
 
-
-    if (!iRevGeocoderPlugin)
-        {
-        iRevGeocoderPlugin = reinterpret_cast<CReverseGeoCoderPlugin*>(
-              REComSession::CreateImplementationL(KReverseGeoCodeUid,iDtorKey));
-        if(iRevGeocoderPlugin)
-            {
-            iRevGeocoderPlugin->AddObserverL(*this);
-            }
-         }
- 
+	  TInt pluginerr = KErrNone;
+    TRAP(pluginerr,iRevGeocoderPlugin = CReverseGeoCoderPlugin::NewL());
+    		
+   if(pluginerr == KErrNone)
+   	{
+		iRevGeocoderPlugin->AddObserverL(*this);
+	 	}
  #endif
 
 	
@@ -201,8 +198,11 @@
     iLocationItems.Close();
 #endif    
     delete iNetworkInfoChangeListener;
+    iNetworkInfoChangeListener = NULL;
     delete iPositionInfo;
+    iPositionInfo = NULL;
     delete iNetworkInfoTimer;
+    iNetworkInfoTimer = NULL;
 #ifdef LOC_GEOTAGGING_CELLID
     if(iGeoConverter)
         {
@@ -214,6 +214,7 @@
 		{
 		iRemapper->StopRemapping();
 		delete iRemapper;
+		iRemapper = NULL;
 		}
     if(iLocationQuery)
 		{
@@ -251,7 +252,6 @@
 	// set the pointer to NULL, ECOM will destroy object.
     delete iRevGeocoderPlugin;
     iRevGeocoderPlugin = NULL;
-    REComSession::DestroyedImplementation(iDtorKey);
 #endif
     LOG( "CLocationRecord::~CLocationRecord(), end" );	
     }
@@ -355,7 +355,7 @@
     LOG1( "CLocationRecord::GetLocationByTimeL - aTime: %Ld", aTime.Int64() );
     TBuf<DateTimeStrMaxLength> str1;
     aTime.FormatL( str1, KDateTimeFormat );
-   // LOG1( "CLocationRecord::GetLocationByTimeL - aTime: %S", &str1 );
+    LOG1( "CLocationRecord::GetLocationByTimeL - aTime: %S", &str1 );
 #endif
 
     TTimeIntervalSeconds interval;
@@ -370,7 +370,7 @@
         LOG1( "CLocationRecord::GetLocationByTimeL - Trail timestamp: %Ld", iTrail[i]->iTimeStamp.Int64() );
         TBuf<DateTimeStrMaxLength> str;
         iTrail[i]->iTimeStamp.FormatL( str, KDateTimeFormat );
-        //LOG1( "CLocationRecord::GetLocationByTimeL - Trail timestamp: %S", &str );
+        LOG1( "CLocationRecord::GetLocationByTimeL - Trail timestamp: %S", &str );
         LOG1( "CLocationRecord::GetLocationByTimeL - timeDiff: %d", timeDiff );
 #endif
 
@@ -503,7 +503,7 @@
     
     if ( !iTrailStarted || iState == RLocationTrail::ETrailStopped)
         {
-    	LOG("trail not started/stopped");
+    	LOG("CLocationRecord::Position(), trail not started/stopped");
         iPositionInfo->Stop();
         return;
         }
@@ -527,24 +527,24 @@
         case KPositionQualityLoss: 
             {
             // Location is stored, even if it may not be valid.
-            LOG("Partial update");
+            LOG("CLocationRecord::Position(), Partial update");
             if ( iState != RLocationTrail::EWaitingGPSData && 
             	 iState != RLocationTrail::ETrailStopping ) 
                 {
                 SetCurrentState( RLocationTrail::EWaitingGPSData );
-            	LOG("Trail waiting for gps");
+            	LOG("CLocationRecord::Position(), Trail waiting for gps");
                 }
             break;
             }
         case KErrNone:
             {
-            LOG("Good GPS coordinates");
+            LOG("CLocationRecord::Position(), Good GPS coordinates");
             iGpsDataAvailableFlag = ETrue;
             if ( iState != RLocationTrail::ETrailStarted ) 
                 {
                 if ( iRemapper )
                 	{
-                	LOG("Start remapping");
+                	LOG("CLocationRecord::Position(), Start remapping");
                		iRemapper->StartRemappingObjects( iNewItem.iLocationData );
 
                     if( iObserver->WaitForPositioningStopTimeout() && !RemappingNeeded() )                                
@@ -565,7 +565,7 @@
             }
         default:
             {
-            LOG1("Searching GPS, aError %d", aError );
+            LOG1("CLocationRecord::Position(), Searching GPS, aError %d", aError );
             if ( iState != RLocationTrail::ESearchingGPS &&
                	 iState != RLocationTrail::ETrailStopping ) 
                 {
@@ -576,12 +576,12 @@
             }      
         }
     TBool fixState = CheckGPSFix( positionSatelliteInfo );
-    LOG1( "fixState %d", fixState );
-    LOG1( "iLastGPSFixState %d", iLastGPSFixState );
+    LOG1( "CLocationRecord::Position(), fixState %d", fixState );
+    LOG1( "CLocationRecord::Position(), iLastGPSFixState %d", iLastGPSFixState );
     
     if ( iObserver && iLastGPSFixState != fixState )
     	{
-    	LOG("Quality changed");
+    	LOG("CLocationRecord::Position(), Quality changed");
     	iObserver->GPSSignalQualityChanged( positionSatelliteInfo );
     	}
     
@@ -707,10 +707,12 @@
         	{
         	iNetwork.iLocationAreaCode = 0;
         	}
+#ifdef LOC_GEOTAGGING_CELLID
         if ( iState == RLocationTrail::ETrailStarting && iTrailStarted )
         	{
         	SetCurrentState( RLocationTrail::ETrailStarted );
         	}
+#endif        
         }
     else
         {
@@ -797,13 +799,16 @@
     if( iTrailStarted )
         {
         // Set the property only when trail is started to avoid icon flickering and wrong icon update in UI
-        if( iGpsDataAvailableFlag  || 
-            iNetwork.iCellId > 0 && 
+        if( iGpsDataAvailableFlag  
+#ifdef LOC_GEOTAGGING_CELLID
+            || ( iNetwork.iCellId > 0 && 
             ((iNetwork.iLocationAreaCode == 0 && iNetwork.iAccess == CTelephony::ENetworkAccessUtran) || // 3G
             (iNetwork.iLocationAreaCode > 0 && (iNetwork.iAccess == CTelephony::ENetworkAccessGsm ||  // 2G
             						iNetwork.iAccess == CTelephony::ENetworkAccessGsmCompact)))  &&
             iNetwork.iCountryCode.Length() > 0 &&
             iNetwork.iNetworkId.Length() > 0 )
+#endif // LOC_GEOTAGGING_CELLID            
+            )
             {
             // set the value 3 to have Geo tag available icon else not available.
             iProperty.Set( KPSUidLocationTrail, KLocationTrailState, 
@@ -957,7 +962,7 @@
             {
             // no n/w info... put it into remap.
             // remove the last appended element.
-            LOG("No network info (offline mode + no GPS fix), keep for remapping");
+            LOG("CLocationRecord::LocationSnapshotL(), No network info (offline mode + no GPS fix), keep for remapping");
 			TRemapItem remapItem;
 			remapItem.iObjectId = aObjectId;
 			remapItem.iTime = timestamp;
@@ -966,7 +971,12 @@
 			TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
 			iMediaItems.Remove(iMediaItems.Count() - 1);
 			iMediaItems.Compress();
-			delete firstPtr;
+            if( firstPtr == newItem)
+                {                
+                newItem = NULL;
+                }           
+            delete firstPtr;            
+            firstPtr = NULL;
             }   
         else
             {
@@ -977,6 +987,7 @@
 	else if ( Math::IsNaN( locationData.iPosition.Latitude() ) && 
 			Math::IsNaN( locationData.iPosition.Longitude() ))
 		{
+        LOG("CLocationRecord::LocationSnapshotL(), coordinates empty with or without cellular info")
 	    // coordinates empty, with or without cellular info
 
 		if ( net->iCellId == 0 && 
@@ -984,7 +995,7 @@
 				net->iCountryCode.Length() == 0 &&
 				net->iNetworkId.Length() == 0 )
 			{
-            LOG("No network info (offline mode + no GPS fix), keep for remapping");
+            LOG("CLocationRecord::LocationSnapshotL(), No network info (offline mode + no GPS fix), keep for remapping");
 			TRemapItem remapItem;
 			remapItem.iObjectId = aObjectId;
 			remapItem.iTime = timestamp;
@@ -993,7 +1004,12 @@
 			TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
 			iMediaItems.Remove(iMediaItems.Count() - 1);
 			iMediaItems.Compress();
-			delete firstPtr;
+            if( firstPtr == newItem)
+                {                
+                newItem = NULL;
+                }           
+            delete firstPtr;            
+            firstPtr = NULL;
 			}
 		// check match for last created locationobject
 #ifdef LOC_REVERSEGEOCODE
@@ -1003,6 +1019,7 @@
         else if ( (iLastMediaItem.iFlag & KSnapMediaFile) > 0)
 #endif //LOC_REVERSEGEOCODE
 			{
+            LOG("CLocationRecord::LocationSnapshotL(), last created locationobject match" );
 			TLocationData lastLocationData = iLastMediaItem.iLocationData;
 			CTelephony::TNetworkInfoV1* lastnet = &lastLocationData.iNetworkInfo;
 
@@ -1027,11 +1044,16 @@
 				TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
 				iMediaItems.Remove(iMediaItems.Count() - 1);
 				iMediaItems.Compress();
-				delete firstPtr;
+	            if( firstPtr == newItem)
+	                {                
+	                newItem = NULL;
+	                }           
+	            delete firstPtr;            
+	            firstPtr = NULL;
 				}
 			}
 		
-		if ( !previousMatch )
+        if (  newItem && !previousMatch )
 		    {
 			// go for n/w based
             newItem->iFlag |= KNetQueryBit;
@@ -1042,6 +1064,7 @@
 	else if ( lastLocationId != 0 && 
         ((iLastMediaItem.iFlag & KSnapMediaFile) > 0))
 		{
+        LOG("CLocationRecord::LocationSnapshotL(), valid coordinates found");
         TLocationData lastLocationData = iLastMediaItem.iLocationData;
 		CTelephony::TNetworkInfoV1* lastnet = &lastLocationData.iNetworkInfo;
 		
@@ -1053,7 +1076,11 @@
 			{
 			
 			// if both locations have valid coordinates, calculate distance between points
-			if ( !Math::IsNaN( lastLocationData.iPosition.Latitude() ) && 
+			if (
+#ifdef LOC_REVERSEGEOCODE
+                !iLastReverseGeocodeFails &&
+#endif //LOC_REVERSEGEOCODE
+                !Math::IsNaN( lastLocationData.iPosition.Latitude() ) && 
 					!Math::IsNaN( lastLocationData.iPosition.Longitude() ) && 
 					!Math::IsNaN( locationData.iPosition.Latitude() ) && 
 					!Math::IsNaN( locationData.iPosition.Longitude() ))
@@ -1063,7 +1090,7 @@
 				
 				if ( distance < iLocationDelta )
 					{
-					LOG("location close to the previous one");
+					LOG("CLocationRecord::LocationSnapshotL(), location close to the previous one");
 					previousMatch = ETrue;
 					CreateRelationL( aObjectId, lastLocationId );
 					
@@ -1076,9 +1103,15 @@
                         TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
                         iMediaItems.Remove(iMediaItems.Count() - 1);
                         iMediaItems.Compress();
-                        delete firstPtr;
+                        if( firstPtr == newItem)
+                            {                
+                            newItem = NULL;
+                            }           
+                        delete firstPtr;            
+                        firstPtr = NULL;
 					    }
-					else 
+					//check newItem for validity
+					else if (newItem)
 					    {
 					    // country tag not found.. go for reverse geocoding..
 					    newItem->iLocationId = lastLocationId;
@@ -1102,7 +1135,12 @@
                     TLocationSnapshotItem* firstPtr = iMediaItems[iMediaItems.Count() - 1];
                     iMediaItems.Remove(iMediaItems.Count() - 1);
                     iMediaItems.Compress();
-                    delete firstPtr;
+                    if( firstPtr == newItem)
+                        {                
+                        newItem = NULL;
+                        }           
+                    delete firstPtr;            
+                    firstPtr = NULL;
 
 #endif //LOC_REVERSEGEOCODE
 					}
@@ -1178,7 +1216,7 @@
 	     || (iMediaItems.Count() <= 0))
         {
 		// query is in progress or queue is empty
-		LOG1( "query is in progress or queue is empty. Count - %d", iMediaItems.Count() );
+		LOG1( "CLocationRecord::FindLocationFromDBL(), Count - %d", iMediaItems.Count() );
         return;
         }
      if ( (iMediaItems[0]->iFlag & KNetQueryBit) > 0 )
@@ -1191,8 +1229,6 @@
      const TReal64 KMeterInDegrees = 0.000009;
      const TReal64 KPi = 3.14159265358979;
      const TReal32 K180Degrees = 180.0;
-     
-     //TLocationData locationData = iMediaItems[0].iLocationData;
             
      TReal64 latitude = iMediaItems[0]->iLocationData.iPosition.Latitude();
      TReal64 longitude = iMediaItems[0]->iLocationData.iPosition.Longitude();
@@ -1223,10 +1259,10 @@
      CMdELogicCondition& cond = iLocationQuery->Conditions();
      cond.SetOperator( ELogicConditionOperatorAnd );
             
-     LOG1( "latitude: %f", latitude);
-     LOG1( "latdelta: %f", latDelta);
-     LOG1( "longitude: %f", longitude);
-     LOG1( "londelta: %f", lonDelta);
+     LOG1( "CLocationRecord::FindLocationFromDBL(), latitude: %f", latitude);
+     LOG1( "CLocationRecord::FindLocationFromDBL(), latdelta: %f", latDelta);
+     LOG1( "CLocationRecord::FindLocationFromDBL(), longitude: %f", longitude);
+     LOG1( "CLocationRecord::FindLocationFromDBL(), londelta: %f", lonDelta);
             
      cond.AddPropertyConditionL( *iLatitudeDef, 
                     TMdERealBetween( latitude - latDelta, latitude + latDelta ));
@@ -1316,12 +1352,14 @@
 	locationObject->AddTextPropertyL( itemTypeDef, Location::KLocationItemType );
 	locationObject->AddInt16PropertyL( offSetDef, timeOffset.Int() / 60 );
 	
-	LOG1( "Location created with stamp: %Ld", timestamp.Int64() );
+	LOG1( "CLocationRecord::DoCreateLocationL(), Location created with stamp: %Ld", timestamp.Int64() );
 	
 	// location related properties
 	if ( !Math::IsNaN( aLocationData.iPosition.Latitude() ) && 
 		 !Math::IsNaN( aLocationData.iPosition.Longitude() ))
 		{
+        LOG1("CLocationRecord::DoCreateLocationL(), Lan - %f", aLocationData.iPosition.Latitude());
+        LOG1("CLocationRecord::DoCreateLocationL(), Lon - %f", aLocationData.iPosition.Longitude());
 		locationObject->AddReal64PropertyL( *iLatitudeDef, aLocationData.iPosition.Latitude() );
 		locationObject->AddReal64PropertyL( *iLongitudeDef, aLocationData.iPosition.Longitude() );
 
@@ -1344,36 +1382,61 @@
 		}
 
 	// network related properties
-	if ( aLocationData.iNetworkInfo.iAreaKnown )
+	if ( aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
+		{
+		LOG1("CLocationRecord::DoCreateLocationL(), Cell id - %d", aLocationData.iNetworkInfo.iCellId);
+		locationObject->AddUint32PropertyL( cellIdDef, aLocationData.iNetworkInfo.iCellId );
+		}
+	if ( aLocationData.iNetworkInfo.iAreaKnown && 
+        aLocationData.iNetworkInfo.iLocationAreaCode != 0 &&
+		aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
 		{
-		if ( aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
-			{
-			locationObject->AddUint32PropertyL( cellIdDef, aLocationData.iNetworkInfo.iCellId );
-			
-			}
-		if ( aLocationData.iNetworkInfo.iLocationAreaCode != 0 &&
-			aLocationData.iNetworkInfo.iAccess != CTelephony::ENetworkAccessUnknown )
-			{
-			locationObject->AddUint32PropertyL( locationCodeDef, 
-					aLocationData.iNetworkInfo.iLocationAreaCode );
-			
-			}
-		if ( aLocationData.iNetworkInfo.iCountryCode.Length() > 0 )
-			{
-			locationObject->AddTextPropertyL( countryCodeDef, 
-					aLocationData.iNetworkInfo.iCountryCode );
-			
-			}
-		if ( aLocationData.iNetworkInfo.iNetworkId.Length() > 0 )
-			{
-			locationObject->AddTextPropertyL(networkCodeDef, aLocationData.iNetworkInfo.iNetworkId);
-			
-			}
+		LOG1("CLocationRecord::DoCreateLocationL(), Areacode - %d", aLocationData.iNetworkInfo.iLocationAreaCode);
+		locationObject->AddUint32PropertyL( locationCodeDef, 
+				aLocationData.iNetworkInfo.iLocationAreaCode );
+		}
+#ifdef _DEBUG
+	
+    if ( aLocationData.iNetworkInfo.iCountryCode.Length() > 0 )
+        {
+        TLex lexer( aLocationData.iNetworkInfo.iCountryCode );
+        TUint countryCode = 0;    
+        TRAP_IGNORE(lexer.Val( countryCode, EDecimal));        
+        LOG1("CLocationRecord::DoCreateLocationL(), Country code - %d", countryCode);
+        }
+    else
+        {
+        LOG("CLocationRecord::DoCreateLocationL(), No Country code");
+        }
+    
+    //Set mobile network code
+    if ( aLocationData.iNetworkInfo.iNetworkId.Length() > 0 )
+        {
+        TLex lexer = aLocationData.iNetworkInfo.iNetworkId;
+        TUint networkCode = 0;
+        TRAP_IGNORE(lexer.Val( networkCode, EDecimal));
+        LOG1("CLocationRecord::DoCreateLocationL(), Network id - %d", networkCode); 
+        }
+    else
+        {
+        LOG("CLocationRecord::DoCreateLocationL(), No network code");
+        }
+
+#endif
+	if ( aLocationData.iNetworkInfo.iCountryCode.Length() > 0 )
+		{
+		locationObject->AddTextPropertyL( countryCodeDef, 
+				aLocationData.iNetworkInfo.iCountryCode );
+		}
+
+	if ( aLocationData.iNetworkInfo.iNetworkId.Length() > 0 )
+		{
+		locationObject->AddTextPropertyL(networkCodeDef, aLocationData.iNetworkInfo.iNetworkId);
 		}
 	        
 	// Add the location object to the database.
 	locationObjectId = iMdeSession->AddObjectL( *locationObject );
-
+    LOG1("CLocationRecord::DoCreateLocationL(), Location id - %d", locationObjectId);
 	CleanupStack::PopAndDestroy( locationObject );
     LOG( "CLocationRecord::DoCreateLocationL(), end" );
 
@@ -1553,7 +1616,6 @@
     LOG( "CLocationRecord::HandleFindLocationFromDB(), begin" );    
 	if ( iMediaItems.Count() > 0 )
 	   {
-		//TODO: by module owner
 		TInt trapErr = KErrNone;
 		TRAP(trapErr,FindLocationFromDBL());
 		// no memory, don't proceed further
@@ -1753,6 +1815,7 @@
         CMdEObject& locationObject = static_cast<CMdEObject&>(item);
         locationId = locationObject.Id();
         
+        iMediaItems[0]->iLocationId = locationId;
         TRAP( error, CreateRelationL( iMediaItems[0]->iObjectId, locationId ) );
 #ifdef LOC_REVERSEGEOCODE
         //check if found location object has lat, long
@@ -1824,10 +1887,21 @@
             iMediaHandlingFlag &= ~KSnapGeoConvertInPendingState;
             }
 #else
+        // cell id based geo tagging is not supported.. go for remapping.
         locationId = DoCreateLocationL( iMediaItems[0]->iLocationData );
         iMediaItems[0]->iLocationId = locationId;
-        CreateRelationL( iMediaItems[0]->iObjectId, locationId );
+        TItemId relationId = CreateRelationL( iMediaItems[0]->iObjectId, locationId );
         TLocationSnapshotItem* firstPtr = iMediaItems[0];
+
+        // Go for remapping.. get locationdata from trail with object time
+        TTime timestamp = GetMdeObjectTimeL( iMediaItems[0]->iObjectId );
+        TRemapItem remapItem;
+        remapItem.iObjectId = iMediaItems[0]->iObjectId;
+        remapItem.iTime = timestamp;
+        remapItem.iLocationId = locationId;
+        remapItem.iRelationId = relationId;
+        iRemapper->Append( remapItem );
+        
         iMediaItems.Remove(0);
         delete firstPtr;
         iMediaItems.Compress();
@@ -1881,7 +1955,6 @@
             iNewItem.iLocationData.iQuality = aPosition.HorizontalAccuracy();
             TItemId locationId = DoCreateLocationL( iNewItem.iLocationData );
             iRemapper->UpdateRelationsL( locationId ); 
-            // TODO: remap.
 #ifdef LOC_REVERSEGEOCODE
             if(!(iMediaHandlingFlag & KReverseGeoCodingInProgress))
                 {
@@ -1911,7 +1984,17 @@
            TLocationSnapshotItem* item = iMediaItems[0];
            TItemId locationId = DoCreateLocationL( iMediaItems[0]->iLocationData );
            iMediaItems[0]->iLocationId = locationId;
-           CreateRelationL( iMediaItems[0]->iObjectId, locationId );
+           TItemId relationId = CreateRelationL( iMediaItems[0]->iObjectId, locationId );
+
+           // Go for remapping.. get locationdata from trail with object time
+           TTime timestamp = GetMdeObjectTimeL( iMediaItems[0]->iObjectId );
+           TRemapItem remapItem;
+           remapItem.iObjectId = iMediaItems[0]->iObjectId;
+           remapItem.iTime = timestamp;
+           remapItem.iLocationId = locationId;
+           remapItem.iRelationId = relationId;
+           iRemapper->Append( remapItem );
+
            if((iMediaItems[0]->iFlag & KSnapMediaFile) > 0)
                {
                iLastMediaItem = *(iMediaItems[0]);
@@ -1952,6 +2035,20 @@
             else if ( iMediaItems.Count() > 0 )
                 {
                 TLocationSnapshotItem* item = iMediaItems[0];
+                // Fails may be becuase of n/w reason..create location + relation so that we can handle at 3:00 AM.
+                TItemId locationId = DoCreateLocationL( iMediaItems[0]->iLocationData );
+                iMediaItems[0]->iLocationId = locationId;
+                TItemId relationId = CreateRelationL( iMediaItems[0]->iObjectId, locationId );
+
+                // Go for remapping.. get locationdata from trail with object time
+                TTime timestamp = GetMdeObjectTimeL( iMediaItems[0]->iObjectId );
+                TRemapItem remapItem;
+                remapItem.iObjectId = iMediaItems[0]->iObjectId;
+                remapItem.iTime = timestamp;
+                remapItem.iLocationId = locationId;
+                remapItem.iRelationId = relationId;
+                iRemapper->Append( remapItem );
+
                 iMediaItems.Remove(0);
                 iMediaItems.Compress();
                 iMediaHandlingFlag &= ~KLocationQueryInProgress;
@@ -2139,6 +2236,11 @@
     CMdEProperty* property = NULL;
     
     object = iMdeSession->GetObjectL( aObjectId );
+    if( !object )
+        {
+        User::Leave( KErrNotFound );
+        }
+    
     CleanupStack::PushL( object );
     object->Property( timeDef, property, 0 );
     if ( !property )
@@ -2148,7 +2250,10 @@
     
     const TTime timeValue( property->TimeValueL() );
     CleanupStack::PopAndDestroy( object );
+    
+    LOG( "CLocationRecord::GetMdeObjectTimeL(), end" );
     return timeValue;
+        
     }
 
 
@@ -2209,7 +2314,6 @@
             
         iNetLocationQuery->FindL(1, 1); 
         iMediaHandlingFlag |= KLocationQueryInProgress;
-    //    iMediaHandlingFlag |= KNetQueryInProgress;
         }
     else
         {
@@ -2461,8 +2565,10 @@
     
     iMediaHandlingFlag &= (~KReverseGeoCodingInProgress);
 	TLocationSnapshotItem* snapshotItem = NULL;
+    LOG1("Error - %d", aErrorcode);
     if( aErrorcode == KErrNone )
         {
+        iLastReverseGeocodeFails = EFalse;
         TPtrC countryPtr( aAddressInfo.GetCountryName() );
         TPtrC cityPtr( aAddressInfo.GetCity() );
         TRAP_IGNORE( iTagCreator->CreateLocationTagsL( countryPtr, countryTagId, 
@@ -2482,19 +2588,16 @@
                 
                 TRAP_IGNORE( iTagCreator->AttachTagsL( 
                                 iLocationItems[0]->iObjectId, countryTagId, cityTagId  ) );
+                if ( (iLastMediaItem.iFlag & KSnapMediaFile) > 0 
+                    && iLastMediaItem.iLocationId == iLastLocationItem.iLocationId )
+                    {
+                    LOG("Updating country/city\n");
+                    iLastMediaItem.iCountryTagId = countryTagId;
+                    iLastMediaItem.iCityTagId = cityTagId;
+                    }
+                
                 }
             }
-        if(iLastMediaItem.iFlag == 0)
-            {
-            LOG("Last media item is null\n");
-            }
-        if ( iLastMediaItem.iFlag > 0 && iLastMediaItem.iLocationId == iLastLocationItem.iLocationId )
-            {
-            LOG("Updating country/city\n");
-            iLastMediaItem.iCountryTagId = countryTagId;
-            iLastMediaItem.iCityTagId = cityTagId;
-            }
-        
         //check other items in the array has same location 
         for ( TInt index = iLocationItems.Count() - 1; index > 0; index--)
             {
@@ -2512,6 +2615,8 @@
     else
         {
         //handle error
+        LOG("Reverse geo coding fails");
+        iLastReverseGeocodeFails = ETrue;
         }
     
     //irrespective of error or not, remove current(first) item to proceed further