summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/BulkMarkerActivity.java
blob: d8fd428762e2f7bbddf986749295f14e3fbd1aa3 (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.mapbox.mapboxsdk.testapp.activity.annotation;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Style;
import com.mapbox.mapboxsdk.testapp.R;
import com.mapbox.mapboxsdk.testapp.utils.GeoParseUtil;
import timber.log.Timber;

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Test activity showcasing adding a large amount of Markers.
 */
public class BulkMarkerActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

  private MapboxMap mapboxMap;
  private MapView mapView;
  private List<LatLng> locations;
  private ProgressDialog progressDialog;

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

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

  private void initMap(MapboxMap mapboxMap) {
    this.mapboxMap =  mapboxMap;
    mapboxMap.setStyle(Style.MAPBOX_STREETS);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(
      this, R.array.bulk_marker_list, android.R.layout.simple_spinner_item);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    getMenuInflater().inflate(R.menu.menu_bulk_marker, menu);
    MenuItem item = menu.findItem(R.id.spinner);
    Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
    spinner.setAdapter(spinnerAdapter);
    spinner.setOnItemSelectedListener(BulkMarkerActivity.this);
    return true;
  }

  @Override
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    int amount = Integer.valueOf(getResources().getStringArray(R.array.bulk_marker_list)[position]);
    if (locations == null) {
      progressDialog = ProgressDialog.show(this, "Loading", "Fetching markers", false);
      new LoadLocationTask(this, amount).execute();
    } else {
      showMarkers(amount);
    }
  }

  private void onLatLngListLoaded(List<LatLng> latLngs, int amount) {
    progressDialog.hide();
    locations = latLngs;
    showMarkers(amount);
  }

  private void showMarkers(int amount) {
    if (mapboxMap == null || locations == null || mapView.isDestroyed()) {
      return;
    }

    mapboxMap.clear();
    if (locations.size() < amount) {
      amount = locations.size();
    }

    showGlMarkers(amount);
  }

  private void showGlMarkers(int amount) {
    List<MarkerOptions> markerOptionsList = new ArrayList<>();
    DecimalFormat formatter = new DecimalFormat("#.#####");
    Random random = new Random();
    int randomIndex;

    for (int i = 0; i < amount; i++) {
      randomIndex = random.nextInt(locations.size());
      LatLng latLng = locations.get(randomIndex);
      markerOptionsList.add(new MarkerOptions()
        .position(latLng)
        .title(String.valueOf(i))
        .snippet(formatter.format(latLng.getLatitude()) + ", " + formatter.format(latLng.getLongitude())));
    }

    mapboxMap.addMarkers(markerOptionsList);
  }

  @Override
  public void onNothingSelected(AdapterView<?> parent) {
    // nothing selected, nothing to do!
  }

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

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

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

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

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

  @Override
  protected void onDestroy() {
    super.onDestroy();
    if (progressDialog != null) {
      progressDialog.dismiss();
    }
    mapView.onDestroy();
  }

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

  private static class LoadLocationTask extends AsyncTask<Void, Integer, List<LatLng>> {

    private WeakReference<BulkMarkerActivity> activity;
    private int amount;

    private LoadLocationTask(BulkMarkerActivity activity, int amount) {
      this.amount = amount;
      this.activity = new WeakReference<>(activity);
    }

    @Override
    protected List<LatLng> doInBackground(Void... params) {
      BulkMarkerActivity activity = this.activity.get();
      if (activity != null) {
        String json = null;
        try {
          json = GeoParseUtil.loadStringFromAssets(activity.getApplicationContext(), "points.geojson");
        } catch (IOException exception) {
          Timber.e(exception, "Could not add markers");
        }

        if (json != null) {
          return GeoParseUtil.parseGeoJsonCoordinates(json);
        }
      }
      return null;
    }

    @Override
    protected void onPostExecute(List<LatLng> locations) {
      super.onPostExecute(locations);
      BulkMarkerActivity activity = this.activity.get();
      if (activity != null) {
        activity.onLatLngListLoaded(locations, amount);
      }
    }
  }
}