[android] #2552 add 10000 markers
@@ -36,6 +36,10 @@
|
||||
<activity
|
||||
android:name=".InfoWindowActivity"
|
||||
android:label="@string/activity_info_window" />
|
||||
|
||||
<activity
|
||||
android:name=".BulkMarkerActivity"
|
||||
android:label="@string/action_add_bulk_markers" />
|
||||
|
||||
<activity
|
||||
android:name=".MapFragmentActivity"
|
||||
|
||||
136346
android/java/MapboxGLAndroidSDKTestApp/src/main/assets/points.geojson
Normal file
@@ -0,0 +1,151 @@
|
||||
package com.mapbox.mapboxsdk.testapp;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.app.ActionBar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.support.v7.widget.Toolbar;
|
||||
import android.util.Log;
|
||||
import android.view.MenuItem;
|
||||
|
||||
import com.mapbox.mapboxsdk.annotations.Marker;
|
||||
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
import com.mapbox.mapboxsdk.geometry.LatLngZoom;
|
||||
import com.mapbox.mapboxsdk.utils.ApiAccess;
|
||||
import com.mapbox.mapboxsdk.views.MapView;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.text.DecimalFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BulkMarkerActivity extends AppCompatActivity {
|
||||
|
||||
private MapView mMapView;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_infowindow);
|
||||
|
||||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
ActionBar actionBar = getSupportActionBar();
|
||||
if (actionBar != null) {
|
||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||
actionBar.setDisplayShowHomeEnabled(true);
|
||||
}
|
||||
|
||||
mMapView = (MapView) findViewById(R.id.mapView);
|
||||
mMapView.setAccessToken(ApiAccess.getToken(this));
|
||||
mMapView.onCreate(savedInstanceState);
|
||||
mMapView.setCenterCoordinate(new LatLngZoom(38.87031, -77.00897, 10));
|
||||
|
||||
new LoadBulkMarkerTask(mMapView, 250).execute();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart() {
|
||||
super.onStart();
|
||||
mMapView.onStart();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
mMapView.onResume();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPause() {
|
||||
super.onPause();
|
||||
mMapView.onPause();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop() {
|
||||
super.onStop();
|
||||
mMapView.onStop();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSaveInstanceState(Bundle outState) {
|
||||
super.onSaveInstanceState(outState);
|
||||
mMapView.onSaveInstanceState(outState);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDestroy() {
|
||||
super.onDestroy();
|
||||
mMapView.onDestroy();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLowMemory() {
|
||||
super.onLowMemory();
|
||||
mMapView.onLowMemory();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onOptionsItemSelected(MenuItem item) {
|
||||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
onBackPressed();
|
||||
return true;
|
||||
default:
|
||||
return super.onOptionsItemSelected(item);
|
||||
}
|
||||
}
|
||||
|
||||
private static class LoadBulkMarkerTask extends AsyncTask<Void, Void, List<MarkerOptions>> {
|
||||
|
||||
private static final String TAG = "LoadBulkMarkerTask";
|
||||
private WeakReference<MapView> mMapView;
|
||||
private Context mAppContext;
|
||||
private int mAmount;
|
||||
|
||||
public LoadBulkMarkerTask(MapView mapView, int amount) {
|
||||
mMapView = new WeakReference<>(mapView);
|
||||
mAppContext = mapView.getContext().getApplicationContext();
|
||||
mAmount = amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<MarkerOptions> doInBackground(Void... params) {
|
||||
List<MarkerOptions> markerOptions = new ArrayList<>();
|
||||
try {
|
||||
DecimalFormat formatter = new DecimalFormat("#.#####");
|
||||
String json = Util.loadStringFromAssets(mAppContext, "points.geojson");
|
||||
List<LatLng> locations = Util.parseGeoJSONCoordinates(json);
|
||||
|
||||
LatLng location;
|
||||
for (int i = 0; i < mAmount; i++) {
|
||||
location = locations.get(i);
|
||||
markerOptions.add(new MarkerOptions()
|
||||
.position(location)
|
||||
.title("Marker")
|
||||
.snippet(formatter.format(location.getLatitude()) + ", " + formatter.format(location.getLongitude())));
|
||||
}
|
||||
} catch (IOException | JSONException e) {
|
||||
Log.e(TAG, "Could not add markers,", e);
|
||||
}
|
||||
return markerOptions;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(List<MarkerOptions> markerOptions) {
|
||||
super.onPostExecute(markerOptions);
|
||||
MapView mapView = mMapView.get();
|
||||
if (mapView != null) {
|
||||
List<Marker> markers = mapView.addMarkers(markerOptions);
|
||||
Log.v(TAG, "Markers added " + markers.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -326,6 +326,10 @@ public class MainActivity extends AppCompatActivity {
|
||||
startActivity(new Intent(getApplicationContext(), PressForMarkerActivity.class));
|
||||
return true;
|
||||
|
||||
case R.id.action_bulk_markers:
|
||||
startActivity(new Intent(getApplicationContext(), BulkMarkerActivity.class));
|
||||
return true;
|
||||
|
||||
case R.id.action_info_window:
|
||||
startActivity(new Intent(getApplicationContext(), InfoWindowActivity.class));
|
||||
return true;
|
||||
@@ -458,10 +462,10 @@ public class MainActivity extends AppCompatActivity {
|
||||
private void addPolyline() {
|
||||
try {
|
||||
String geojsonStr = Util.loadStringFromAssets(this, "small_line.geojson");
|
||||
LatLng[] latLngs = Util.parseGeoJSONCoordinates(geojsonStr);
|
||||
List<LatLng> latLngs = Util.parseGeoJSONCoordinates(geojsonStr);
|
||||
MapView map = mMapView;
|
||||
map.addPolyline(new PolylineOptions()
|
||||
.add(latLngs)
|
||||
.add(latLngs.toArray(new LatLng[latLngs.size()]))
|
||||
.width(2)
|
||||
.color(Color.RED));
|
||||
} catch (Exception e) {
|
||||
@@ -473,11 +477,11 @@ public class MainActivity extends AppCompatActivity {
|
||||
private void addPolygon() {
|
||||
try {
|
||||
String geojsonStr = Util.loadStringFromAssets(this, "small_poly.geojson");
|
||||
LatLng[] latLngs = Util.parseGeoJSONCoordinates(geojsonStr);
|
||||
List<LatLng> latLngs = Util.parseGeoJSONCoordinates(geojsonStr);
|
||||
MapView map = mMapView;
|
||||
ArrayList<PolygonOptions> opts = new ArrayList<>();
|
||||
opts.add(new PolygonOptions()
|
||||
.add(latLngs)
|
||||
.add(latLngs.toArray(new LatLng[latLngs.size()]))
|
||||
.strokeColor(Color.MAGENTA)
|
||||
.fillColor(Color.BLUE).alpha(0.5f));
|
||||
map.addPolygons(opts).get(0);
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
package com.mapbox.mapboxsdk.testapp;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.mapbox.mapboxsdk.geometry.LatLng;
|
||||
|
||||
@@ -18,9 +15,11 @@ import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.Reader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Util {
|
||||
|
||||
|
||||
public static String loadStringFromAssets(final Context context, final String fileName) throws IOException {
|
||||
if (TextUtils.isEmpty(fileName)) {
|
||||
throw new NullPointerException("No GeoJSON File Name passed in.");
|
||||
@@ -30,25 +29,34 @@ public class Util {
|
||||
return readAll(rd);
|
||||
}
|
||||
|
||||
public static LatLng[] parseGeoJSONCoordinates(String geojsonStr) throws JSONException {
|
||||
public static List<LatLng> parseGeoJSONCoordinates(String geojsonStr) throws JSONException {
|
||||
List<LatLng> latLngs = new ArrayList<>();
|
||||
JSONObject jsonObject = new JSONObject(geojsonStr);
|
||||
JSONArray features = jsonObject.getJSONArray("features");
|
||||
JSONObject feature = features.getJSONObject(0);
|
||||
JSONObject geometry = feature.getJSONObject("geometry");
|
||||
String type = geometry.getString("type");
|
||||
JSONArray coordinates;
|
||||
if (type.equals("Polygon")) {
|
||||
coordinates = geometry.getJSONArray("coordinates").getJSONArray(0);
|
||||
} else {
|
||||
coordinates = geometry.getJSONArray("coordinates");
|
||||
}
|
||||
int len = coordinates.length();
|
||||
LatLng[] latLngs = new LatLng[coordinates.length()];
|
||||
for (int i = 0; i < len; ++i) {
|
||||
JSONArray coord = coordinates.getJSONArray(i);
|
||||
double lng = coord.getDouble(0);
|
||||
double lat = coord.getDouble(1);
|
||||
latLngs[i] = new LatLng(lat, lng);
|
||||
int featureLength = features.length();
|
||||
for (int j = 0; j < featureLength; ++j) {
|
||||
JSONObject feature = features.getJSONObject(j);
|
||||
JSONObject geometry = feature.getJSONObject("geometry");
|
||||
String type = geometry.getString("type");
|
||||
JSONArray coordinates;
|
||||
if (type.equals("Polygon")) {
|
||||
coordinates = geometry.getJSONArray("coordinates").getJSONArray(0);
|
||||
} else {
|
||||
coordinates = geometry.getJSONArray("coordinates");
|
||||
}
|
||||
int len = coordinates.length();
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if (coordinates.get(i) instanceof JSONArray) {
|
||||
JSONArray coord = coordinates.getJSONArray(i);
|
||||
double lng = coord.getDouble(0);
|
||||
double lat = coord.getDouble(1);
|
||||
latLngs.add(new LatLng(lat, lng));
|
||||
} else {
|
||||
double lng = coordinates.getDouble(0);
|
||||
double lat = coordinates.getDouble(1);
|
||||
latLngs.add(new LatLng(lat, lng));
|
||||
}
|
||||
}
|
||||
}
|
||||
return latLngs;
|
||||
}
|
||||
|
||||
|
After Width: | Height: | Size: 280 B |
|
After Width: | Height: | Size: 224 B |
|
After Width: | Height: | Size: 215 B |
|
After Width: | Height: | Size: 176 B |
|
After Width: | Height: | Size: 136 B |
|
After Width: | Height: | Size: 135 B |
|
After Width: | Height: | Size: 260 B |
|
After Width: | Height: | Size: 201 B |
|
After Width: | Height: | Size: 190 B |
|
After Width: | Height: | Size: 365 B |
|
After Width: | Height: | Size: 279 B |
|
After Width: | Height: | Size: 263 B |
|
After Width: | Height: | Size: 450 B |
|
After Width: | Height: | Size: 343 B |
|
After Width: | Height: | Size: 332 B |
@@ -0,0 +1,25 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical">
|
||||
|
||||
<android.support.v7.widget.Toolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@color/primary"
|
||||
android:minHeight="?attr/actionBarSize"
|
||||
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
|
||||
|
||||
<com.mapbox.mapboxsdk.views.MapView
|
||||
android:id="@+id/mapView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
app:center_latitude="38.897705003219784"
|
||||
app:center_longitude="-77.03655168667463"
|
||||
app:style_url="@string/style_mapbox_streets"
|
||||
app:zoom_level="15" />
|
||||
|
||||
</LinearLayout>
|
||||
@@ -83,24 +83,33 @@
|
||||
<item
|
||||
android:id="@+id/action_info_window"
|
||||
android:checkable="false"
|
||||
android:icon="@drawable/ic_filter_none_white_24dp"
|
||||
android:icon="@drawable/ic_flip_to_front_white_24dp"
|
||||
android:title="@string/action_info_window" />
|
||||
|
||||
<item
|
||||
android:id="@+id/action_info_window_adapter"
|
||||
android:checkable="false"
|
||||
android:icon="@drawable/ic_filter_none_white_24dp"
|
||||
android:icon="@drawable/ic_flip_to_back_white_24dp"
|
||||
android:title="@string/action_info_window_adapter" />
|
||||
|
||||
<item android:id="@+id/action_press_for_marker"
|
||||
android:checkable="false"
|
||||
android:icon="@drawable/ic_pin_drop_white_24dp"
|
||||
android:title="@string/action_press_for_marker"/>
|
||||
<item
|
||||
android:id="@+id/action_press_for_marker"
|
||||
android:checkable="false"
|
||||
android:icon="@drawable/ic_pin_drop_white_24dp"
|
||||
android:title="@string/action_press_for_marker" />
|
||||
|
||||
<item android:id="@+id/action_visible_bounds"
|
||||
<item
|
||||
android:id="@+id/action_visible_bounds"
|
||||
android:checkable="false"
|
||||
android:icon="@drawable/ic_crop_white_24dp"
|
||||
android:title="@string/action_visible_bounds"/>
|
||||
android:title="@string/action_visible_bounds" />
|
||||
|
||||
<item
|
||||
android:id="@+id/action_bulk_markers"
|
||||
android:checkable="false"
|
||||
android:icon="@drawable/ic_blur_on_white_24dp"
|
||||
android:title="@string/action_add_bulk_markers" />
|
||||
|
||||
</menu>
|
||||
</item>
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
<string name="activity_infowindow_adapter">InfoWindow Adapter</string>
|
||||
<string name="activity_map_fragment">Map Fragment Activity</string>
|
||||
<string name="activity_press_for_marker">Press For Marker Activity</string>
|
||||
<string name="activity_marker_in_bulk">Add Bulk Markers Activity</string>
|
||||
<string name="activity_info_window">InfoWindow Activity</string>
|
||||
<string name="activity_visible_coordinate_bounds">Visible Coordinate Bounds</string>
|
||||
|
||||
@@ -24,6 +25,7 @@
|
||||
<string name="action_map_fragment">MapFragment</string>
|
||||
<string name="action_press_for_marker">Press For Marker</string>
|
||||
<string name="action_info_window">InfoWindow</string>
|
||||
<string name="action_add_bulk_markers">Add Markers in bulk</string>
|
||||
<string name="action_visible_bounds">Set Visible Bounds</string>
|
||||
<string name="action_visible_bounds_explanation">Center map around 2 markers</string>
|
||||
|
||||
|
||||