Vaadin 24 站地理定位地图库

问题描述 投票:0回答:1

美好的一天, 我使用 Vaadin 24、Spring Boot 3.2.4 和 maplibre 进行开发。 在此输入链接描述 摘自我的 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>

如何停止已启动的地理定位?一旦我想停止地理定位,输入字段就会继续被填充。

启动地理定位的代码:

        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));
    });
geolocation vaadin maplibre-gl
1个回答
0
投票

我解决了问题:

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));
        }
    });
© www.soinside.com 2019 - 2024. All rights reserved.