×
Namespaces

Variants
Actions
(Difference between revisions)

Reverse Geocoding an address with Nokia Maps API for JavaME

From Nokia Developer Wiki
Jump to: navigation, search
symbianyucca (Talk | contribs)
(Symbianyucca -)
 
jasfox (Talk | contribs)
m (Jasfox - Add links,)
Line 1: Line 1:
 
[[Category:Draft]][[Category:Java ME]][[Category:Location]][[Category:Nokia Maps]]
 
[[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.}}
 +
 +
 +
{{SeeAlso|
 +
*  [http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ Map API for Java ME]
 +
* [http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/Code_examples/#search Search Demo]
 +
}}
  
 
== Introduction ==
 
== Introduction ==

Revision as of 18:57, 7 March 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.

SearchManager and SearchListener

The SearchManager 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, and a SearchListener is notified when they are completed, cleared or when an error has occurred.

Implementing reverse geo coding midlet

To be able to utilizing the reverse geo service, there need to be class which is implementing the required SearchListener interface, this interface defines 3 methods which are:

  • searchRequestError(SearchManager source, java.lang.Throwable error)
  • searchRequestFinished(SearchManager source)
  • searchResponseCleared(SearchManager source)

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 CommandListener, SearchListener
{
public void searchRequestFinished(SearchManager searchManager){
...
}
 
public void searchRequestError(SearchManager searchManager, Throwable error) {
...
}
public void searchResponseCleared(SearchManager searchManager) {
...
}
}

Starting the query

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

sm = SearchManager.getInstance();

Then you need to set the SearchListener for the SearchManager, so you would be able to get the results for queries. With this example the class in which the service is used implements also the interface, so you can use this reference as SearchListener. Before attempting to start the query it is advisable to clear the previous query, which is handled by called clear for the SearchManager instance. Then the actual reverse geo service query can be started by calling reverseGeocode. This method takes one argument which is geo location, in this example the center of the map is used as the geo location for the service.

sm.setListener(this);
sm.clear();
sm.reverseGeocode(mapCanvas.getMapDisplay().getCenter());

Handling the result

Successful completion of the reverse geo coding will be notified by calling the searchRequestFinished method implemented for the SearchListener interface. The searchRequestFinished will be called with SearchManager argument, which can be used for retrieving the actual result data by simply calling getLocations method. The getLocations method returns array of Location objects, so you should check whether there is multiple possible results for the query, with this example only the first match is used.

public void searchRequestFinished(SearchManager searchManager){
if(searchManager.getLocations().length > 0){
Location firstLocation = searchManager.getLocations()[0];
Address address = firstLocation.getAddress();
...
}
}

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

public void searchRequestError (SearchManager searchManager, 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.

195 page views in the last 30 days.
×