Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit cc53fde

Browse files
author
Langston Smith
authored
Added TurfMeasurement distance example (#1193)
1 parent ae48d26 commit cc53fde

File tree

8 files changed

+270
-0
lines changed

8 files changed

+270
-0
lines changed

MapboxAndroidDemo/src/global/java/com/mapbox/mapboxandroiddemo/MainActivity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import com.mapbox.mapboxandroiddemo.examples.javaservices.SimplifyPolylineActivity;
7373
import com.mapbox.mapboxandroiddemo.examples.javaservices.StaticImageActivity;
7474
import com.mapbox.mapboxandroiddemo.examples.javaservices.TilequeryActivity;
75+
import com.mapbox.mapboxandroiddemo.examples.javaservices.TurfLineDistanceActivity;
7576
import com.mapbox.mapboxandroiddemo.examples.javaservices.TurfRingActivity;
7677
import com.mapbox.mapboxandroiddemo.examples.javaservices.TurfPhysicalCircleActivity;
7778
import com.mapbox.mapboxandroiddemo.examples.labs.AnimatedImageGifActivity;
@@ -1043,6 +1044,14 @@ private void initializeModels() {
10431044
null,
10441045
R.string.activity_java_services_tilequery_url, false, BuildConfig.MIN_SDK_VERSION));
10451046

