Example usage for com.google.gwt.maps.client.geocoder GeocoderCallback GeocoderCallback

List of usage examples for com.google.gwt.maps.client.geocoder GeocoderCallback GeocoderCallback

Introduction

In this page you can find the example usage for com.google.gwt.maps.client.geocoder GeocoderCallback GeocoderCallback.

Prototype

GeocoderCallback

Source Link

Usage

From source file:com.google.mobile.trippy.web.client.presenter.CreateNewTripPresenter.java

License:Apache License

/**
 * Creates the trip by providing the location trip using service.
 *///from w  ww.j a va  2s.co  m
void createTrip(final String location) {
    final HasGeocoderRequest geocoderRequest = geocoderRequestProvider.get();
    geocoderRequest.setAddress(location);
    singletonComponents.getToast().showLoading(singletonComponents.getMessage().toastMsgResolvingName());
    geocoder.geocode(geocoderRequest, new GeocoderCallback() {

        @Override
        public void callback(final List<HasGeocoderResult> responses, final String status) {
            singletonComponents.getToast().hideLoading();

            switch (responses.size()) {
            case 0: // no locations found
                singletonComponents.getToast().showToast(singletonComponents.getMessage().noTripLocations(),
                        STATUS_TIME_MILLIS);
                break;

            case 1: // one location found create trip directly
                final HasGeocoderGeometry geometry = responses.get(0).getGeometry();
                if (geometry.getBounds() != null) {
                    final String address = responses.get(0).getAddressComponents().get(0).getLongName();
                    addTripToDb(location, address, geometry.getBounds());
                } else {
                    singletonComponents.getToast().showToast(singletonComponents.getMessage().noTripLocations(),
                            STATUS_TIME_MILLIS);
                }
                break;

            default: // more than one location found, allow user to choose one
                if (registerSuggestedHandlers(responses)) {
                    display.showPopup();
                } else {
                    singletonComponents.getToast().showToast(singletonComponents.getMessage().noTripLocations(),
                            STATUS_TIME_MILLIS);
                }
                break;
            }
        }
    });
}

From source file:com.mashery.examples.api.client.ActiveExample.java

License:Open Source License

public ActiveExample(final PopupMapWidget mapWidget) {
    FlowPanel panel = new FlowPanel();

    panel.add(new HTML("<h1>Search</h1>"));

    FormPanel form = new FormPanel();
    panel.add(form);/*from ww  w.j a  v  a  2 s.c  o m*/

    FlexTable entryGrid = new FlexTable();
    form.add(entryGrid);
    FlexTable.FlexCellFormatter formatter = (FlexCellFormatter) entryGrid.getCellFormatter();
    entryGrid.setWidget(0, 0, new Label("Keywords:"));
    formatter.setHorizontalAlignment(0, 0, HasHorizontalAlignment.ALIGN_RIGHT);

    final TextBox keywordsText = new TextBox();
    entryGrid.setWidget(0, 1, keywordsText);
    keywordsText.setWidth("200px");

    entryGrid.setWidget(1, 0, new Label("Location:"));
    formatter.setHorizontalAlignment(1, 0, HasHorizontalAlignment.ALIGN_RIGHT);

    final TextBox locationText = new TextBox();
    entryGrid.setWidget(1, 1, locationText);
    locationText.setWidth("200px");

    Anchor fromMapLink = new Anchor("<- from Map");
    final Geocoder geocoder = new Geocoder();
    fromMapLink.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            mapWidget.show();
            HasLatLng center = mapWidget.getMap().getCenter();
            GeocoderRequest request = new GeocoderRequest();
            request.setLatLng(center);
            geocoder.geocode(request, new GeocoderCallback() {
                @Override
                public void callback(List<HasGeocoderResult> responses, String status) {
                    if (responses != null) {
                        String postalCode = null;
                        String country = null;
                        RESULTS: for (HasGeocoderResult result : responses) {
                            if (result.getTypes().contains("postal_code")) {
                                for (HasAddressComponent addr : result.getAddressComponents()) {
                                    if (postalCode == null && addr.getTypes().contains("postal_code"))
                                        postalCode = addr.getLongName();

                                    if (country == null && addr.getTypes().contains("country"))
                                        country = addr.getLongName();

                                    if (postalCode != null && country != null)
                                        break RESULTS;
                                }
                            }
                        }

                        if (postalCode != null) {
                            if (country == null)
                                locationText.setText(postalCode);
                            else
                                locationText.setText(postalCode + ", " + country);
                        }
                    }
                }
            });
        }
    });

    entryGrid.setWidget(1, 2, fromMapLink);

    SubmitButton submitButton = new SubmitButton("Search");
    entryGrid.setWidget(2, 0, submitButton);
    formatter.setColSpan(2, 0, 3);
    formatter.setHorizontalAlignment(2, 0, HasHorizontalAlignment.ALIGN_RIGHT);

    final SearchResultsTable table = new SearchResultsTable(10);
    panel.add(table);
    table.setWidth("500px");

    form.addSubmitHandler(new FormPanel.SubmitHandler() {
        @Override
        public void onSubmit(SubmitEvent event) {
            String keywords = keywordsText.getValue().trim();
            String location = locationText.getValue().trim();
            table.loadData(keywords.length() == 0 ? null : keywords, location.length() == 0 ? null : location);
            event.cancel();
        }
    });

    infoPanel = new PopupPanel(true);
    infoPanel.setAutoHideOnHistoryEventsEnabled(true);
    infoGrid = new FlexTable();
    infoPanel.setWidget(infoGrid);
    FlowPanel linkPanel = new FlowPanel();
    infoGrid.setWidget(3, 0, linkPanel);

    Anchor mapLink = new Anchor("Map", "#");
    linkPanel.add(mapLink);
    mapLink.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            event.preventDefault();
            if (selectedResult == null)
                return;

            Meta meta = selectedResult.getMeta();
            if (meta == null) {
                Window.alert("No location available.");
                return;
            }

            MarkerOptions opt = new MarkerOptions();
            if (meta.getAssetName() != null)
                opt.setTitle(meta.getAssetName().trim());

            opt.setPosition(new LatLng(meta.getLatitude(), meta.getLongitude()));
            opt.setClickable(true);
            opt.setVisible(true);
            mapWidget.show(new Marker(opt));
        }
    });

    mapWidget.addAutoHidePartner(mapLink.getElement());

    linkPanel.add(new InlineHTML("&nbsp;|&nbsp;"));
    linkPanel.add(visitLink = new Anchor("Visit", "#"));

    ((FlexTable.FlexCellFormatter) infoGrid.getCellFormatter()).setColSpan(1, 0, 2);
    ((FlexTable.FlexCellFormatter) infoGrid.getCellFormatter()).setColSpan(2, 0, 2);
    ((FlexTable.FlexCellFormatter) infoGrid.getCellFormatter()).setColSpan(3, 0, 2);

    initWidget(new ScrollPanel(panel));
}