gpt4 book ai didi

google-app-engine - 在 GWT/Java 的 SuggestBox 中建议地址

转载 作者:太空宇宙 更新时间:2023-11-03 15:31:41 26 4
gpt4 key购买 nike

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

我想,我需要使用 Geocoder.getLocations(String address, LocationCallback callback) method ,但我不知道如何将它与 oracle 连接,建议框需要 oracle 来产生它的建议。

请问如何将 getLocations 方法与 SuggestOracle 连接起来?

最佳答案

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

所以这是我的解决方案:

首先,我们为带有 Google map 建议的 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() 方法中编写以下行,将新的小部件绑定(bind)到您的网页中:

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

关于google-app-engine - 在 GWT/Java 的 SuggestBox 中建议地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8479603/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com