1047+
exampleItemModels.add(new ExampleItemModel(
1048+
R.id.nav_java_services,
1049+
R.string.activity_java_services_turf_line_distance_title,
1050+
R.string.activity_java_services_turf_line_distance_description,
1051+
new Intent(MainActivity.this, TurfLineDistanceActivity.class),
1052+
null,
1053+
R.string.activity_java_services_turf_line_distance_url, false, BuildConfig.MIN_SDK_VERSION));
1054+
10461055
exampleItemModels.add(new ExampleItemModel(
10471056
R.id.nav_snapshot_image_generator,
10481057
R.string.activity_image_generator_snapshot_notification_title,

MapboxAndroidDemo/src/main/AndroidManifest.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,14 @@
914914
android:name="android.support.PARENT_ACTIVITY"
915915
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
916916
</activity>
917+
<activity
918+
android:name=".examples.javaservices.TurfLineDistanceActivity"
919+
android:label="@string/activity_java_services_turf_line_distance_title"
920+
android:screenOrientation="portrait">
921+
<meta-data
922+
android:name="android.support.PARENT_ACTIVITY"
923+
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
924+
</activity>
917925
<activity
918926
android:name=".examples.labs.ValueAnimatorIconAnimationActivity"
919927
android:label="@string/activity_lab_animated_interpolator_icon_drop_title"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
package com.mapbox.mapboxandroiddemo.examples.javaservices;
2+
3+
import android.graphics.Color;
4+
import android.os.Bundle;
5+
import android.widget.TextView;
6+
import android.widget.Toast;
7+
8+
import com.mapbox.geojson.Feature;
9+
import com.mapbox.geojson.LineString;
10+
import com.mapbox.geojson.Point;
11+
import com.mapbox.mapboxandroiddemo.R;
12+
import com.mapbox.mapboxsdk.Mapbox;
13+
import com.mapbox.mapboxsdk.geometry.LatLng;
14+
import com.mapbox.mapboxsdk.maps.MapView;
15+
import com.mapbox.mapboxsdk.maps.MapboxMap;
16+
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
17+
import com.mapbox.mapboxsdk.maps.Style;
18+
import com.mapbox.mapboxsdk.style.layers.CircleLayer;
19+
import com.mapbox.mapboxsdk.style.layers.LineLayer;
20+
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource;
21+
import com.mapbox.turf.TurfMeasurement;
22+
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import androidx.annotation.NonNull;
27+
import androidx.appcompat.app.AppCompatActivity;
28+
29+
import static com.mapbox.mapboxsdk.style.layers.Property.LINE_JOIN_ROUND;
30+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleColor;
31+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.circleRadius;
32+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;
33+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin;
34+
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;
35+
import static com.mapbox.turf.TurfConstants.UNIT_KILOMETERS;
36+
37+
/**
38+
* Use {@link com.mapbox.turf.TurfMeasurement#distance(Point, Point)} to measure the distance of a drawn line.
39+
*/
40+
public class TurfLineDistanceActivity extends AppCompatActivity implements MapboxMap.OnMapClickListener {
41+
42+
private static final String SOURCE_ID = "SOURCE_ID";
43+
private static final String CIRCLE_LAYER_ID = "CIRCLE_LAYER_ID";
44+
private static final String LINE_LAYER_ID = "LINE_LAYER_ID";
45+
46+
// Adjust private static final variables below to change the example's UI
47+
private static final String STYLE_URI = "mapbox://styles/mapbox/cjv6rzz4j3m4b1fqcchuxclhb";
48+
private static final int CIRCLE_COLOR = Color.RED;
49+
private static final int LINE_COLOR = CIRCLE_COLOR;
50+
private static final float CIRCLE_RADIUS = 6f;
51+
private static final float LINE_WIDTH = 4f;
52+
private static final String DISTANCE_UNITS = UNIT_KILOMETERS; // DISTANCE_UNITS must be equal to a String
53+
// found in the TurfConstants class
54+
55+
private List<Point> pointList = new ArrayList<>();
56+
private MapView mapView;
57+
private MapboxMap mapboxMap;
58+
private TextView lineLengthTextView;
59+
private double totalLineDistance = 0;
60+
61+
@Override
62+
protected void onCreate(Bundle savedInstanceState) {
63+
super.onCreate(savedInstanceState);
64+
65+
// Mapbox access token is configured here. This needs to be called either in your application
66+
// object or in the same activity which contains the mapview.
67+
Mapbox.getInstance(this, getString(R.string.access_token));
68+
69+
// This contains the MapView in XML and needs to be called after the access token is configured.
70+
setContentView(R.layout.activity_javaservices_turf_measure_line);
71+
72+
mapView = findViewById(R.id.mapView);
73+
mapView.onCreate(savedInstanceState);
74+
75+
mapView.getMapAsync(new OnMapReadyCallback() {
76+
@Override
77+
public void onMapReady(@NonNull MapboxMap mapboxMap) {
78+
79+
lineLengthTextView = findViewById(R.id.line_length_textView);
80+
81+
// DISTANCE_UNITS must be equal to a String found in the TurfConstants class
82+
lineLengthTextView.setText(String.format(getString(R.string.line_distance_textview),
83+
DISTANCE_UNITS, String.valueOf(totalLineDistance)));
84+
85+
TurfLineDistanceActivity.this.mapboxMap = mapboxMap;
86+
mapboxMap.setStyle(new Style.Builder()
87+
.fromUri(STYLE_URI)
88+
89+
// Add the source to the map
90+
.withSource(new GeoJsonSource(SOURCE_ID))
91+
92+
// Style and add the CircleLayer to the map
93+
.withLayer(new CircleLayer(CIRCLE_LAYER_ID, SOURCE_ID).withProperties(
94+
circleColor(CIRCLE_COLOR),
95+
circleRadius(CIRCLE_RADIUS)
96+
))
97+
98+
// Style and add the LineLayer to the map. The LineLayer is placed below the CircleLayer.
99+
.withLayerBelow(new LineLayer(LINE_LAYER_ID, SOURCE_ID).withProperties(
100+
lineColor(LINE_COLOR),
101+
lineWidth(LINE_WIDTH),
102+
lineJoin(LINE_JOIN_ROUND)
103+
), CIRCLE_LAYER_ID), new Style.OnStyleLoaded() {
104+
@Override
105+
public void onStyleLoaded(@NonNull Style style) {
106+
TurfLineDistanceActivity.this.mapboxMap.addOnMapClickListener(TurfLineDistanceActivity.this);
107+
Toast.makeText(TurfLineDistanceActivity.this, getString(
108+
R.string.line_distance_tap_instruction), Toast.LENGTH_SHORT).show();
109+
}
110+
}
111+
);
112+
}
113+
});
114+
}
115+
116+
@Override
117+
public boolean onMapClick(@NonNull LatLng point) {
118+
addClickPointToLine(point);
119+
return true;
120+
}
121+
122+
/**
123+
* Handle the map click location and re-draw the circle and line data.
124+
*
125+
* @param clickLatLng where the map was tapped on.
126+
*/
127+
private void addClickPointToLine(@NonNull LatLng clickLatLng) {
128+
mapboxMap.getStyle(new Style.OnStyleLoaded() {
129+
@Override
130+
public void onStyleLoaded(@NonNull Style style) {
131+
// Get the source from the map's style
132+
GeoJsonSource geoJsonSource = style.getSourceAs(SOURCE_ID);
133+
if (geoJsonSource != null) {
134+
135+
pointList.add(Point.fromLngLat(clickLatLng.getLongitude(), clickLatLng.getLatitude()));
136+
137+
int pointListSize = pointList.size();
138+
139+
double distanceBetweenLastAndSecondToLastClickPoint = 0;
140+
141+
// Make the Turf calculation between the last tap point and the second-to-last tap point.
142+
if (pointList.size() >= 2) {
143+
distanceBetweenLastAndSecondToLastClickPoint = TurfMeasurement.distance(
144+
pointList.get(pointListSize - 2), pointList.get(pointListSize - 1));
145+
}
146+
147+
// Re-draw the new GeoJSON data
148+
if (pointListSize >= 2 && distanceBetweenLastAndSecondToLastClickPoint > 0) {
149+
150+
// Add the last TurfMeasurement#distance calculated distance to the total line distance.
151+
totalLineDistance += distanceBetweenLastAndSecondToLastClickPoint;
152+
153+
// Adjust the TextView to display the new total line distance.
154+
// DISTANCE_UNITS must be equal to a String found in the TurfConstants class
155+
lineLengthTextView.setText(String.format(getString(R.string.line_distance_textview), DISTANCE_UNITS,
156+
String.valueOf(totalLineDistance)));
157+
158+
// Set the specific source's GeoJSON data
159+
geoJsonSource.setGeoJson(Feature.fromGeometry(LineString.fromLngLats(pointList)));
160+
}
161+
}
162+
}
163+
});
164+
}
165+
166+
// Add the mapView lifecycle to the activity's lifecycle methods
167+
@Override
168+
public void onResume() {
169+
super.onResume();
170+
mapView.onResume();
171+
}
172+
173+
@Override
174+
protected void onStart() {
175+
super.onStart();
176+
mapView.onStart();
177+
}
178+
179+
@Override
180+
protected void onStop() {
181+
super.onStop();
182+
mapView.onStop();
183+
}
184+
185+
@Override
186+
public void onPause() {
187+
super.onPause();
188+
mapView.onPause();
189+
}
190+
191+
@Override
192+
public void onLowMemory() {
193+
super.onLowMemory();
194+
mapView.onLowMemory();
195+
}
196+
197+
@Override
198+
protected void onDestroy() {
199+
super.onDestroy();
200+
if (mapboxMap != null) {
201+
mapboxMap.removeOnMapClickListener(this);
202+
}
203+
mapView.onDestroy();
204+
}
205+
206+
@Override
207+
protected void onSaveInstanceState(Bundle outState) {
208+
super.onSaveInstanceState(outState);
209+
mapView.onSaveInstanceState(outState);
210+
}
211+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/constraint_layout"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
tools:context=".examples.javaservices.TurfLineDistanceActivity">
9+
10+
<com.mapbox.mapboxsdk.maps.MapView
11+
android:id="@+id/mapView"
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent"
14+
mapbox:mapbox_cameraTargetLat="21.3011229"
15+
mapbox:mapbox_cameraTargetLng="-157.851376"
16+
mapbox:mapbox_cameraZoom="12" />
17+
18+
<androidx.cardview.widget.CardView
19+
android:id="@+id/cardView7"
20+
android:layout_width="wrap_content"
21+
android:layout_height="wrap_content"
22+
android:layout_gravity="top|center"
23+
android:layout_marginTop="8dp">
24+
25+
<TextView
26+
android:id="@+id/line_length_textView"
27+
android:layout_width="wrap_content"
28+
android:layout_height="wrap_content"
29+
android:padding="8dp"
30+
android:text="@string/line_distance_textview" />
31+
32+
</androidx.cardview.widget.CardView>
33+
34+
</FrameLayout>

MapboxAndroidDemo/src/main/res/values/activity_strings.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,9 @@
426426
<string name="invalid_max_ambient_cache_size">Set ambient\nmax cache</string>
427427
<string name="clear_ambient_cache_button">Clear ambient\ncache</string>
428428
<string name="adjust_map_button">Adjust map</string>
429+
430+
<!-- Turf measure line distance -->
431+
<string name="line_distance_tap_instruction">Tap on map at least twice to create and measure a line</string>
432+
<string name="line_distance_textview">Line distance in %1$s: %2$s</string>
433+
429434
</resources>

MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
<string name="activity_java_services_map_matching_description">Match raw GPS points to the map so they aligns with the roads/pathways.</string>
9797
<string name="activity_java_services_turf_ring_description">Use Turf to calculate coordinates to eventually draw a ring around a center coordinate.</string>
9898
<string name="activity_java_services_turf_physical_circle_description">Use Turf to generate a circle with a radius expressed in physical units (e.g. miles, kilometers, etc).</string>
99+
<string name="activity_java_services_turf_line_distance_description">Use Turf to generate a circle with a radius expressed in physical units (e.g. miles, kilometers, etc).</string>
99100
<string name="activity_plugins_traffic_plugin_description">Use the traffic plugin to display live car congestion data on top of a map.</string>
100101
<string name="activity_plugins_building_plugin_description">Use the building plugin to easily display 3D building height</string>
101102
<string name="activity_plugins_geojson_plugin_description">Easily retrieve GeoJSON data from a url, asset, or path</string>

MapboxAndroidDemo/src/main/res/values/titles_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<string name="activity_java_services_map_matching_title">Map Matching</string>
9595
<string name="activity_java_services_turf_ring_title">Hollow circle</string>
9696
<string name="activity_java_services_turf_physical_circle_title">Define a circle in physical units</string>
97+
<string name="activity_java_services_turf_line_distance_title">Measure line distance</string>
9798
<string name="activity_plugins_traffic_plugin_title">Display real-time traffic</string>
9899
<string name="activity_plugins_building_plugin_title">Display buildings in 3D</string>
99100
<string name="activity_plugins_localization_plugin_title">Change map text to device language</string>

MapboxAndroidDemo/src/main/res/values/urls_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
<string name="activity_java_services_isochrone_url" translatable="false">https://i.imgur.com/eUByGvt.png</string>
9191
<string name="activity_java_services_isochrone_with_seekbar_url" translatable="false">https://i.imgur.com/hCVswtE.png</string>
9292
<string name="activity_java_services_tilequery_url" translatable="false">http://i.imgur.com/VkOCYwq.jpg</string>
93+
<string name="activity_java_services_turf_line_distance_url" translatable="false">https://i.imgur.com/dQLvi1t.png</string>
9394
<string name="activity_java_services_simplify_polyline_url" translatable="false">http://i.imgur.com/uATgul1.png</string>
9495
<string name="activity_java_services_map_matching_url" translatable="false">https://i.imgur.com/ig8gGnY.png</string>
9596
<string name="activity_java_services_turf_ring_url" translatable="false">https://i.imgur.com/nG8xeXH.png</string>

0 commit comments

Comments
 (0)