This page was last modified 21:36, 5 June 2008.
Google Maps using Location Api in Symbian
From Forum Nokia Wiki
Here is a complete application with source code on how to display Google Maps on S60 with Location API. The application is tested on N95 and S60 3rd Edition FP1 Emulator.
The details and on how to make this application is as detailed below
Contents |
Pre-requisites
Before using this example, compile the example of HttpClientEngine
and then the author as mentioned
- include <LBSSample_0xE1038E0E.rsg>
replace it with
- include <LBSSample.rsg>
will compile the example properly.
Signup For google Maps key
This is quite simple, just go to the following site and register providing a URL but only after login with a gmail account
http://code.google.com/apis/maps/signup.html
The sample I have used uses a key generated by myself so if any error occurs it might be due to that key. So go to the above site and register your own key.
Complete help documentation is provided in the above site.
Also documentation regarding usage of Google maps in mobile is provided in How to use Google Maps data in mobile applications
Now coding
For making the application and testing it I made use of Carbide.C++ and S60 3rd Edition FP1 emulator Started of using the 'hello world ' template provided by Carbide.C++
Following are the beakdown of different components with main code. The full code can be downloaded from the source zip attached to this page.
Retrieve Current latitude and Longitude
The current latitude and longitude is retrieved using the following code
void CLBSSampleAppView::GetGPSInfoL() { // create CGpsPositionRequest object and put it into cleanup stack; // pass application name as argument CGpsPositionRequest* request = CGpsPositionRequest::NewL( _L("LBS_0xEA962394")); CleanupStack::PushL( request ); // get current location (this operation can be long up to 30 seconds); // progress dialog is shown to user during this time TBool result = request->FetchCurrentPostionL(latitude, longitude); // delete request object CleanupStack::PopAndDestroy(request); // process result here if (result) { // success, use latitude and longitude coordinates } else { // failed getting current position, show error message to user } }
For more details on how to retrieve GPS latitude and longitude can be found from GPS API in S60 3rd Edition
Setup folder to download Map
Setup a file in the filesystem of the phone where the google map image is to be saved.The google map to be downloaded is of jpeg format. You may download other format such as PNG and Gif. If so change the code accordingly
void CLBSSampleAppView::SetupFileDownload() { _LIT(KXMLFilePath, "c:\\Data\\Images\\"); TFileName iCurrentFileName; iCurrentFileName.Append(KXMLFilePath); iCurrentFileName.Append(_L("LbsSample.jpg")); TInt err=iRLbsImage.Open(CCoeEnv::Static()->FsSession(),iCurrentFileName,EFileWrite); if (err==KErrNotFound) // file does not exist - create it { err=iRLbsImage.Create(CCoeEnv::Static()->FsSession(),iCurrentFileName,EFileWrite); } }
Download Map of the corresponding location
To download the corresponding MAP of the location (Latitude and longitude) obtained from GetGPSInfoL, you have to send an HTTP request to google maps with the latitude & longitude values.
Inorder to send the HTTP request and also to parse the data obtained from the server, I made use of S60 Platform: HTTP Client API Example v2.1 provided in the document section of the Forum Nokia site. This is a quite helpful document on how to use http Get & Post requests. For the LBSSample application only Get method is used.
//Prefix of google URL to retrieve Map // %S, %S will be replaced by latitude and longitude values respectively by the values retrieved from the method GetGPSInfoL _LIT(KGoogleMapURL,"http://maps.google.com/staticmap?center=%S,%S&format=jpg&zoom=8&size=240x320&key="); // This is my key, Signup in Google maps and create your own key _LIT(KGoogleMapKey,"ABQIAAAA7mTDJNgGP953Yt-jOVCpaRSSyDKWhj1AwFHP2_2SAaYvELj_yhTyAVWHzDWGNg0x2EqtccgujgzKwA"); // ----------------------------------------------------------------------------- // CLBSSampleAppView::SendHTTPRequestL() // send Http request with the latitude and longitude values to google maps // ----------------------------------------------------------------------------- // void CLBSSampleAppView::SendHTTPRequestL() { const TInt KMaxFolatLength = 8; const TInt KDecimalPos = 5; TRealFormat format( KMaxFolatLength, KDecimalPos ); format.iType = KRealFormatFixed | KDoNotUseTriads; TBuf8<KDefaultBufferSize> uri8; uri8.Append( KGoogleMapURL ); uri8.Append( KGoogleMapKey ); HBufC8* longitudeDes = HBufC8::NewLC(257); longitudeDes->Des().Num(longitude,format); HBufC8* latitudeDes = HBufC8::NewLC(257); latitudeDes->Des().Num(latitude,format); TBuf8<KDefaultBufferSize> TempBuffer; TempBuffer.Format(uri8,latitudeDes,longitudeDes); CleanupStack::PopAndDestroy(2); // Start transaction TRAPD(err, iEngine->IssueHTTPGetL(TempBuffer)); // TODO: Error handling if (err) { } }
The general URL is as follows as in Google Maps data in mobile applications
http://maps.google.com/staticmap?center=41.867878,12.471516&format=png32&zoom=8&size=240x320&key=<API_KEY>
And I had changed it using my key. It is recommended that you should use your own key.
Display the Downloaded map
After the image is download then you have to display it for this you have to use imageconversion API. A nice tutorial on how to read the image file in the filesystem of the phone is provided in thie Wiki PageHow to read images to Symbian bitmap . If you have downloaded other formats of Google maps like PNG or Gif, then also you can make use of the same code.
The following code gives the call to the image reader.
_LIT(KTxtFileName, "c:\\Data\\Images\\LbsSample.jpg"); void CLBSSampleAppView::SetImage() { iJPG_Reader = new(ELeave)CImageReader(*this); iJPG_Reader->ConstructL(KTxtFileName); }
To Display the image on the screen made use of the following code in the Draw Method
if(iJPG_Reader) { if(iJPG_Reader->Bitmap()) { if(iJPG_Reader->Bitmap()->Handle()) { gc.DrawBitmap(aRect, iJPG_Reader->Bitmap()); } } }
The full source code and application can be downloaded from Media:LBSSample.zip
To test the application, you have to import the project to your carbide. After that build the application. Run it on the emulator. Select "Options" --> Select 'GPS Map'. After that select the provider, wait few second here you will see the MAP. In the emulator you will see the MAP of a place in Finland because the emulator returns dummy latitude and longitudinal values of a place in Finland.
One can make changes to this application and make a fine application where a marked postion to be displayed after intervals.
| Related Discussions | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Location Acquisition API | prcontrol | Symbian Networking & Messaging | 0 | 2005-12-29 12:47 |
| gmaps | anigma.55 | Location Based Services and Navigation | 1 | 2008-07-23 09:21 |
| Location Based API (newbie) | alqurri | Mobile Java General | 5 | 2006-05-19 12:14 |
| Using MWS and Context-Situation for Mobile Applications | ARJWright | Mobile Web Server | 16 | 2008-07-30 17:30 |
| JSR Support and MIDlets | Paranoid_Android | Mobile Java Networking & Messaging & Security | 12 | 2006-10-20 14:02 |

