Ejemplo de código: recuperar una dirección para obtener las coordenadas geográficas mediante la codificación geográfica inversa

El siguiente ejemplo de código muestra cómo recuperar el nombre de una dirección mediante una solicitud de codificación geográfica inversa asíncrona. La solicitud especifica que el servidor debe devolver la dirección precisa de una calle, indicada por el parámetro ReverseGeocodeExchange.ADDRESS que se pasa a geocode(). Cuando la aplicación recibe los resultados de la solicitud, muestra la dirección en la pantalla.
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.
            }   
        }
    }
}

¿Le ha resultado útil esta información? Envíenos sus comentarios.