From 1d5a2a171663339184a97c4ee05cada484b80dd9 Mon Sep 17 00:00:00 2001 From: langsmith Date: Fri, 30 Aug 2019 17:23:59 -0700 Subject: [PATCH] added TurfMeasurement distance example --- .../mapboxandroiddemo/MainActivity.java | 9 + .../src/main/AndroidManifest.xml | 8 + .../TurfLineDistanceActivity.java | 211 ++++++++++++++++++ ...ctivity_javaservices_turf_measure_line.xml | 34 +++ .../src/main/res/values/activity_strings.xml | 5 + .../main/res/values/descriptions_strings.xml | 1 + .../src/main/res/values/titles_strings.xml | 1 + .../src/main/res/values/urls_strings.xml | 1 + 8 files changed, 270 insertions(+) create mode 100644 MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/examples/javaservices/TurfLineDistanceActivity.java create mode 100644 MapboxAndroidDemo/src/main/res/layout/activity_javaservices_turf_measure_line.xml diff --git a/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java b/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java index 4811c92ba..a5dcf9ecc 100644 --- a/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java +++ b/MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java @@ -72,6 +72,7 @@ import com.mapbox.mapboxandroiddemo.examples.javaservices.SimplifyPolylineActivity; import com.mapbox.mapboxandroiddemo.examples.javaservices.StaticImageActivity; import com.mapbox.mapboxandroiddemo.examples.javaservices.TilequeryActivity; +import com.mapbox.mapboxandroiddemo.examples.javaservices.TurfLineDistanceActivity; import com.mapbox.mapboxandroiddemo.examples.javaservices.TurfRingActivity; import com.mapbox.mapboxandroiddemo.examples.javaservices.TurfPhysicalCircleActivity; import com.mapbox.mapboxandroiddemo.examples.labs.AnimatedImageGifActivity; @@ -1043,6 +1044,14 @@ private void initializeModels() { null, R.string.activity_java_services_tilequery_url, false, BuildConfig.MIN_SDK_VERSION)); + exampleItemModels.add(new ExampleItemModel( + R.id.nav_java_services, + R.string.activity_java_services_turf_line_distance_title, + R.string.activity_java_services_turf_line_distance_description, + new Intent(MainActivity.this, TurfLineDistanceActivity.class), + null, + R.string.activity_java_services_turf_line_distance_url, false, BuildConfig.MIN_SDK_VERSION)); + exampleItemModels.add(new ExampleItemModel( R.id.nav_snapshot_image_generator, R.string.activity_image_generator_snapshot_notification_title, diff --git a/MapboxAndroidDemo/src/main/AndroidManifest.xml b/MapboxAndroidDemo/src/main/AndroidManifest.xml index 5ab420a77..b4f978a67 100644 --- a/MapboxAndroidDemo/src/main/AndroidManifest.xml +++ b/MapboxAndroidDemo/src/main/AndroidManifest.xml @@ -914,6 +914,14 @@ android:name="android.support.PARENT_ACTIVITY" android:value="com.mapbox.mapboxandroiddemo.MainActivity" /> + + + pointList = new ArrayList<>(); + private MapView mapView; + private MapboxMap mapboxMap; + private TextView lineLengthTextView; + private double totalLineDistance = 0; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + // Mapbox access token is configured here. This needs to be called either in your application + // object or in the same activity which contains the mapview. + Mapbox.getInstance(this, getString(R.string.access_token)); + + // This contains the MapView in XML and needs to be called after the access token is configured. + setContentView(R.layout.activity_javaservices_turf_measure_line); + + mapView = findViewById(R.id.mapView); + mapView.onCreate(savedInstanceState); + + mapView.getMapAsync(new OnMapReadyCallback() { + @Override + public void onMapReady(@NonNull MapboxMap mapboxMap) { + + lineLengthTextView = findViewById(R.id.line_length_textView); + + // DISTANCE_UNITS must be equal to a String found in the TurfConstants class + lineLengthTextView.setText(String.format(getString(R.string.line_distance_textview), + DISTANCE_UNITS, String.valueOf(totalLineDistance))); + + TurfLineDistanceActivity.this.mapboxMap = mapboxMap; + mapboxMap.setStyle(new Style.Builder() + .fromUri(STYLE_URI) + + // Add the source to the map + .withSource(new GeoJsonSource(SOURCE_ID)) + + // Style and add the CircleLayer to the map + .withLayer(new CircleLayer(CIRCLE_LAYER_ID, SOURCE_ID).withProperties( + circleColor(CIRCLE_COLOR), + circleRadius(CIRCLE_RADIUS) + )) + + // Style and add the LineLayer to the map. The LineLayer is placed below the CircleLayer. + .withLayerBelow(new LineLayer(LINE_LAYER_ID, SOURCE_ID).withProperties( + lineColor(LINE_COLOR), + lineWidth(LINE_WIDTH), + lineJoin(LINE_JOIN_ROUND) + ), CIRCLE_LAYER_ID), new Style.OnStyleLoaded() { + @Override + public void onStyleLoaded(@NonNull Style style) { + TurfLineDistanceActivity.this.mapboxMap.addOnMapClickListener(TurfLineDistanceActivity.this); + Toast.makeText(TurfLineDistanceActivity.this, getString( + R.string.line_distance_tap_instruction), Toast.LENGTH_SHORT).show(); + } + } + ); + } + }); + } + + @Override + public boolean onMapClick(@NonNull LatLng point) { + addClickPointToLine(point); + return true; + } + + /** + * Handle the map click location and re-draw the circle and line data. + * + * @param clickLatLng where the map was tapped on. + */ + private void addClickPointToLine(@NonNull LatLng clickLatLng) { + mapboxMap.getStyle(new Style.OnStyleLoaded() { + @Override + public void onStyleLoaded(@NonNull Style style) { + // Get the source from the map's style + GeoJsonSource geoJsonSource = style.getSourceAs(SOURCE_ID); + if (geoJsonSource != null) { + + pointList.add(Point.fromLngLat(clickLatLng.getLongitude(), clickLatLng.getLatitude())); + + int pointListSize = pointList.size(); + + double distanceBetweenLastAndSecondToLastClickPoint = 0; + + // Make the Turf calculation between the last tap point and the second-to-last tap point. + if (pointList.size() >= 2) { + distanceBetweenLastAndSecondToLastClickPoint = TurfMeasurement.distance( + pointList.get(pointListSize - 2), pointList.get(pointListSize - 1)); + } + + // Re-draw the new GeoJSON data + if (pointListSize >= 2 && distanceBetweenLastAndSecondToLastClickPoint > 0) { + + // Add the last TurfMeasurement#distance calculated distance to the total line distance. + totalLineDistance += distanceBetweenLastAndSecondToLastClickPoint; + + // Adjust the TextView to display the new total line distance. + // DISTANCE_UNITS must be equal to a String found in the TurfConstants class + lineLengthTextView.setText(String.format(getString(R.string.line_distance_textview), DISTANCE_UNITS, + String.valueOf(totalLineDistance))); + + // Set the specific source's GeoJSON data + geoJsonSource.setGeoJson(Feature.fromGeometry(LineString.fromLngLats(pointList))); + } + } + } + }); + } + + // Add the mapView lifecycle to the activity's lifecycle methods + @Override + public void onResume() { + super.onResume(); + mapView.onResume(); + } + + @Override + protected void onStart() { + super.onStart(); + mapView.onStart(); + } + + @Override + protected void onStop() { + super.onStop(); + mapView.onStop(); + } + + @Override + public void onPause() { + super.onPause(); + mapView.onPause(); + } + + @Override + public void onLowMemory() { + super.onLowMemory(); + mapView.onLowMemory(); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + if (mapboxMap != null) { + mapboxMap.removeOnMapClickListener(this); + } + mapView.onDestroy(); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + mapView.onSaveInstanceState(outState); + } +} diff --git a/MapboxAndroidDemo/src/main/res/layout/activity_javaservices_turf_measure_line.xml b/MapboxAndroidDemo/src/main/res/layout/activity_javaservices_turf_measure_line.xml new file mode 100644 index 000000000..a773a5268 --- /dev/null +++ b/MapboxAndroidDemo/src/main/res/layout/activity_javaservices_turf_measure_line.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/MapboxAndroidDemo/src/main/res/values/activity_strings.xml b/MapboxAndroidDemo/src/main/res/values/activity_strings.xml index 8bb13e8d3..c2e68906a 100644 --- a/MapboxAndroidDemo/src/main/res/values/activity_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/activity_strings.xml @@ -426,4 +426,9 @@ Set ambient\nmax cache Clear ambient\ncache Adjust map + + + Tap on map at least twice to create and measure a line + Line distance in %1$s: %2$s + \ No newline at end of file diff --git a/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml b/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml index 2a21b1fea..6210dab88 100644 --- a/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml @@ -96,6 +96,7 @@ Match raw GPS points to the map so they aligns with the roads/pathways. Use Turf to calculate coordinates to eventually draw a ring around a center coordinate. Use Turf to generate a circle with a radius expressed in physical units (e.g. miles, kilometers, etc). + Use Turf to generate a circle with a radius expressed in physical units (e.g. miles, kilometers, etc). Use the traffic plugin to display live car congestion data on top of a map. Use the building plugin to easily display 3D building height Easily retrieve GeoJSON data from a url, asset, or path diff --git a/MapboxAndroidDemo/src/main/res/values/titles_strings.xml b/MapboxAndroidDemo/src/main/res/values/titles_strings.xml index 5c8dc9a76..374a731a2 100644 --- a/MapboxAndroidDemo/src/main/res/values/titles_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/titles_strings.xml @@ -94,6 +94,7 @@ Map Matching Hollow circle Define a circle in physical units + Measure line distance Display real-time traffic Display buildings in 3D Change map text to device language diff --git a/MapboxAndroidDemo/src/main/res/values/urls_strings.xml b/MapboxAndroidDemo/src/main/res/values/urls_strings.xml index 5602f9d86..a2c707e45 100644 --- a/MapboxAndroidDemo/src/main/res/values/urls_strings.xml +++ b/MapboxAndroidDemo/src/main/res/values/urls_strings.xml @@ -90,6 +90,7 @@ https://i.imgur.com/eUByGvt.png https://i.imgur.com/hCVswtE.png http://i.imgur.com/VkOCYwq.jpg + https://i.imgur.com/dQLvi1t.png http://i.imgur.com/uATgul1.png https://i.imgur.com/ig8gGnY.png https://i.imgur.com/nG8xeXH.png