코드 샘플: 지오코딩을 사용하여 주소에 대한 지리적 좌표 검색

다음 코드 샘플은 비동기 지오코딩 요청을 사용하여 주소에 대한 지리적 좌표를 검색하는 방법을 보여줍니다. 요청에서 거리 주소만을 지정하더라도 지오코딩 서버는 요청에 포함되어 있는 지리적 컨텍스트 덕분에 관련 좌표를 반환할 수 있습니다. 프로그램이 검색 결과를 수신하면 화면에 위도와 경도를 표시합니다.
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.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.MainScreen;
import java.util.Vector;
public class GeocodingDemo extends UiApplication
{
    public static void main(String[] args)
    {
        GeocodingDemo app = new GeocodingDemo();
        app.enterEventDispatcher();
    }
    public GeocodingDemo()
    {
        pushScreen(new GeocodingDemoScreen());
    }
    public class GeocodingDemoScreen extends MainScreen
    {
        private LabelField _resultsField;
        public GeocodingDemoScreen()
        {
            setTitle("Geocoding Demo");
            _resultsField = new LabelField();
            add(_resultsField);
            // Geographic context that is sent with the request.
            MapDimensions location = new MapDimensions(
                    new MapPoint(43.4815, -80.5407), 480, 360, 5, 0);           
            // The address used in the request. Only the street name is 
            // required, since geographic context is provided that
            // indicates the search is coming from Waterloo, Ontario.
            MapLocation address = new MapLocation();
            address.addData(MapLocation.LBS_LOCATION_STREET_ADDRESS_KEY, 
                    "419 phillip st");
            // Create an instance of the class that implements 
            // ServerExchangeCallback.
            MyServerExchangeCallback callback = new MyServerExchangeCallback();
            try
            {
                // Initiate the reverse geocoding request.
                Geocoder.getInstance().geocode(callback, address, location, 0);
            } 
            catch (GeocodeException e)
            {
                // Do something with the exception.
            }   
        }
        public class MyServerExchangeCallback implements ServerExchangeCallback
        {
            // StringBuffer used to display the location information from the 
            // request.
            StringBuffer buffer = new StringBuffer();
            public void requestSuccess(ServerExchange exchange)
            {
                if(exchange instanceof GeocodeExchange)
                {
                    // Cast the Exchange object to a GeocodeExchange object
                    // and retrieve the Vector of results.
                    GeocodeExchange geocodeExchange = (GeocodeExchange)exchange;
                    Vector results = geocodeExchange.getResults();
                    // Iterate through each MapLocation object in the Vector 
                    // (can be many results depending on the context you provide 
                    // with the request).
                    for(int i = 0; i < results.size(); i++)
                    {
                        // Retrieve the lat and lon from the MapLocation object 
                        // and appends it to the StringBuffer.
                        MapLocation location = (MapLocation) results.elementAt(i);
                        buffer.append("Latitude: ").append(location.getLat())
                                .append('\n');
                        buffer.append("Longitude: ").append(location.getLon())
                                .append('\n');
                    }
                    // Take control of the UI thread and displays 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.
            }   
        }
    }        
}

이 정보가 도움이 되었습니까? 의견을 보내 주십시오.