1+ package com .mapbox .services .android .testapp .turf ;
2+
3+ import android .app .Dialog ;
4+ import android .os .Bundle ;
5+ import android .support .design .widget .FloatingActionButton ;
6+ import android .support .design .widget .Snackbar ;
7+ import android .support .v7 .app .AppCompatActivity ;
8+ import android .support .v7 .widget .Toolbar ;
9+ import android .view .View ;
10+ import android .widget .Button ;
11+ import android .widget .EditText ;
12+ import android .widget .Spinner ;
13+ import android .widget .Toast ;
14+
15+ import com .mapbox .mapboxsdk .annotations .Marker ;
16+ import com .mapbox .mapboxsdk .annotations .MarkerViewOptions ;
17+ import com .mapbox .mapboxsdk .camera .CameraUpdateFactory ;
18+ import com .mapbox .mapboxsdk .geometry .LatLng ;
19+ import com .mapbox .mapboxsdk .geometry .LatLngBounds ;
20+ import com .mapbox .mapboxsdk .maps .MapView ;
21+ import com .mapbox .mapboxsdk .maps .MapboxMap ;
22+ import com .mapbox .mapboxsdk .maps .OnMapReadyCallback ;
23+ import com .mapbox .services .android .testapp .R ;
24+ import com .mapbox .services .android .testapp .Utils ;
25+ import com .mapbox .services .commons .geojson .Point ;
26+ import com .mapbox .services .commons .models .Position ;
27+ import com .mapbox .services .commons .turf .TurfConstants ;
28+ import com .mapbox .services .commons .turf .TurfException ;
29+ import com .mapbox .services .commons .turf .TurfMeasurement ;
30+
31+ public class TurfDestinationActivity extends AppCompatActivity {
32+
33+ private MapView mapView ;
34+ private MapboxMap map ;
35+ private Marker destinationMarker ;
36+
37+ private Position cadillacHotelPosition = Position .fromCoordinates (-118.479852 , 33.993898 );
38+
39+ private View container ;
40+
41+ @ Override
42+ protected void onCreate (Bundle savedInstanceState ) {
43+ super .onCreate (savedInstanceState );
44+ setContentView (R .layout .activity_turf_destination );
45+
46+ container = findViewById (R .id .turf_destination_container );
47+
48+ Toolbar toolbar = (Toolbar ) findViewById (R .id .toolbar );
49+ setSupportActionBar (toolbar );
50+
51+ getSupportActionBar ().setDisplayHomeAsUpEnabled (true );
52+
53+ FloatingActionButton fab = (FloatingActionButton ) findViewById (R .id .turf_destination_fab );
54+ if (fab != null )
55+ fab .setOnClickListener (new View .OnClickListener () {
56+ @ Override
57+ public void onClick (View v ) {
58+ if (map != null ) destination ();
59+ }
60+ });
61+
62+ mapView = (MapView ) findViewById (R .id .mapView );
63+ mapView .setAccessToken (Utils .getMapboxAccessToken (this ));
64+ mapView .onCreate (savedInstanceState );
65+ mapView .getMapAsync (new OnMapReadyCallback () {
66+ @ Override
67+ public void onMapReady (MapboxMap mapboxMap ) {
68+ map = mapboxMap ;
69+ destination ();
70+ }
71+ });
72+ }
73+
74+ @ Override
75+ public void onResume () {
76+ super .onResume ();
77+ mapView .onResume ();
78+ }
79+
80+ @ Override
81+ public void onPause () {
82+ super .onPause ();
83+ mapView .onPause ();
84+ }
85+
86+ @ Override
87+ public void onLowMemory () {
88+ super .onLowMemory ();
89+ mapView .onLowMemory ();
90+ }
91+
92+ @ Override
93+ protected void onDestroy () {
94+ super .onDestroy ();
95+ mapView .onDestroy ();
96+ }
97+
98+ @ Override
99+ protected void onSaveInstanceState (Bundle outState ) {
100+ super .onSaveInstanceState (outState );
101+ mapView .onSaveInstanceState (outState );
102+ }
103+
104+ private void destination () {
105+
106+ map .addMarker (new MarkerViewOptions ()
107+ .position (new LatLng (cadillacHotelPosition .getLatitude (), cadillacHotelPosition .getLongitude ()))
108+ .title ("point 1" ));
109+
110+ // custom dialog
111+ final Dialog dialog = new Dialog (TurfDestinationActivity .this );
112+ dialog .setContentView (R .layout .layout_turf_destination_dialog );
113+
114+ final EditText distanceInput = (EditText ) dialog .findViewById (R .id .turf_destination_distance_input );
115+ final EditText bearingInput = (EditText ) dialog .findViewById (R .id .turf_destination_bearing_input );
116+ final Spinner distanceUnitsSpinner = (Spinner ) dialog .findViewById (R .id .turf_destination_unit_spinner );
117+ Button calculateButton = (Button ) dialog .findViewById (R .id .turf_destination_calculate_button );
118+ if (calculateButton != null )
119+ calculateButton .setOnClickListener (new View .OnClickListener () {
120+ @ Override
121+ public void onClick (View v ) {
122+ double distance ;
123+ double bearing ;
124+
125+
126+ if (distanceInput .getText ().length () != 0 ) {
127+ distance = Double .parseDouble (distanceInput .getText ().toString ());
128+ } else {
129+ Toast .makeText (TurfDestinationActivity .this , "missing distance value" , Toast .LENGTH_LONG ).show ();
130+ return ;
131+ }
132+
133+ if (bearingInput .getText ().length () != 0 ) {
134+ bearing = Double .parseDouble (bearingInput .getText ().toString ());
135+ } else {
136+ Toast .makeText (TurfDestinationActivity .this , "missing bearing value" , Toast .LENGTH_LONG ).show ();
137+ return ;
138+ }
139+ try {
140+ Point result = TurfMeasurement .destination (Point .fromCoordinates (cadillacHotelPosition ), distance , bearing , distanceUnitsSpinner .getSelectedItem ().toString ());
141+ Position resultPosition = result .getCoordinates ();
142+ if (destinationMarker != null ) map .removeMarker (destinationMarker );
143+ destinationMarker = map .addMarker (new MarkerViewOptions ()
144+ .position (new LatLng (result .getCoordinates ().getLatitude (), result .getCoordinates ().getLongitude ()))
145+ .title ("destination" ));
146+ LatLngBounds latLngBounds = new LatLngBounds .Builder ()
147+ .include (new LatLng (cadillacHotelPosition .getLatitude (), cadillacHotelPosition .getLongitude ()))
148+ .include (new LatLng (resultPosition .getLatitude (), resultPosition .getLongitude ()))
149+ .build ();
150+
151+ map .animateCamera (CameraUpdateFactory .newLatLngBounds (latLngBounds , 80 , 160 , 80 , 160 ), 1000 );
152+
153+ Snackbar .make (container , "Destination = " + resultPosition .getLatitude () + ", " + resultPosition .getLongitude (), Snackbar .LENGTH_INDEFINITE ).show ();
154+ } catch (TurfException e ) {
155+ e .printStackTrace ();
156+ }
157+
158+ dialog .dismiss ();
159+
160+ }
161+ });
162+
163+ dialog .show ();
164+ }
165+ }
0 commit comments