|
| 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 | +} |
0 commit comments