Suggest Addresses in a SuggestBox in GWT/Java
Asked Answered
C

1

3

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?

Cracy answered 12/12, 2011 at 19:19 Comment(0)
C
8

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());
Cracy answered 14/12, 2011 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.