1 min read

Adding GWT Geolocation support to Vaadin

Adding GWT Geolocation support to Vaadin
Photo by Tamas Tuzes-Katai / Unsplash

If you're looking to add geolocation support to your Vaadin applications, I've whipped up a quick Vaadin widget to add support for the gwt-mobile-webkit Geolocation API feature.

I created a simple module in Eclipse with the Vaadin plugin and dropped the gwt-mobile-webkit Geolocation API feature into my project and worked up the server/client classes to pass back the longitude, latitude, and accuracy position information.

Here is some sample code demonstrating use in your application:

package org.vaadin.geolocation; 
import java.util.Date; 
import java.util.Map; 
import com.vaadin.Application; 
import com.vaadin.ui.Button; 
import com.vaadin.ui.Label; 
import com.vaadin.ui.Window; 
import com.vaadin.ui.Button.ClickEvent; 

public class GeolocationApplication extends Application { 
    private static final long serialVersionUID = 6180642880984113485L;
    private Geolocation geolocation; private Label currentLocation;
    @Override public void init() { 
        Window mainWindow = new Window("Geolocation Application"); 
        // Add current location label 
        currentLocation = new Label("Unknown");

        // Add Geolocation support 
        geolocation = new Geolocation();
        geolocation.addListener(new Geolocation.UpdateListener() {
            @Override public void locationUpdated(Geolocation geolocation) { 
                if(geolocation.isSupported()) { 
                    Map	pos = geolocation.getPosition(); 
                    double lat = pos.get("latitude"); 
                    double log = pos.get("longitude"); 
                    double acc = pos.get("accuracy");
                    currentLocation.setValue( "Lat: " + 
                        Double.toString(lat) + ", Long: " + 
                        Double.toString(log) + ", within " + 
                        Double.toString(acc) + " at " + 
                        new Date().toString()
                } else { 
                    currentLocation.setValue("Unsupported browser!"); 
        // Add refresh button 
        Button refresh = new Button(
            new Button.ClickListener() { 
                public void buttonClick(ClickEvent event) { 							geolocation.getLocation(); 

You can download the source here: org.vaadin.geolocation

You'll have to preview it on a browser with location services Firefox 3.5 or above.

This is about my second day of Vaadin development so please excuse and glaring bugs or missed best practices.

I used some of the examples for the Vaadin contrib repository to help springboard this undertaking and have to send thanks to the Vaadin forum users for their tips.

Any comments/suggestions are welcome below.