Get user geolocation from browser

Hi

I need to get geolocation information from user’s device. I have found this documentation for html5 HTML Geolocation API and it seems easy.

Where do I start in integrating this with cuba? I would like to read the geolocation when a user opens a screen.

Kind regards,
George

1 Like

Hi,

In fact, there is a Vaadin add-on for obtaining geolocation, but it’s outdated and doesn’t support Vaadin 7, so I would recommend you to write custom component from the scratch. You can read more about creating custom visual components in the documentation. I would recommend you to pay attention to this topic.

Regards,
Gleb

Thank you Gleb for your input

I have managed to read coordinates like this


public class Screentest extends AbstractWindow {

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);


        JavaScript.getCurrent().addFunction("com.company.spica",
                arguments -> {
                  //  final long timeInMillis = (long) arguments.getNumber(0);

                    final BigDecimal latitudine = new BigDecimal(arguments.getNumber(0)) ;
                    final BigDecimal longitudine = new BigDecimal(arguments.getNumber(1)) ;
                    Notification.show("Am primit coordonatele " + latitudine + " " +  longitudine, Notification.Type.TRAY_NOTIFICATION);
                });

        JavaScript.getCurrent().execute("getLocation();\n" +
                "         function getLocation(){\n" +
                "\n" +
                "            if(navigator.geolocation){\n" +
                "               // timeout at 60000 milliseconds (60 seconds)\n" +
                "               var options = {timeout:60000};\n" +
                "               navigator.geolocation.getCurrentPosition(showLocation, errorHandler, options);\n" +
                "            }\n" +
                "\n" +
                "            else{\n" +
                "               alert(\"Sorry, browser does not support geolocation!\");\n" +
                "            }\n" +
                "         }\n" +
                "\t\t \n" +
                "\t\t          function errorHandler(err) {\n" +
                "            if(err.code == 1) {\n" +
                "               alert(\"Error: Access is denied!\");\n" +
                "            }\n" +
                "\n" +
                "            else if( err.code == 2) {\n" +
                "               alert(\"Error: Position is unavailable!\");\n" +
                "            }\n" +
                "         }\n" +
                "\t\t \n" +
                "\t\t \t function showLocation(position) {\n" +
                "            var latitude = position.coords.latitude;\n" +
                "            var longitude = position.coords.longitude;\n" +
                "            alert(\"Latitude : \" + latitude + \" Longitude: \" + longitude);\n" +
                "\t\t\tcom.company.spica(latitude,longitude)\n" +
                "         }");
    }
}

Hi George
Interesting… Does it work when the need is to track location of the device (say cell phone) from another location?