1+ package com .mapbox .mapboxandroiddemo .examples .location ;
2+
3+ import android .os .Bundle ;
4+ import android .widget .Toast ;
5+
6+ import com .mapbox .android .core .permissions .PermissionsListener ;
7+ import com .mapbox .android .core .permissions .PermissionsManager ;
8+ import com .mapbox .mapboxandroiddemo .R ;
9+ import com .mapbox .mapboxsdk .Mapbox ;
10+ import com .mapbox .mapboxsdk .location .LocationComponent ;
11+ import com .mapbox .mapboxsdk .location .LocationComponentActivationOptions ;
12+ import com .mapbox .mapboxsdk .location .LocationComponentOptions ;
13+ import com .mapbox .mapboxsdk .location .modes .CameraMode ;
14+ import com .mapbox .mapboxsdk .location .modes .RenderMode ;
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 .mapboxsdk .maps .Style ;
19+
20+ import java .util .List ;
21+
22+ import androidx .annotation .NonNull ;
23+ import androidx .appcompat .app .AppCompatActivity ;
24+
25+ /**
26+ * Use the {@link LocationComponentOptions} builder's pulseEnabled()
27+ * method to enable basic pulsing of the LocationComponent's pulsing circle.
28+ */
29+ public class LocationComponentBasicPulsingActivity extends AppCompatActivity implements
30+ OnMapReadyCallback , PermissionsListener {
31+
32+ private PermissionsManager permissionsManager ;
33+ private MapboxMap mapboxMap ;
34+ private MapView mapView ;
35+
36+ @ Override
37+ protected void onCreate (Bundle savedInstanceState ) {
38+ super .onCreate (savedInstanceState );
39+
40+ // Mapbox access token is configured here. This needs to be called either in your application
41+ // object or in the same activity which contains the mapview.
42+ Mapbox .getInstance (this , getString (R .string .access_token ));
43+
44+ // This contains the MapView in XML and needs to be called after the access token is configured.
45+ setContentView (R .layout .activity_location_component );
46+
47+ mapView = findViewById (R .id .mapView );
48+ mapView .onCreate (savedInstanceState );
49+ mapView .getMapAsync (this );
50+ }
51+
52+ @ Override
53+ public void onMapReady (@ NonNull final MapboxMap mapboxMap ) {
54+ LocationComponentBasicPulsingActivity .this .mapboxMap = mapboxMap ;
55+ mapboxMap .setStyle (Style .MAPBOX_STREETS , new Style .OnStyleLoaded () {
56+ @ Override
57+ public void onStyleLoaded (@ NonNull Style style ) {
58+ enableLocationComponent (style );
59+ }
60+ });
61+ }
62+
63+ @ SuppressWarnings ( {"MissingPermission" })
64+ private void enableLocationComponent (@ NonNull Style loadedMapStyle ) {
65+ // Check if permissions are enabled and if not request
66+ if (PermissionsManager .areLocationPermissionsGranted (this )) {
67+
68+ // Enable the most basic pulsing styling by ONLY using
69+ // the `.pulseEnabled()` method
70+ LocationComponentOptions customLocationComponentOptions = LocationComponentOptions .builder (this )
71+ .pulseEnabled (true )
72+ .build ();
73+
74+ // Get an instance of the component
75+ LocationComponent locationComponent = mapboxMap .getLocationComponent ();
76+
77+ // Activate with options
78+ locationComponent .activateLocationComponent (
79+ LocationComponentActivationOptions .builder (this , loadedMapStyle )
80+ .locationComponentOptions (customLocationComponentOptions )
81+ .build ());
82+
83+ // Enable to make component visible
84+ locationComponent .setLocationComponentEnabled (true );
85+
86+ // Set the component's camera mode
87+ locationComponent .setCameraMode (CameraMode .TRACKING );
88+
89+ // Set the component's render mode
90+ locationComponent .setRenderMode (RenderMode .NORMAL );
91+ } else {
92+ permissionsManager = new PermissionsManager (this );
93+ permissionsManager .requestLocationPermissions (this );
94+ }
95+ }
96+
97+ @ Override
98+ public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
99+ permissionsManager .onRequestPermissionsResult (requestCode , permissions , grantResults );
100+ }
101+
102+ @ Override
103+ public void onExplanationNeeded (List <String > permissionsToExplain ) {
104+ Toast .makeText (this , R .string .user_location_permission_explanation , Toast .LENGTH_LONG ).show ();
105+ }
106+
107+ @ Override
108+ public void onPermissionResult (boolean granted ) {
109+ if (granted ) {
110+ mapboxMap .getStyle (new Style .OnStyleLoaded () {
111+ @ Override
112+ public void onStyleLoaded (@ NonNull Style style ) {
113+ enableLocationComponent (style );
114+ }
115+ });
116+ } else {
117+ Toast .makeText (this , R .string .user_location_permission_not_granted , Toast .LENGTH_LONG ).show ();
118+ finish ();
119+ }
120+ }
121+
122+ @ Override
123+ @ SuppressWarnings ( {"MissingPermission" })
124+ protected void onStart () {
125+ super .onStart ();
126+ mapView .onStart ();
127+ }
128+
129+ @ Override
130+ protected void onResume () {
131+ super .onResume ();
132+ mapView .onResume ();
133+ }
134+
135+ @ Override
136+ protected void onPause () {
137+ super .onPause ();
138+ mapView .onPause ();
139+ }
140+
141+ @ Override
142+ protected void onStop () {
143+ super .onStop ();
144+ mapView .onStop ();
145+ }
146+
147+ @ Override
148+ protected void onSaveInstanceState (Bundle outState ) {
149+ super .onSaveInstanceState (outState );
150+ mapView .onSaveInstanceState (outState );
151+ }
152+
153+ @ Override
154+ protected void onDestroy () {
155+ super .onDestroy ();
156+ mapView .onDestroy ();
157+ }
158+
159+ @ Override
160+ public void onLowMemory () {
161+ super .onLowMemory ();
162+ mapView .onLowMemory ();
163+ }
164+ }
0 commit comments