×
Namespaces

Variants
Actions
(Difference between revisions)

Reverse Geocoding an address with Nokia Maps API for JavaME

From Nokia Developer Wiki
Jump to: navigation, search
jasfox (Talk | contribs)
m (Jasfox - Add links,)
symbianyucca (Talk | contribs)
(Symbianyucca -)
Line 14: Line 14:
 
The [[http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ 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.
 
The [[http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ 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 ==
+
== SearchFactory and ReverseGeocodeRequestListener ==
  
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.
+
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 ===
 
=== 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:
+
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:
* searchRequestError(SearchManager source, java.lang.Throwable error
+
* onRequestComplete(ReverseGeocodeRequest request, Location[] result)
* searchRequestFinished(SearchManager source)
+
* onRequestError(ReverseGeocodeRequest request, java.lang.Throwable error)
* 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:
 
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:
 
<code java>
 
<code java>
 
public class MapRevGeoCoding extends MIDlet  
 
public class MapRevGeoCoding extends MIDlet  
implements CommandListener, SearchListener
+
implements ReverseGeocodeRequestListener, ...
 
{
 
{
     public void searchRequestFinished(SearchManager searchManager){
+
     public void onRequestComplete(ReverseGeocodeRequest request, Location[] result){
 
     ...
 
     ...
 
     }
 
     }
 
          
 
          
     public void searchRequestError(SearchManager searchManager, Throwable error) {
+
     public void onRequestError(ReverseGeocodeRequest request, java.lang.Throwable error) {
    ...
+
    }
+
    public void searchResponseCleared(SearchManager searchManager) {
+
 
     ...
 
     ...
 
     }
 
     }
Line 42: Line 38:
 
</code>
 
</code>
 
=== Starting the query ===
 
=== 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:
+
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:
 
<code java>
 
<code java>
sm = SearchManager.getInstance();
+
sm = SearchFactory.getInstance();  
 
</code>
 
</code>
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.
+
 
 +
 
 +
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.
 
<code java>
 
<code java>
sm.setListener(this);
+
ReverseGeocodeRequest revG = sm.createReverseGeocodeRequest();
sm.clear();
+
revG.reverseGeocode(mapCanvas.getMapDisplay().getCenter(),this);  
sm.reverseGeocode(mapCanvas.getMapDisplay().getCenter());
+
 
</code>
 
</code>
 
=== Handling the result ===
 
=== Handling the result ===
Successful completion of the reverse geo coding will be notified by calling the searchRequestFinished method implemented for the ''SearchListener'' interface.
+
Successful completion of the asynchronous reverse geo coding will be notified by calling the onRequestComplete method implemented for the ReverseGeocodeRequestListener 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 onRequestComplete will be called with ReverseGeocodeRequest argument, as well as Location array holding the results for the request.
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.
+
 
<code java>
 
<code java>
public void searchRequestFinished(SearchManager searchManager){
+
public void onRequestComplete(ReverseGeocodeRequest request, Location[] result){
    if(searchManager.getLocations().length > 0){
+
if(result.length > 0){
        Location firstLocation = searchManager.getLocations()[0];
+
        Location firstLocation = result[0];
        Address address = firstLocation.getAddress();
+
                Address address = firstLocation.getAddress();
        ...
+
...
    }
+
}
}
+
} </code>
</code>
+
 
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.
+
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.
  
 
<code java>
 
<code java>
public void searchRequestError (SearchManager searchManager, Throwable error) {
+
public void onRequestError(ReverseGeocodeRequest request, java.lang.Throwable error){
 
Alert alertView = new Alert("Search request error: " + error.getMessage());
 
Alert alertView = new Alert("Search request error: " + error.getMessage());
 
display.setCurrent(alertView);
 
display.setCurrent(alertView);

Revision as of 06:49, 4 April 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.

×