Esempio di codice: recupero di un indirizzo per le coordinate geografiche mediante il geocoding inverso

L'esempio di codice seguente mostra come recuperare il nome di un indirizzo mediante una richiesta di geocoding inverso asincrona. La richiesta specifica che il server deve restituire un indirizzo preciso, indicato dal parametro ReverseGeocodeExchange.ADDRESS passato in geocode(). Quando l'applicazione riceve i risultati della richiesta, visualizza l'indirizzo sullo schermo.
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 informazioni sono state utili? Inviateci i vostri commenti.