在GWT / Java中的SuggestBox中建议地址

时间:2011-12-12 19:19:31

标签: google-app-engine google-maps gwt gwt-rpc

我想定义一个SuggestBox,其行为类似于Google Maps中的搜索栏:当您开始输入时,会出现以输入字母开头的实际地址。

我认为,我需要使用Geocoder.getLocations(String address, LocationCallback callback) method,但我不知道如何将其与oracle连接,而建议框需要oracle来提供它的建议。

您能否告诉我如何将getLocations方法与SuggestOracle相关联?

1 个答案:

答案 0 :(得分:8)

我通过实现SuggestBox的子类来解决这个问题,该子类具有自己的SuggestOracleAddressOracle作为Google地图服务的包装进行交易,Google Maps API for GWT中的课程Geocoder提供抽象。

所以这是我的解决方案:

首先,我们使用Google Maps建议实现SuggestBox的Widget

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

然后我们实现了SuggestOracle,它包装了Geocoder异步方法抽象:

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);
        }

    }
}

这是oracle建议的一个特殊类,它只代表一个带有传递地址的String。

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;
    }
}

现在,您可以通过在onModuleLoad() - EntryPoint类的RootPanel.get("hm-map").add(new GoogleMapsSuggestBox()); - 方法中编写以下行,将新窗口小部件绑定到您的网页中:

{{1}}