지오코딩을 사용하여 주소에 대한 지리적 좌표 검색
프로그램은 Geocoder 클래스를 사용하여 주소에 대한 지리적 좌표를 검색할 수 있습니다. 예를 들어, 프로그램은 사용자에게 주소에 대한 메시지를 표시한 다음 해당 주소를 지도에 표시할 수 있습니다. 주소는 비체계적인 문자열 형태거나 MapLocation 객체일 수 있습니다. 지오코딩 요청은 동기화 또는 비동기화될 수 있습니다.
주소 지정
// Structured address
MapLocation address1 = new MapLocation();
address1.addData(MapLocation.LBS_LOCATION_CITY_KEY, "waterloo");
address1.addData(MapLocation.LBS_LOCATION_COUNTRY_KEY, "canada");
address1.addData(MapLocation.LBS_LOCATION_STREET_ADDRESS_KEY, "419 phillip st");
address1.addData(MapLocation.LBS_LOCATION_POSTAL_CODE_KEY, "N2L3X2");
// Unstructured address
String address2 = new String("419 phillip st, waterloo, canada, N2L3X2");
주소에 지리적 컨텍스트 제공
지오코딩 서버로부터 가장 적절한 결과를 얻으려면 지오코딩 요청에 지리적 컨텍스트를 제공해야 합니다. 컨텍스트를 제공하기 위해 지리적 좌표(예: BlackBerry 단말기 사용자의 현재 위치)를 포함하는 MapDimensions 객체를 생성할 수 있습니다. 컨텍스트를 제공하면 사용자가 주소만 제공하더라도 관련된 프로그램이 적절한 지리적 좌표를 수신할 수 있습니다. MapDimensions 객체를 제공하지 않은 경우, 프로그램은 BlackBerry Maps 에 저장된 최근 위치를 컨텍스트로 사용하며, 이로 인해 원하는 결과가 제공되지 않을 수도 있습니다.
MapPoint origin = new MapPoint(43.4815, -80.5407); MapDimensions context = new MapDimensions(origin, 480, 360, 5, 0);
비동기 지오코딩 요청 보내기
MyServerExchangeCallback callback = new MyServerExchangeCallback();
try
{
Geocoder.getInstance().geocode(callback, address1, context, 0);
}
catch (GeocodeException e)
{
// Do something with the exception
}
ServerExchangeCallback 인터페이스를 구현하는 클래스를 만들기 위해 프로그램에 포함해야 하는 메소드에는 requestSuccess(), requestFailure() 및 requestHalted()의 세 가지가 있습니다. 지오코딩 요청이 완료되면 프로그램은 요청 결과에 따라 세 메소드 중 하나를 호출합니다.
public class MyServerExchangeCallback implements ServerExchangeCallback
{
public void requestSuccess(ServerExchange exchange)
{
if(exchange instanceof GeocodeExchange)
{
GeocodeExchange geocodeExchange = (GeocodeExchange)exchange;
Vector results = geocodeExchange.getResults();
// Do something with the results
}
}
public void requestFailure(ServerExchange exchange)
{
// Inform the user of the failure
}
public void requestHalted()
{
// Invoked when the request is stopped
}
}
동기 역 지오코딩 요청 보내기
GeocodeExchange exchange = Geocoder.getInstance().geocode
(null, address2, context, 0);
// Check if the request was successful
if(exchange.getExceptionList().size() == 0)
{
// Do something with the result of the request
}
else
{
// Inform the user of the failure
}