×
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,)
jasfox (Talk | contribs)
m (Jasfox - Link to GitHub)
 
(16 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[[Category:Draft]][[Category:Java ME]][[Category:Location]][[Category:Nokia Maps]]
+
[[Category:HERE Maps for Java ME]][[Category:Code Examples]][[Category:Nokia Asha]][[Category:Nokia Asha Platform 1.0]][[Category:Series 40]][[Category:Series 40 Developer Platform 2.0]][[Category:Series 40 Developer Platform 1.1]]
{{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 the  [http://www.developer.nokia.com/Resources/Library/HERE_Maps_Java_ME/#!index.html Map API for Java ME] to get address for selected Geo location.}}
 
+
 
+
 
{{SeeAlso|
 
{{SeeAlso|
*  [http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/ Map API for Java ME]
+
*  [http://www.developer.nokia.com/Resources/Library/HERE_Maps_Java_ME/#!user-guide/places/geocoding-and-reverse-geocoding.html Reverse Geocoding] (Java Developer's Library)
* [http://www.developer.nokia.com/Develop/Maps/Maps_API_for_Java_ME/Code_examples/#search Search Demo]
+
*  [https://github.com/nokia-developer/here-maps-component-demos HERE Maps Code Examples]
 +
}}
 +
{{ArticleMetaData <!-- v1.2 -->
 +
|sourcecode= [[Media:Java MapRevGeoCoding.zip]]
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices=  X3-02, Asha 311, Asha 501
 +
|sdk=  [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 1.0 for Java], [http://www.developer.nokia.com/Develop/Java/ Nokia SDK 2.0 for Java], [http://www.developer.nokia.com/Develop/asha/java/ Nokia Asha SDK 1.0]
 +
|platform= Asha, Series 40, Symbian^1, Symbian^3
 +
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 +
|dependencies=HERE Maps API for Java ME v1.3
 +
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= HERE Maps, Java ME, search, reverse geocoding
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20120227
 +
|author= [[User:Symbianyucca]]
 
}}
 
}}
 +
 +
  
 
== Introduction ==
 
== Introduction ==
Line 12: Line 34:
 
[[File: MapRevGeoCode.png]]
 
[[File: MapRevGeoCode.png]]
  
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/Resources/Library/HERE_Maps_Java_ME/#!index.html HERE Maps API for Java ME] facilitates the easy embedding of the HERE Maps service into Java ME applications. The API includes reverse geocode functionality, which makes it possible to find the 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 60:
 
</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 instance of search factory, which implements the service, this is handled by 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);
Line 79: Line 99:
 
== Resources ==
 
== Resources ==
  
Full source code of the MapRevGeoCoding is available here: [[File: Java_MapRevGeoCoding.zip]]
+
Full source code of the MapRevGeoCoding is available here: [[File: Java MapRevGeoCoding.zip]]
  
 
== Summary ==
 
== Summary ==
  
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 [http://www.developer.nokia.com/Resources/Library/HERE_Maps_Java_ME/#!index.html Map API for Java ME] offers rich functionalities that allows to integrate all the main HERE Maps features in a Java ME application, with just a few lines of code.

Latest revision as of 09:44, 20 September 2013

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

See Also

Article Metadata
Code ExampleTested with
Devices(s): X3-02, Asha 311, Asha 501
Compatibility
Platform(s): Asha, Series 40, Symbian^1, Symbian^3
Nokia Asha
Nokia Asha Platform 1.0
Series 40
Series 40 DP 2.0
Series 40 DP 1.1
Dependencies: HERE Maps API for Java ME v1.3
Article
Keywords: HERE Maps, Java ME, search, reverse geocoding
Created: symbianyucca (27 Feb 2012)
Last edited: jasfox (20 Sep 2013)


Contents

[edit] Introduction

MapRevGeoCode.png

The HERE Maps API for Java ME facilitates the easy embedding of the HERE Maps service into Java ME applications. The API includes reverse geocode functionality, which makes it possible to find the address of the given geographical coordinates.

[edit] 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.

[edit] 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) {
...
}
}

[edit] Starting the query

Before the service can be used you need to get the instance of search factory, which implements the service, this is handled by 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);

[edit] 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);
}


[edit] Resources

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

[edit] Summary

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

This page was last modified on 20 September 2013, at 09:44.
147 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.

×