2011-12-12 2 views
3

Google Maps의 검색 창처럼 동작하는 SuggestBox을 정의하고 싶습니다. 입력을 시작하면 입력 한 문자로 시작하는 실제 주소가 나타납니다.GWT/Java의 SuggestBox에 주소 제안

나는 Geocoder.getLocations(String address, LocationCallback callback) method을 사용할 필요가 있다고 생각하지만,이 제안을 제안 상자에서 필요로하는 Oracle과 연결하는 방법을 모릅니다.

getLocations 방법을 SuggestOracle과 어떻게 연결할 수 있습니까?

답변

8

서브 클래스 SuggestBox을 구현하여이 문제를 해결했습니다.이 서브 클래스는 SuggestOracle입니다. AddressOracle은 GWT 용 Google Maps API의 클래스 Geocoder이 추상화를 제공하는 Google Maps Service의 래퍼 (Wrapper) 역할을합니다. 그래서 여기

내 솔루션입니다 :

먼저 Google지도 제안과 SuggestBox의 위젯을 구현

public class GoogleMapsSuggestBox extends SuggestBox { 
    public GoogleMapsSuggestBox() { 
     super(new AddressOracle()); 
    } 
} 

그리고 우리는 지오 코더 비동기 방식 추상화 래핑 SuggestOracle 구현 :

class AddressOracle extends SuggestOracle { 

    // this instance is needed, to call the getLocations-Service 
    private final Geocoder geocoder; 


    public AddressOracle() { 
     geocoder = new Geocoder(); 
    } 

    @Override 
    public void requestSuggestions(final Request request, 
      final Callback callback) { 
     // this is the string, the user has typed so far 
     String addressQuery = request.getQuery(); 
     // look up for suggestions, only if at least 2 letters have been typed 
     if (addressQuery.length() > 2) {  
      geocoder.getLocations(addressQuery, new LocationCallback() { 

       @Override 
       public void onFailure(int statusCode) { 
        // do nothing 
       } 

       @Override 
       public void onSuccess(JsArray<Placemark> places) { 
        // create an oracle response from the places, found by the 
        // getLocations-Service 
        Collection<Suggestion> result = new LinkedList<Suggestion>(); 
        for (int i = 0; i < places.length(); i++) { 
         String address = places.get(i).getAddress(); 
         AddressSuggestion newSuggestion = new AddressSuggestion(
           address); 
         result.add(newSuggestion); 
        } 
        Response response = new Response(result); 
        callback.onSuggestionsReady(request, response); 
       } 

      }); 

     } else { 
      Response response = new Response(
        Collections.<Suggestion> emptyList()); 
      callback.onSuggestionsReady(request, response); 
     } 

    } 
} 

그리고 이것은 오라클 제안을위한 특별한 클래스입니다. 오라클 제안은 전달 된 주소가있는 String을 나타냅니다.

RootPanel.get("hm-map").add(new GoogleMapsSuggestBox()); 
:

class AddressSuggestion implements SuggestOracle.Suggestion, Serializable { 

    private static final long serialVersionUID = 1L; 

    String address; 

    public AddressSuggestion(String address) { 
     this.address = address; 
    } 

    @Override 
    public String getDisplayString() { 
     return this.address; 
    } 

    @Override 
    public String getReplacementString() { 
     return this.address; 
    } 
} 

지금 당신은 당신의 EntryPoint - 클래스의 onModuleLoad() -method에 다음 줄을 작성하여 웹 페이지에 새 위젯을 바인딩 할 수 있습니다