summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/style/GridSourceActivity.java
blob: 9dda0f8fa2b1d64fee197026a605901f5727f9a1 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package com.mapbox.mapboxsdk.testapp.activity.style;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;

import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.style.layers.LineLayer;
import com.mapbox.mapboxsdk.style.sources.CustomGeometrySource;
import com.mapbox.mapboxsdk.style.sources.GeometryTileProvider;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.services.commons.geojson.Feature;
import com.mapbox.services.commons.geojson.FeatureCollection;
import com.mapbox.services.commons.geojson.MultiLineString;
import com.mapbox.services.commons.models.Position;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;

/**
 * Test activity showcasing using CustomGeometrySource to create a grid overlay on the map.
 */
public class GridSourceActivity extends AppCompatActivity implements OnMapReadyCallback {

  private static final String ID_GRID_SOURCE = "grid_source";
  private static final String ID_GRID_LAYER = "grid_layer";

  private MapView mapView;
  private MapboxMap mapboxMap;

  /**
   * Implementation of GeometryTileProvider that returns features representing a zoom-dependent
   * grid.
   */
  static class GridProvider implements GeometryTileProvider {
    public FeatureCollection getFeaturesForBounds(LatLngBounds bounds, int zoom) {
      List<Feature> features = new ArrayList<>();
      double gridSpacing;
      if (zoom >= 13) {
        gridSpacing = 0.01;
      } else if (zoom >= 11) {
        gridSpacing = 0.05;
      } else if (zoom == 10) {
        gridSpacing = .1;
      } else if (zoom == 9) {
        gridSpacing = 0.25;
      } else if (zoom == 8) {
        gridSpacing = 0.5;
      } else if (zoom >= 6) {
        gridSpacing = 1;
      } else if (zoom == 5) {
        gridSpacing = 2;
      } else if (zoom >= 4) {
        gridSpacing = 5;
      } else if (zoom == 2) {
        gridSpacing = 10;
      } else {
        gridSpacing = 20;
      }

      List gridLines = new ArrayList();
      for (double y = Math.ceil(bounds.getLatNorth() / gridSpacing) * gridSpacing;
           y >= Math.floor(bounds.getLatSouth() / gridSpacing) * gridSpacing; y -= gridSpacing) {
        gridLines.add(Arrays.asList(Position.fromCoordinates(bounds.getLonWest(), y),
          Position.fromCoordinates(bounds.getLonEast(), y)));
      }
      features.add(Feature.fromGeometry(MultiLineString.fromCoordinates(gridLines)));

      gridLines = new ArrayList();
      for (double x = Math.floor(bounds.getLonWest() / gridSpacing) * gridSpacing;
           x <= Math.ceil(bounds.getLonEast() / gridSpacing) * gridSpacing; x += gridSpacing) {
        gridLines.add(Arrays.asList(Position.fromCoordinates(x, bounds.getLatSouth()),
          Position.fromCoordinates(x, bounds.getLatNorth())));
      }
      features.add(Feature.fromGeometry(MultiLineString.fromCoordinates(gridLines)));

      return FeatureCollection.fromFeatures(features);
    }
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_grid_source);

    mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);
    mapView.getMapAsync(this);
  }

  @Override
  public void onMapReady(@NonNull final MapboxMap map) {
    mapboxMap = map;

    // add source
    CustomGeometrySource source = new CustomGeometrySource(ID_GRID_SOURCE, new GridProvider());
    mapboxMap.addSource(source);

    // add layer
    LineLayer layer = new LineLayer(ID_GRID_LAYER, ID_GRID_SOURCE);
    layer.setProperties(
      lineColor(Color.parseColor("#000000"))
    );

    mapboxMap.addLayer(layer);
  }

  @Override
  protected void onStart() {
    super.onStart();
    mapView.onStart();
  }

  @Override
  public void onResume() {
    super.onResume();
    mapView.onResume();
  }

  @Override
  public void onPause() {
    super.onPause();
    mapView.onPause();
  }

  @Override
  protected void onStop() {
    super.onStop();
    mapView.onStop();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
  }

}