I solved this by running a subclass of SuggestBox that has its SuggestOracle . AddressOracle used as a wrapper for the Google Maps service, for which the Geocoder class in the Google Maps API for GWT offers abstractions.
So here is my solution:
First we will implement a widget for a proposal with offers of Google Maps
public class GoogleMapsSuggestBox extends SuggestBox { public GoogleMapsSuggestBox() { super(new AddressOracle()); } }
Then we implement the sentence, which wraps the abstractions of the Geocoder asynchronous method:
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 oracle sentences that simply represent a string with a given 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 to your web page by writing the following line in the onModuleLoad() method of your EntryPoint -class:
RootPanel.get("hm-map").add(new GoogleMapsSuggestBox());
Georgy dobrev
source share