Code sample: Retrieving an address for geographic coordinates by using reverse geocoding

The following code sample demonstrates how to retrieve an address name by using an asynchronous reverse geocoding request. The request specifies that the server must return a precise street address, which is indicated by the ReverseGeocodeExchange.ADDRESS parameter that is passed into geocode(). When the application receives the results of the request, it displays the address on the screen.
import net.rim.device.api.lbs.maps.MapDimensions;
import net.rim.device.api.lbs.maps.model.*;
import net.rim.device.api.lbs.maps.server.*;
import net.rim.device.api.lbs.maps.server.exchange.*;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.*;
import net.rim.device.api.system.*;
import java.util.Vector;

public class ReverseGeocodingDemo extends UiApplication
{
    public static void main(String[] args)
    {
        ReverseGeocodingDemo app = new ReverseGeocodingDemo();
        app.enterEventDispatcher();
    }
      
    public ReverseGeocodingDemo()
    {
        pushScreen(new ReverseGeocodingDemoScreen());
    }
          
    public class ReverseGeocodingDemoScreen extends MainScreen
    {
        private LabelField _resultsField;
        
        public ReverseGeocodingDemoScreen()
        {
            setTitle("Reverse Geocoding Demo");
            
            _resultsField = new LabelField();
            add(_resultsField);  
           
            // Location for the request.
            MapPoint origin = new MapPoint(43.4815, -80.5407);
            
            // Create an instance of the class that implements 
            // ServerExchangeCallback.
            MyServerExchangeCallback callback = new MyServerExchangeCallback();
                        
            try
            {
                // Initiate the reverse geocoding request.
                ReverseGeocoder.getInstance().reverseGeocode(callback, origin, 
                        ReverseGeocodeExchange.ADDRESS, 1, 0);
            } 
            catch (ReverseGeocodeException e)
            {
                // Do something with the exception.
            }   
        }
        
        public class MyServerExchangeCallback implements ServerExchangeCallback
        {
            public void requestSuccess(ServerExchange exchange)
            {
                // StringBuffer used to display the location information from 
                // the request.
                StringBuffer buffer = new StringBuffer();

                if(exchange instanceof ReverseGeocodeExchange)
                {
                    // Cast the Exchange object to a ReverseGeocodeExchange 
                    // object and retrieve the Vector of results.
                    ReverseGeocodeExchange reverseGeocodeExchange = 
                            (ReverseGeocodeExchange)exchange;
                    Vector results = reverseGeocodeExchange.getResults();

                    // Iterate through each MapLocation object in the Vector.
                    for(int i = 0; i < results.size(); i++)
                    {
                        // Retrieve info from the MapLocation object
                        // and append it to the StringBuffer.
                        MapLocation location = (MapLocation) results.elementAt(i);
                        buffer.append("Name: ").append(location.getName())
                                .append('\n');
                        buffer.append("Desc: ").append(location.getDescription())
                                .append('\n');
                        buffer.append("Addr: ").append(location.getData
                                (MapLocation.LBS_LOCATION_STREET_ADDRESS_KEY))
                                .append('\n');
                        buffer.append("City: ").append(location.getData
                                (MapLocation.LBS_LOCATION_CITY_KEY))
                                .append('\n');
                        buffer.append("County: ").append(location.getData
                                (MapLocation.LBS_LOCATION_COUNTY_KEY))
                                .append('\n');
                        buffer.append("Zip: ").append(location.getData
                                (MapLocation.LBS_LOCATION_POSTAL_CODE_KEY))
                                .append('\n');
                    }
                    
                    // Take control of the UI thread and display the location 
                    // information on the screen.
                    synchronized(UiApplication.getEventLock())
                    {
                        _resultsField.setText(buffer.toString());
                    }        
                }
            }

            public void requestFailure(ServerExchange exchange)
            {
                // Report the failure to the user.
            }

            public void requestHalted()
            {
                // Request was stopped.
            }   
        }
    }
}

Was this information helpful? Send us your comments.