在GWT / Java的SuggestBox中建议地址 [英] Suggest Addresses in a SuggestBox in GWT/Java

查看:88
本文介绍了在GWT / Java的SuggestBox中建议地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义一个 SuggestBox ,其行为与 Google地图:开始输入时,会出现以输入字母开头的实际地址。

我认为,我需要使用 Geocoder.getLocations(String address,LocationCallback callback)方法,但我不知道如何将它与提示框所需的oracle联系起来以提出建议。



您可以给我一些想法,我该如何将 getLocations 方法与 SuggestOracle SuggestBox ,它有自己的 SuggestOracle AddressOracle 作为Google地图服务的封装器处理,对于GWT的Google Maps API中的类 Geocoder 提供抽象化功能。



这是我的解决方案:

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

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


$ / code>

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

  class AddressOracle扩展SuggestOracle {

//需要此实例来调用getLocations-Service
私有最终Geocoder geocoder;


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

$ b @Override
public void requestSuggestions(final Request request,$ b $ final callback callback){
//这是字符串,用户有键入到目前为止
String addressQuery = request.getQuery();
//查找建议,只有至少输入2个字母
if(addressQuery.length()> 2){
geocoder.getLocations(addressQuery,new LocationCallback() {

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

@Override
public void onSuccess(JsArray< Placemark> places){
//从
// getLocations-Service
Collection< Suggestion> result = new LinkedList<建议>();
for(int i = 0; i< places.length(); i ++){
String address = places.get(i).getAddress();
AddressSuggestion newSuggestion =新的AddressSuggestion(
地址);
result.add(newSuggestion);
}
响应响应=新响应(结果);
callback.onSuggestionsReady(request,response);
}

});

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

}
}

是oracle建议的一个特殊类,它只是一个带有地址的字符串。

  class AddressSuggestion实现了SuggestOracle.Suggestion,可串行化{

private static final long serialVersionUID = 1L;

字符串地址;

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

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

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






$ b现在你可以将新的小部件绑定到你的网页中通过在入口点 - 类的 onModuleLoad() - 方法中写入以下行:

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


I want to define a SuggestBox, which behaves like the search bar in Google Maps: When you begin to type, real addresses, starting with the typed letters, appear.

I think, that I need to use the Geocoder.getLocations(String address, LocationCallback callback) method, but I have no idea how to connect this with the oracle, which is needed by the suggest box to produce its suggestions.

Can you please give me ideas how do I connect the getLocations Method with the SuggestOracle?

解决方案

I solved this by implementing a subclass of SuggestBox, which has it's own SuggestOracle. The AddressOracle deals as a Wrapper for the Google Maps Service, for which the class Geocoderin the Google Maps API for GWT offers abstractions.

So here is my solution:

First we implement the Widget for a SuggestBox with Google Maps suggestions

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

Then we implement the SuggestOracle, which wraps the Geocoder async method abstractions:

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

    }
}

And this is a special class for the oracle suggestions, which just represent a String with the delivered address.

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

Now you can bind the new widget into your web page by writing the following line in the onModuleLoad()-method of your EntryPoint-class:

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

这篇关于在GWT / Java的SuggestBox中建议地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