summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/widgets/scaleview/MapScaleModel.java
blob: 2245d1d40a086e7e07424cddbc6621342de67f51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.mapbox.mapboxsdk.maps.widgets.scaleview;

import android.graphics.PointF;

import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.Projection;


class MapScaleModel {

    private final int[] meters = {
            150,350,700, 1500, 2500, 5000, 10000, 20000, 40000, 100000, 150000, 350000, 700000, 1500000, 2500000};

    private final LatLng src = new LatLng(31, 121);

    private int maxWidth;

    private double zoom = -1;
    private Scale scale;

    MapScaleModel() {
    }

    void setMaxWidth(int width) {
        maxWidth = width;
    }

    Scale setProjection(Projection projection, CameraPosition cameraPosition) {
        if (zoom == cameraPosition.zoom) return scale;

        int distance = 0;
        int distanceIndex = meters.length;
        double screenDistance = maxWidth + 1;

        while (screenDistance > maxWidth && distanceIndex > 0) {
            distance = meters[--distanceIndex];

            LatLng dest = DistanceUtils.translatePoint(src, distance, 120);

            PointF pointSrc = projection.toScreenLocation(src);
            PointF pointDest = projection.toScreenLocation(dest);

            screenDistance = Math.sqrt(Math.pow(pointSrc.x - pointDest.x, 2) + Math.pow(pointSrc.y - pointDest.y, 2));
        }

        zoom = cameraPosition.zoom;
        scale = new Scale(text(distance), (float) screenDistance);

        return scale;
    }

    private String text(int distance) {
        if (distance < 1000) return distance + " m";
        else return distance / 1000 + " km";
    }
}