1+ package com .mapbox .services .android .testapp .utils ;
2+
3+ import android .os .Bundle ;
4+ import android .support .design .widget .Snackbar ;
5+ import android .support .v7 .app .AppCompatActivity ;
6+ import android .support .v7 .widget .Toolbar ;
7+ import android .view .View ;
8+ import android .widget .AdapterView ;
9+ import android .widget .ArrayAdapter ;
10+ import android .widget .Spinner ;
11+
12+ import com .mapbox .mapboxsdk .annotations .Marker ;
13+ import com .mapbox .mapboxsdk .annotations .MarkerViewOptions ;
14+ import com .mapbox .mapboxsdk .geometry .LatLng ;
15+ import com .mapbox .mapboxsdk .maps .MapView ;
16+ import com .mapbox .mapboxsdk .maps .MapboxMap ;
17+ import com .mapbox .mapboxsdk .maps .OnMapReadyCallback ;
18+ import com .mapbox .services .android .testapp .R ;
19+ import com .mapbox .services .android .testapp .Utils ;
20+ import com .mapbox .services .commons .geojson .Geometry ;
21+ import com .mapbox .services .commons .geojson .LineString ;
22+ import com .mapbox .services .commons .geojson .Point ;
23+ import com .mapbox .services .commons .models .Position ;
24+ import com .mapbox .services .commons .turf .TurfConstants ;
25+ import com .mapbox .services .commons .turf .TurfException ;
26+ import com .mapbox .services .commons .turf .TurfMeasurement ;
27+ import com .mapbox .services .commons .turf .TurfMisc ;
28+
29+ import java .util .ArrayList ;
30+ import java .util .List ;
31+
32+ public class TurfCalculationsActivity extends AppCompatActivity {
33+
34+ private MapView mapView ;
35+ private MapboxMap map ;
36+
37+ private Marker cadillacHotel ;
38+ private Marker horizonAve ;
39+ private Marker westminsterAve ;
40+
41+ private Position cadillacHotelPosition = Position .fromCoordinates (-118.479852 , 33.993898 );
42+ private Position horizionAvePosition = Position .fromCoordinates (-118.474681 , 33.988071 );
43+ private Position westminsterAvePosition = Position .fromCoordinates (-118.470658 , 33.991861 );
44+
45+ private View container ;
46+
47+
48+ @ Override
49+ protected void onCreate (Bundle savedInstanceState ) {
50+ super .onCreate (savedInstanceState );
51+ setContentView (R .layout .activity_utils_turf_calculations );
52+
53+ container = findViewById (R .id .turf_calculation_map_container );
54+
55+ Toolbar toolbar = (Toolbar ) findViewById (R .id .toolbar );
56+ setSupportActionBar (toolbar );
57+
58+ getSupportActionBar ().setDisplayHomeAsUpEnabled (true );
59+
60+ ArrayAdapter <CharSequence > turfCalculationsAdapter = ArrayAdapter .createFromResource (TurfCalculationsActivity .this , R .array .turf_calculation_items , android .R .layout .simple_spinner_item );
61+ turfCalculationsAdapter .setDropDownViewResource (android .R .layout .simple_spinner_dropdown_item );
62+ Spinner turfCalculation = (Spinner ) findViewById (R .id .turf_calculation_spinner );
63+ turfCalculation .setAdapter (turfCalculationsAdapter );
64+ turfCalculation .setOnItemSelectedListener (new AdapterView .OnItemSelectedListener () {
65+ @ Override
66+ public void onItemSelected (AdapterView <?> parent , View view , int position , long id ) {
67+ switch (position ) {
68+ case 0 :
69+ // select one
70+ break ;
71+ case 1 :
72+ double bearing = TurfMeasurement .bearing (Point .fromCoordinates (cadillacHotelPosition ), Point .fromCoordinates (horizionAvePosition ));
73+ Snackbar .make (container , "Bearing = " + bearing , Snackbar .LENGTH_INDEFINITE ).show ();
74+ break ;
75+ case 2 :
76+ try {
77+ Point result = TurfMeasurement .destination (Point .fromCoordinates (cadillacHotelPosition ), 1 , 45 , TurfConstants .UNIT_MILES );
78+ Position resultPosition = result .getCoordinates ();
79+ Snackbar .make (container , "Destination = " + resultPosition .getLatitude () + ", " + resultPosition .getLongitude (), Snackbar .LENGTH_INDEFINITE ).show ();
80+ } catch (TurfException e ) {
81+ e .printStackTrace ();
82+ }
83+ break ;
84+ case 3 :
85+ try {
86+ double distance = TurfMeasurement .distance (Point .fromCoordinates (cadillacHotelPosition ), Point .fromCoordinates (horizionAvePosition ), TurfConstants .UNIT_MILES );
87+ Snackbar .make (container , "Distance in miles= " + distance , Snackbar .LENGTH_INDEFINITE ).show ();
88+ } catch (TurfException e ) {
89+ e .printStackTrace ();
90+ }
91+ break ;
92+ case 4 :
93+ // TODO
94+ // TurfMeasurement.lineDistance()
95+ break ;
96+ case 5 :
97+ List <Position > linePoints = new ArrayList <>();
98+ linePoints .add (cadillacHotelPosition );
99+ linePoints .add (horizionAvePosition );
100+ linePoints .add (westminsterAvePosition );
101+ linePoints .add (cadillacHotelPosition );
102+
103+ LineString lineString = LineString .fromCoordinates (linePoints );
104+
105+ try {
106+ LineString slicedLine = TurfMisc .lineSlice (Point .fromCoordinates (horizionAvePosition ), Point .fromCoordinates (westminsterAvePosition ), lineString );
107+ //List<Position> slicedLinePoints = slicedLine.getCoordinates();
108+
109+ //for (int i = 0; i < slicedLinePoints.size(); i++) {
110+ // map.addMarker(new MarkerViewOptions().position(new LatLng(slicedLinePoints.get(i).getLatitude(), slicedLinePoints.get(i).getLongitude())));
111+ //}
112+ } catch (TurfException e ) {
113+ e .printStackTrace ();
114+ }
115+ break ;
116+ }
117+ }
118+
119+ @ Override
120+ public void onNothingSelected (AdapterView <?> parent ) {
121+
122+ }
123+ });
124+
125+ mapView = (MapView ) findViewById (R .id .mapView );
126+ mapView .setAccessToken (Utils .getMapboxAccessToken (this ));
127+ mapView .onCreate (savedInstanceState );
128+ mapView .getMapAsync (new OnMapReadyCallback () {
129+ @ Override
130+ public void onMapReady (MapboxMap mapboxMap ) {
131+ map = mapboxMap ;
132+
133+ cadillacHotel = mapboxMap .addMarker (new MarkerViewOptions ()
134+ .position (new LatLng (33.993898 , -118.479852 )));
135+
136+ horizonAve = mapboxMap .addMarker (new MarkerViewOptions ()
137+ .position (new LatLng (33.988071 , -118.474681 )));
138+
139+ westminsterAve = mapboxMap .addMarker (new MarkerViewOptions ()
140+ .position (new LatLng (33.991861 , -118.470658 )));
141+ }
142+ });
143+ }
144+
145+ @ Override
146+ public void onResume () {
147+ super .onResume ();
148+ mapView .onResume ();
149+ }
150+
151+ @ Override
152+ public void onPause () {
153+ super .onPause ();
154+ mapView .onPause ();
155+ }
156+
157+ @ Override
158+ public void onLowMemory () {
159+ super .onLowMemory ();
160+ mapView .onLowMemory ();
161+ }
162+
163+ @ Override
164+ protected void onDestroy () {
165+ super .onDestroy ();
166+ mapView .onDestroy ();
167+ }
168+
169+ @ Override
170+ protected void onSaveInstanceState (Bundle outState ) {
171+ super .onSaveInstanceState (outState );
172+ mapView .onSaveInstanceState (outState );
173+ }
174+ }
0 commit comments