Vaadin 24 stop Geolocation maplibre

31 Views Asked by At

Good day, I develop with Vaadin 24, Spring Boot 3.2.4 and maplibre. enter link description here Excerpt from my pom.

    <dependency>
        <groupId>in.virit</groupId>
        <artifactId>viritin</artifactId>
        <version>2.7.1</version>
    </dependency>
    <dependency>
        <groupId>org.parttio</groupId>
        <artifactId>maplibre</artifactId>
        <version>0.0.6</version>
    </dependency>
    <dependency>
        <groupId>org.parttio</groupId>
        <artifactId>tinymce-for-flow</artifactId>
        <version>4.0.2</version>
    </dependency>
    <dependency>
        <groupId>org.parttio</groupId>
        <artifactId>pulltorefresh</artifactId>
        <version>0.0.1</version>
    </dependency>

how do I stop a started geolocation? As soon as I want to stop the geolocation, the input fields continue to be filled.

Code to start geolocation:

        start.addClickListener(e -> {
        ...

        Geolocation.watchPosition(position -> {
            longitude =position.getCoords().getLongitude();
            latitude =position.getCoords().getLatitude();
            txtLongitude.setValue(String.valueOf(longitude));
            txtLatitude.setValue(String.valueOf(latitude));
           
            if( position.getCoords().getAltitude()!=null) {
                altitude = position.getCoords().getAltitude() -47.0;
                txtAltitude.setValue(String.valueOf(altitude));
            }else{
                txtAltitude.setValue("0.0" );
            }
            if( position.getCoords().getAltitudeAccuracy()!=null) {
                genauigkeitAltitude = position.getCoords().getAltitudeAccuracy();
                txtGenauigkeitAltitude.setValue(String.valueOf(genauigkeitAltitude));
            }else{
                txtGenauigkeitAltitude.setValue("0.0" );
            }
            if( position.getCoords().getSpeed()!=null) {
                speed = position.getCoords().getSpeed();
                txtSpeed.setValue(String.valueOf(speed));
            }else{
                txtSpeed.setValue("0.0" );
            }
        
        }, error -> {
            log.error("No Geolocation: {})",error);
            Notification.show("No Geolocation");
            horizontalLayout.add(stop);
        },new GeolocationOptions(true,5000, 0));
    });
1

There are 1 best solutions below

0
Patrick Lange On BEST ANSWER

I solved the problem:

Button starten = new Button("start");
    starten.addThemeVariants(ButtonVariant.LUMO_PRIMARY);
    starten.addClickListener(e -> {
        if (geolocation == null) {
            geolocation = Geolocation.watchPosition(
                    event -> {
                        log.debug(Instant.ofEpochMilli(event.getTimestamp()) + " : " + event.getCoords());
                        double h = 0.0;
                        double s = 0.0;
                        if (event.getCoords().getAccuracy() != null) {
                            h = Math.floor((event.getCoords().getAccuracy()  * 100 / 100) - 47.0);
                        }
                        if (event.getCoords().getSpeed() != null) {
                            s = Math.floor((event.getCoords().getSpeed()  * 100 / 100) - 47.0);
                        }
                        updateMyLocation(event.getCoords().getLatitude(), event.getCoords().getLongitude(), h, s);
                    },
                    browserError -> {
                        Notification.show("ERROR, code: %s, msg: %s".formatted(browserError.intern(), browserError));
                    },
                    new GeolocationOptions(enableHighAccuracy.getValue(), timeout.getValue(), maximumAge.getValue())
            );
            starten.setText("stop");
            abstractSinglePropertyFields.forEach(c -> c.setEnabled(false));
        } else {
            starten.setText("start");
            abstractSinglePropertyFields
                    .forEach(c -> c.setEnabled(true));
        }
    });