Example usage for com.google.gwt.maps.client.geocoder Geocoder geocode

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

Introduction

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

Prototype

@Override
    public void geocode(HasGeocoderRequest request, GeocoderCallback callback) 

Source Link

Usage

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);// w w  w  .ja  va2 s .c om

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