|
| 1 | +package com.mapbox.mapboxsdk.plugins.testapp.activity.annotation; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.graphics.Color; |
| 5 | +import android.os.AsyncTask; |
| 6 | +import android.os.Bundle; |
| 7 | + |
| 8 | +import androidx.appcompat.app.AppCompatActivity; |
| 9 | +import androidx.core.util.Pair; |
| 10 | + |
| 11 | +import com.mapbox.geojson.Feature; |
| 12 | +import com.mapbox.geojson.FeatureCollection; |
| 13 | +import com.mapbox.geojson.Point; |
| 14 | +import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; |
| 15 | +import com.mapbox.mapboxsdk.geometry.LatLng; |
| 16 | +import com.mapbox.mapboxsdk.maps.MapView; |
| 17 | +import com.mapbox.mapboxsdk.maps.MapboxMap; |
| 18 | +import com.mapbox.mapboxsdk.maps.Style; |
| 19 | +import com.mapbox.mapboxsdk.plugins.annotation.ClusterOptions; |
| 20 | +import com.mapbox.mapboxsdk.plugins.annotation.Symbol; |
| 21 | +import com.mapbox.mapboxsdk.plugins.annotation.SymbolManager; |
| 22 | +import com.mapbox.mapboxsdk.plugins.annotation.SymbolOptions; |
| 23 | +import com.mapbox.mapboxsdk.plugins.testapp.R; |
| 24 | + |
| 25 | +import java.io.BufferedReader; |
| 26 | +import java.io.IOException; |
| 27 | +import java.io.InputStream; |
| 28 | +import java.io.InputStreamReader; |
| 29 | +import java.io.Reader; |
| 30 | +import java.lang.ref.WeakReference; |
| 31 | +import java.nio.charset.Charset; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Random; |
| 35 | + |
| 36 | +import timber.log.Timber; |
| 37 | + |
| 38 | +/** |
| 39 | + * Test activity showcasing adding a large amount of Symbols with a cluster configuration. |
| 40 | + */ |
| 41 | +public class ClusterSymbolActivity extends AppCompatActivity { |
| 42 | + |
| 43 | + private SymbolManager symbolManager; |
| 44 | + private List<Symbol> symbols = new ArrayList<>(); |
| 45 | + |
| 46 | + private MapboxMap mapboxMap; |
| 47 | + private MapView mapView; |
| 48 | + private FeatureCollection locations; |
| 49 | + |
| 50 | + @Override |
| 51 | + protected void onCreate(Bundle savedInstanceState) { |
| 52 | + super.onCreate(savedInstanceState); |
| 53 | + setContentView(R.layout.activity_cluster); |
| 54 | + |
| 55 | + mapView = findViewById(R.id.mapView); |
| 56 | + mapView.onCreate(savedInstanceState); |
| 57 | + mapView.getMapAsync(this::initMap); |
| 58 | + } |
| 59 | + |
| 60 | + private void initMap(MapboxMap mapboxMap) { |
| 61 | + this.mapboxMap = mapboxMap; |
| 62 | + mapboxMap.moveCamera( |
| 63 | + CameraUpdateFactory.newLatLngZoom( |
| 64 | + new LatLng(38.87031, -77.00897), 10 |
| 65 | + ) |
| 66 | + ); |
| 67 | + |
| 68 | + ClusterOptions clusterOptions = new ClusterOptions() |
| 69 | + .withColorLevels(new Pair[] { |
| 70 | + new Pair(100, Color.RED), |
| 71 | + new Pair(50, Color.BLUE), |
| 72 | + new Pair(0, Color.GREEN) |
| 73 | + }); |
| 74 | + |
| 75 | + mapboxMap.setStyle(new Style.Builder().fromUri(Style.MAPBOX_STREETS), style -> { |
| 76 | + symbolManager = new SymbolManager(mapView, mapboxMap, style, null, clusterOptions); |
| 77 | + symbolManager.setIconAllowOverlap(true); |
| 78 | + loadData(); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + private void loadData() { |
| 83 | + int amount = 10000; |
| 84 | + if (locations == null) { |
| 85 | + new LoadLocationTask(this, amount).execute(); |
| 86 | + } else { |
| 87 | + showMarkers(amount); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private void onLatLngListLoaded(FeatureCollection featureCollection, int amount) { |
| 92 | + locations = featureCollection; |
| 93 | + showMarkers(amount); |
| 94 | + } |
| 95 | + |
| 96 | + private void showMarkers(int amount) { |
| 97 | + if (mapboxMap == null || locations == null || locations.features() == null || mapView.isDestroyed()) { |
| 98 | + return; |
| 99 | + } |
| 100 | + // delete old symbols |
| 101 | + symbolManager.delete(symbols); |
| 102 | + if (locations.features().size() < amount) { |
| 103 | + amount = locations.features().size(); |
| 104 | + } |
| 105 | + |
| 106 | + showSymbols(amount); |
| 107 | + } |
| 108 | + |
| 109 | + private void showSymbols(int amount) { |
| 110 | + List<SymbolOptions> options = new ArrayList<>(); |
| 111 | + Random random = new Random(); |
| 112 | + int randomIndex; |
| 113 | + |
| 114 | + List<Feature> features = locations.features(); |
| 115 | + if (features == null) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + for (int i = 0; i < amount; i++) { |
| 120 | + randomIndex = random.nextInt(features.size()); |
| 121 | + Feature feature = features.get(randomIndex); |
| 122 | + options.add(new SymbolOptions() |
| 123 | + .withGeometry((Point) feature.geometry()) |
| 124 | + .withIconImage("fire-station-11") |
| 125 | + ); |
| 126 | + } |
| 127 | + symbols = symbolManager.create(options); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + protected void onStart() { |
| 132 | + super.onStart(); |
| 133 | + mapView.onStart(); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + protected void onResume() { |
| 138 | + super.onResume(); |
| 139 | + mapView.onResume(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + protected void onPause() { |
| 144 | + super.onPause(); |
| 145 | + mapView.onPause(); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + protected void onStop() { |
| 150 | + super.onStop(); |
| 151 | + mapView.onStop(); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + protected void onSaveInstanceState(Bundle outState) { |
| 156 | + super.onSaveInstanceState(outState); |
| 157 | + mapView.onSaveInstanceState(outState); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + protected void onDestroy() { |
| 162 | + super.onDestroy(); |
| 163 | + |
| 164 | + if (symbolManager != null) { |
| 165 | + symbolManager.onDestroy(); |
| 166 | + } |
| 167 | + |
| 168 | + mapView.onDestroy(); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void onLowMemory() { |
| 173 | + super.onLowMemory(); |
| 174 | + mapView.onLowMemory(); |
| 175 | + } |
| 176 | + |
| 177 | + private static class LoadLocationTask extends AsyncTask<Void, Integer, FeatureCollection> { |
| 178 | + |
| 179 | + private final WeakReference<ClusterSymbolActivity> activity; |
| 180 | + private final int amount; |
| 181 | + |
| 182 | + private LoadLocationTask(ClusterSymbolActivity activity, int amount) { |
| 183 | + this.amount = amount; |
| 184 | + this.activity = new WeakReference<>(activity); |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + protected FeatureCollection doInBackground(Void... params) { |
| 189 | + ClusterSymbolActivity activity = this.activity.get(); |
| 190 | + if (activity != null) { |
| 191 | + String json = null; |
| 192 | + try { |
| 193 | + json = GeoParseUtil.loadStringFromAssets(activity.getApplicationContext()); |
| 194 | + } catch (IOException exception) { |
| 195 | + Timber.e(exception, "Could not add markers"); |
| 196 | + } |
| 197 | + |
| 198 | + if (json != null) { |
| 199 | + return FeatureCollection.fromJson(json); |
| 200 | + } |
| 201 | + } |
| 202 | + return null; |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + protected void onPostExecute(FeatureCollection locations) { |
| 207 | + super.onPostExecute(locations); |
| 208 | + ClusterSymbolActivity activity = this.activity.get(); |
| 209 | + if (activity != null) { |
| 210 | + activity.onLatLngListLoaded(locations, amount); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + |
| 215 | + public static class GeoParseUtil { |
| 216 | + |
| 217 | + static String loadStringFromAssets(final Context context) throws IOException { |
| 218 | + InputStream is = context.getAssets().open("points.geojson"); |
| 219 | + BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); |
| 220 | + return readAll(rd); |
| 221 | + } |
| 222 | + |
| 223 | + private static String readAll(Reader rd) throws IOException { |
| 224 | + StringBuilder sb = new StringBuilder(); |
| 225 | + int cp; |
| 226 | + while ((cp = rd.read()) != -1) { |
| 227 | + sb.append((char) cp); |
| 228 | + } |
| 229 | + return sb.toString(); |
| 230 | + } |
| 231 | + } |
| 232 | +} |
0 commit comments