This repository has been archived by the owner on Oct 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 493
Adding example of using TurfTransformation#circle to create visual ring #1039
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
...mo/src/main/java/com/mapbox/mapboxandroiddemo/examples/javaservices/TurfRingActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package com.mapbox.mapboxandroiddemo.examples.javaservices; | ||
|
||
import android.graphics.Color; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.widget.Toast; | ||
|
||
import com.mapbox.geojson.LineString; | ||
import com.mapbox.geojson.Point; | ||
import com.mapbox.geojson.Polygon; | ||
import com.mapbox.mapboxandroiddemo.R; | ||
import com.mapbox.mapboxsdk.Mapbox; | ||
import com.mapbox.mapboxsdk.geometry.LatLng; | ||
import com.mapbox.mapboxsdk.maps.MapView; | ||
import com.mapbox.mapboxsdk.maps.MapboxMap; | ||
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; | ||
import com.mapbox.mapboxsdk.maps.Style; | ||
import com.mapbox.mapboxsdk.style.layers.FillLayer; | ||
import com.mapbox.mapboxsdk.style.sources.GeoJsonSource; | ||
import com.mapbox.turf.TurfMeta; | ||
import com.mapbox.turf.TurfTransformation; | ||
|
||
import static com.mapbox.mapboxsdk.style.layers.PropertyFactory.fillColor; | ||
import static com.mapbox.turf.TurfConstants.UNIT_MILES; | ||
|
||
/** | ||
* Use {@link TurfTransformation#circle(Point, double, int, String)} to draw a hollow circle | ||
* (i.e. ring) around a center coordinate. | ||
*/ | ||
public class TurfRingActivity extends AppCompatActivity implements MapboxMap.OnMapClickListener { | ||
private static final String OUTER_CIRCLE_GEOJSON_SOURCE_ID = "OUTER_CIRCLE_GEOJSON_SOURCE_ID"; | ||
private static final String INNER_CIRCLE_GEOJSON_SOURCE_ID = "INNER_CIRCLE_GEOJSON_SOURCE_ID"; | ||
private static final String OUTER_CIRCLE_LAYER_ID = "OUTER_CIRCLE_LAYER_ID"; | ||
private static final String INNER_CIRCLE_LAYER_ID = "INNER_CIRCLE_LAYER_ID"; | ||
private static final int OUTER_CIRCLE_MILE_RADIUS = 1; | ||
private static final double MILE_DIFFERENCE_BETWEEN_CIRCLES = .2; | ||
private static final int CIRCLE_STEPS = 360; | ||
private static final Point POINT_IN_MIDDLE_OF_CIRCLE = Point.fromLngLat(-115.150738, 36.16218); | ||
private MapView mapView; | ||
private MapboxMap mapboxMap; | ||
|
||
@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_lab_hollow_circle); | ||
|
||
mapView = findViewById(R.id.mapView); | ||
mapView.onCreate(savedInstanceState); | ||
|
||
mapView.getMapAsync(new OnMapReadyCallback() { | ||
@Override | ||
public void onMapReady(@NonNull MapboxMap mapboxMap) { | ||
TurfRingActivity.this.mapboxMap = mapboxMap; | ||
mapboxMap.setStyle(new Style.Builder() | ||
.fromUrl(Style.LIGHT) | ||
.withSource(new GeoJsonSource(OUTER_CIRCLE_GEOJSON_SOURCE_ID)) | ||
.withSource(new GeoJsonSource(INNER_CIRCLE_GEOJSON_SOURCE_ID)) | ||
.withLayer(new FillLayer(OUTER_CIRCLE_LAYER_ID, OUTER_CIRCLE_GEOJSON_SOURCE_ID).withProperties( | ||
fillColor(Color.RED) | ||
)) | ||
.withLayer(new FillLayer(INNER_CIRCLE_LAYER_ID, INNER_CIRCLE_GEOJSON_SOURCE_ID).withProperties( | ||
fillColor(Color.RED) | ||
)), new Style.OnStyleLoaded() { | ||
@Override | ||
public void onStyleLoaded(@NonNull Style style) { | ||
moveRing(POINT_IN_MIDDLE_OF_CIRCLE); | ||
|
||
TurfRingActivity.this.mapboxMap.addOnMapClickListener(TurfRingActivity.this); | ||
|
||
Toast.makeText(TurfRingActivity.this, getString(R.string.tap_on_map), | ||
Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean onMapClick(@NonNull LatLng point) { | ||
|
||
moveRing(Point.fromLngLat(point.getLongitude(), point.getLatitude())); | ||
|
||
return true; | ||
} | ||
|
||
private void moveRing(Point centerPoint) { | ||
if (mapboxMap.getStyle() != null) { | ||
Style style = mapboxMap.getStyle(); | ||
|
||
// Use Turf to calculate the coordinates for the outer ring of the final Polygon | ||
Polygon outerCirclePolygon = getTurfPolygon(OUTER_CIRCLE_MILE_RADIUS, centerPoint); | ||
|
||
// Use Turf to calculate the coordinates for the inner ring of the final Polygon | ||
Polygon innerCirclePolygon = getTurfPolygon( | ||
OUTER_CIRCLE_MILE_RADIUS - MILE_DIFFERENCE_BETWEEN_CIRCLES, centerPoint); | ||
|
||
GeoJsonSource outerCircleSource = style.getSourceAs(OUTER_CIRCLE_GEOJSON_SOURCE_ID); | ||
|
||
if (outerCircleSource != null) { | ||
|
||
// Use the two Polygon objects above to create the final Polygon that visually represents the ring. | ||
outerCircleSource.setGeoJson(Polygon.fromOuterInner( | ||
// Create outer LineString | ||
LineString.fromLngLats(TurfMeta.coordAll(outerCirclePolygon, false)), | ||
// Create inter LineString | ||
LineString.fromLngLats(TurfMeta.coordAll(innerCirclePolygon, false)) | ||
)); | ||
} | ||
} | ||
} | ||
|
||
private Polygon getTurfPolygon(@NonNull double radius, Point centerPoint) { | ||
return TurfTransformation.circle(centerPoint, radius, CIRCLE_STEPS, UNIT_MILES); | ||
} | ||
|
||
// 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(); | ||
mapView.onDestroy(); | ||
} | ||
|
||
@Override | ||
protected void onSaveInstanceState(Bundle outState) { | ||
super.onSaveInstanceState(outState); | ||
mapView.onSaveInstanceState(outState); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
MapboxAndroidDemo/src/main/res/layout/activity_lab_hollow_circle.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:mapbox="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context=".examples.labs.SpaceStationLocationActivity"> | ||
|
||
<com.mapbox.mapboxsdk.maps.MapView | ||
android:id="@+id/mapView" | ||
android:layout_width="match_parent" | ||
mapbox:mapbox_cameraTargetLat=" 36.16218" | ||
mapbox:mapbox_cameraTargetLng="-115.150738" | ||
mapbox:mapbox_cameraZoom="11" | ||
android:layout_height="match_parent"/> | ||
|
||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just as fyi, alternatively to avoid nullability of a style you can use
mapboxMap.getStyle(OnStyleLoadedCallback)
this will give you a hook where style will always be non null.