GWT/Java中SuggestBox中的建议地址

3
我想定义一个SuggestBox,它的行为类似于Google Maps中的搜索栏:当您开始输入时,以所键入的字母开头的真实地址将出现。
我认为我需要使用Geocoder.getLocations(String address, LocationCallback callback)方法,但我不知道如何将其与oracle连接起来,SuggestBox需要它来生成建议。
请给我一些想法,如何将getLocations方法与SuggestOracle连接?
1个回答

8
我解决了这个问题,通过实现SuggestBox的子类,并拥有自己的SuggestOracleAddressOracle作为Google Maps服务的包装器,其中类Geocoder在Google Maps API for GWT中提供抽象化。

以下是我的解决方案:

首先,我们实现一个带有Google Maps建议的SuggestBox小部件。

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建议的特殊类,它只代表了一个包含交付地址的字符串。

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() 方法中编写以下行来将新小部件绑定到您的网页中:
RootPanel.get("hm-map").add(new GoogleMapsSuggestBox());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接