如何根据融合位置API获得的当前速度缩放地图

时间:2019-02-11 15:32:37

标签: android google-maps

我正在使用融合位置API来获取用户的当前位置并计算速度,如下所示。

    LocationCallback mLocationCallback = new LocationCallback(){
    private Location lastLocation = null;
    private double calculatedSpeed = 0;
    @Override
    public void onLocationResult(LocationResult locationResult) {
        Location location = locationResult.getLastLocation();
        if (lastLocation != null) {
            double elapsedTime = (location.getTime() - lastLocation.getTime()) / 1_000; // Convert milliseconds to seconds
            calculatedSpeed = lastLocation.distanceTo(location) / elapsedTime;

        }
        this.lastLocation = location;
        double speed = location.hasSpeed() ? location.getSpeed() : calculatedSpeed;
        }
    };

使用计算出的速度,我想在速度慢时放大,而在速度高时缩小,就像Google Maps App中发生的那样。这是用于四轮车跟踪应用程序。

非常感谢您提出任何实现建议,谢谢!

解决方案

解决方案是根据获得的当前速度值手动设置缩放级别。

    float zoom;
    if(speed < 5)
        zoom = 18;
    else if(speed < 10)
        zoom = (float) 17.5;
    else if(speed < 15)
        zoom = 17;
    else if(speed < 20)
        zoom = (float) 16.5;
    else if(speed < 25)
        zoom = 16;
    else
        zoom = 15;

    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(myLatLng)
            .zoom(zoom)
            .tilt(0)
            .build();
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

0 个答案:

没有答案