×
Namespaces

Variants
Actions
(Difference between revisions)

Reverse Geocoding an address with Nokia Maps API for JavaME

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Bot update - Fix ArticleMetaData and links)
symbianyucca (Talk | contribs)
(Symbianyucca -)
Line 1: Line 1:
 +
[[Category:Java ME]][[Category:Location]][[Category:Nokia Maps]][[Category:Code Examples]]
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
 
|sourcecode= [[Media:Java MapRevGeoCoding.zip]]  
 
|sourcecode= [[Media:Java MapRevGeoCoding.zip]]  
Line 21: Line 22:
 
|author= [[User:Symbianyucca]]
 
|author= [[User:Symbianyucca]]
 
}}
 
}}
[[Category:Draft]][[Category:Java ME]][[Category:Location]][[Category:Nokia Maps]]
+
 
 
{{Abstract| This article explains how to use reverse geocoding service with [http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ Maps API for Java ME] API to get address for selected Geo location.}}
 
{{Abstract| This article explains how to use reverse geocoding service with [http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ Maps API for Java ME] API to get address for selected Geo location.}}
  
Line 104: Line 105:
  
 
The [https://projects.developer.nokia.com/LBSPJME/wiki Java ME Location API] offers rich functionalities that allows to integrate all the main Ovi Maps features in a Java ME application, with just a few lines of code.
 
The [https://projects.developer.nokia.com/LBSPJME/wiki Java ME Location API] offers rich functionalities that allows to integrate all the main Ovi Maps features in a Java ME application, with just a few lines of code.
[[Category:Code Examples]]
 

Revision as of 05:48, 11 July 2012

Article Metadata
Code ExampleCompatibilityArticle
Created: symbianyucca (27 Feb 2012)
Last edited: symbianyucca (11 Jul 2012)

This article explains how to use reverse geocoding service with Maps API for Java ME API to get address for selected Geo location.


Contents

Introduction

MapRevGeoCode.png

The [Maps API for Java ME allows to easily embed Nokia Maps service into Java ME applications. The API includes reverse geocode functionality, which allows retrieving address of the given geographical coordinates.

SearchFactory and ReverseGeocodeRequestListener

The SearchFactory class (available in the 'com.nokia.maps.search ' package) offers various functionalities, including geocode, reverse geocode, and search. These operations are performed in an asynchronous manner, for each service there is a listener interface defined for result callback, which is used when the service is used in asynchronous manner. This interface implements methods for informing of the successful completion of the request as well as there is separate method to be called in error situations.

Implementing reverse geo coding midlet

To be able to utilizing the reverse geo service, there need to be class which is implementing the required PlaceSearchRequestListener interface, this interface defines following 2 methods:

  • onRequestComplete(ReverseGeocodeRequest request, Location[] result)
  • onRequestError(ReverseGeocodeRequest request, java.lang.Throwable error)

And to include this interface in your midlet implementation you need to use the implements clause to, basic code that handles this is shown below:

public class MapRevGeoCoding extends MIDlet 
implements ReverseGeocodeRequestListener, ...
{
public void onRequestComplete(ReverseGeocodeRequest request, Location[] result){
...
}
 
public void onRequestError(ReverseGeocodeRequest request, java.lang.Throwable error) {
...
}
}

Starting the query

Before the service can be used you need to get the search factory instance, which is implementing the service, this is handled simply calling the getInstance method from SearchFactory:

sm = SearchFactory.getInstance();


Then you need to create the ReverseGeocodeRequest instance. This is handled by calling createReverseGeocodeRequest method for the search factory, which after you can use the methods implemented in the reverse geo coding request to handle the actual reverse geo coding request. Note that this request class is implementing multiple functions for making the request, and if you choose you can use the synchronous versions, which would allow you to handle the reply right after the function cal. With this example we are using asynchronous methods, and as we implemented the ReverseGeocodeRequestListener interface we can use the this with with the method call, the other argument used with this method call, is geo the location to be used with reverse geo coding, in this example the center of the map is used as the geo location for the service.

ReverseGeocodeRequest revG = sm.createReverseGeocodeRequest();
revG.reverseGeocode(mapCanvas.getMapDisplay().getCenter(),this);

Handling the result

Successful completion of the asynchronous reverse geo coding will be notified by calling the onRequestComplete method implemented for the ReverseGeocodeRequestListener interface. The onRequestComplete will be called with ReverseGeocodeRequest argument, as well as Location array holding the results for the request.

public void onRequestComplete(ReverseGeocodeRequest request, Location[] result){
if(result.length > 0){
Location firstLocation = result[0];
Address address = firstLocation.getAddress();
...
}
}

The query could also be finished with error, and to handle those you should also implement the onRequestError method, in this example this method simply informs the user of the possible problem.

public void onRequestError(ReverseGeocodeRequest request, java.lang.Throwable error){
Alert alertView = new Alert("Search request error: " + error.getMessage());
display.setCurrent(alertView);
}


Resources

Full source code of the MapRevGeoCoding is available here: File:Java MapRevGeoCoding.zip

Summary

The Java ME Location API offers rich functionalities that allows to integrate all the main Ovi Maps features in a Java ME application, with just a few lines of code.

141 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×