LocationTextField
LocationTextField
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();
Links
Compatibility
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
LocationTextField - Vaadin Add-on Directory
LocationTextFieldIssue Tracker
Source Code
Discussion Forum
LocationTextField version 1.0.0
null
LocationTextField version 1.0.1
- Genericized LocationProvider interface to allow for specifying a subclass of GeocodedLocation
LocationTextField version 1.0.2
- Fix bug whereby LocationTextField would try to cast a value set by a Form to a GeocodedLocation when it might not be
LocationTextField version 1.0.3
* Fixed paging or search results
LocationTextField version 1.0.5
Fix bug that caused widget not to wait for text change timeout to fire updates to server
LocationTextField version 1.0.6
* Use generics in geocoders
* Google geocoder can now be used successfully (patch from Eric Bottard)
* OpenStreetMapGeocoder now has a limit field that can be used to retrieve > 10 results (their default)
* Null values are no longer allowed
* Added method to explicitly set the location of LocationTextField
* Added boolean flag to indicate if ENTER key should immediately trigger text change event
* Enhanced sample app
LocationTextField version 1.0.7
* Fixed some repainting issues
* Properly handle setting value to null (clear text field and container)
* Add convenience method for clearing text field and container
LocationTextField version 1.0.8
* Fixed repaint problem where a text change would occasionally set the old text in the text field instead of the newly entered text
* Added dependency on SLF4J
LocationTextField version 1.1.0
* Rewrite of current text handling and client notification code
* Fixed bug that would revert change when pressing TAB key inside the text field
LocationTextField version 1.1.1
Fixed bug whereby any whitespace in the text sent from the client was trimmed
LocationTextField version 1.1.2
Added better handling of state on ENTER and TAB
LocationTextField version 1.2.0
Add 3 new features:
* Ability to set minimum length of ext from client to initiate geocoding
* Add ability to override above feature by pressing ENTER key
* Add ability to limit results by setting limit on the underlying LocationProvider
LocationTextField version 1.2.1
Fixed bug whereby clicking text field then pressing enter key twice cleared field
LocationTextField version 1.2.2
Moved to Maven build. No code changes.
LocationTextField version 1.2.3
* Fixed a display bug
* Added ability to set an input prompt
* Added ability to turn debug (Firebug) logging on/off
LocationTextField version 1.2.4
* Fixed bug where input prompt did not disappear on click
LocationTextField version 1.2.6
* Fix bug whereby if a minimum text length was configured anything smaler than that value sent to the server after a text change timeout was causing the textbox on the client to revert to its previous value
* Fix problem where enter key was not selecting highlighted option if only one option is present
LocationTextField version 2.0.0
* Vaadin 7 version is finally here!
* New fluent API
* Complete design change to use the newer GWT `SuggestBox` instead of a `ComboBox`
** This change was inspired by the excellent AutocompleteField for Vaadin add-on
* New design alters behavior of previous Vaadin 6 version as there is no direct binding to a property with a type of `GeocodedLocation`; the field's value is always a String. Alternatively, you may provide a `Property` to the fluent API for use or you may add a `ValueChangeListener` using the method `addLocationValueChangeListener(ValueChangeListener)`.
LocationTextField version 2.0.1
* Fix bad import causing build problems for others
* Require class of generic type to always have a Property for get/set location object
* Add method to retrieve current location object
LocationTextField version 2.0.2
Fix bogus import statement causing people issues
LocationTextField version 2.0.3
Have connector extend AbstractFieldConnector since widget is an AbstractField
LocationTextField version 2.0.4
Don't popup menu with single selection when calling setLocation
LocationTextField version 2.0.5
- Make sure we mark as dirty on state changes
- Set property value to null on reset
LocationTextField version 2.1.0
- Added input prompt
- Added keyboard navigation of options
LocationTextField version 2.1.1
- Remove standard.css
- Add styles for selected item by default
- Fix ClassCastException on client
LocationTextField version 2.1.2
- Move check for minimum query length to server so server knows about recent changes in text box
LocationTextField version 2.1.4
Fixed issue where location select was not taking effect. Users of 2.1.3 only were effected.
LocationTextField version 2.1.5
- Compensated for margin gap when scrolling with keys
LocationTextField version 2.1.6
* Added hook method for sub-classes for executing custom code after location changes
LocationTextField version 2.1.7
* 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.
LocationTextField version 3.0.0
Support Vaadin 8
LocationTextField version 3.0.1
Fixed OpenStreetMap geocoder URL
LocationTextField version 2.1.8
LocationTextField version 3.0.2
- Added 'key' parameter to Google Geocoder. This parameter is now REQUIRED by Google to use the geocoder.