Directory

← Back

LocationTextField

LocationTextField

Author

Contributors

Rating

LocationTextField is an add-on that geocodes addresses using a configurable geocoder. Any class implementing org.vaadin.addons.locationtextfield.LocationProvider can be used to geocode addresses with LocationTextField.

At a minimum an instance of LocationProvider is necessary to use this add-on. There are currently two LocationProvider implementations provided; one using Google's geocoder and the other using OpenStreetMap's Nominatim. Adding new implementations is trivial. Results for the geocoded address are available to choose from in a drop-down menu.

Configuration can be done via typical setters or via the new fluent API in version 2.0.0+.

Note: It is the user's responsibility to make sure he or she abides by any rules or requirements for geocoder use. For instance, Google's geocoder will not even work unless it is used in a browser with GMaps in the DOM. OpenStreetMap has a usage limit upon which they will cut you off; however, MapQuest uses the OpenStreetMap code and has no limit. MapQuest should be added next.

Sample code

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.Title;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.data.Property;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

import javax.servlet.annotation.WebServlet;

import org.vaadin.addons.locationtextfield.GeocodedLocation;
import org.vaadin.addons.locationtextfield.LocationTextField;
import org.vaadin.addons.locationtextfield.OpenStreetMapGeocoder;

@Theme("demo")
@Title("LocationTextField Add-on Demo")
@SuppressWarnings("serial")
public class DemoUI extends UI
{

    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = DemoUI.class, widgetset = "org.vaadin.addons.ltf.demo.DemoWidgetSet")
    public static class Servlet extends VaadinServlet {
    }

    @Override
    protected void init(VaadinRequest request) {

        VerticalLayout vl = new VerticalLayout();
        vl.setSizeUndefined();
        vl.setWidth("50%");
        vl.setSpacing(true);
        vl.setMargin(true);

        final OpenStreetMapGeocoder geocoder = OpenStreetMapGeocoder.getInstance();
        geocoder.setLimit(25);
        final LocationTextField<GeocodedLocation> ltf = new LocationTextField<GeocodedLocation>(geocoder, GeocodedLocation.class);
        ltf.setCaption("Address: ");
        ltf.setWidth("100%");
        ltf.setImmediate(true);
        //ltf.setAutoSelectionEnabled(false);
        vl.addComponent(ltf);

        final TextField lat = new TextField("Latitude: ");
        final TextField lon = new TextField("Longitude: ");
        vl.addComponent(lat);
        vl.addComponent(lon);

        ltf.addLocationValueChangeListener(new Property.ValueChangeListener() {
            public void valueChange(Property.ValueChangeEvent event) {
                GeocodedLocation loc = (GeocodedLocation)event.getProperty().getValue();
                if (loc != null) {
                    lat.setValue("" + loc.getLat());
                    lon.setValue("" + loc.getLon());
                } else {
                    lat.setValue("");
                    lon.setValue("");
                }
            }
        });

        Button b = new Button("New York City, NY", new Button.ClickListener() {
            public void buttonClick(Button.ClickEvent event) {
                ltf.geocode("New York City, NY");
            }
        });
        vl.addComponent(b);

        setContent(vl);
    }
}
final LocationTextField<GeocodedLocation> ltf = LocationTextField.<GeocodedLocation>newBuilder()
    .withCaption("Address:")
    .withDelayMillis(1200)
    .withType(GeocodedLocation.class.class)
    .withInitialValue(someLocation)
    .withLocationProvider(OpenStreetMapGeocoder.getInstance())
    .withMinimumQueryCharacters(5)
    .withWidth("100%")
    .withHeight("40px")
    .withImmediate(true)
    .build();

Compatibility

(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.

Version

  • Always update text of widget on state changes to workaround issue whereby Vaadin does not recognize when the text has been changed then changed back to the original text in the same update.
Released
2016-03-21
Maturity
STABLE
License
Apache License 2.0

Compatibility

Framework
Vaadin 7.2+
Vaadin 6.8 in 1.2.3
Vaadin 6.7 in 1.2.3
Vaadin 6.6 in 1.2.3
Vaadin 6.6+ in 1.0.0
Vaadin 7.0+ in 2.1.8
Vaadin 8.0+ in 3.0.0
Browser
Firefox
Opera
Safari
Google Chrome
iOS Browser
Android Browser
Internet Explorer
Internet Explorer

Vaadin Add-on Directory

Find open-source widgets, add-ons, themes, and integrations for your Vaadin application. Vaadin Add-on Directory
The channel for finding, promoting, and distributing Vaadin add-ons.
Online