Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit e6ef5eb

Browse files
authored
User Location on a Map Fragment Example (#843)
1 parent e57973a commit e6ef5eb

File tree

9 files changed

+192
-1
lines changed

9 files changed

+192
-1
lines changed

MapboxAndroidDemo/src/main/AndroidManifest.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@
154154
android:name="android.support.PARENT_ACTIVITY"
155155
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
156156
</activity>
157+
<activity
158+
android:name=".examples.plugins.LocationPluginFragmentActivity"
159+
android:label="@string/activity_user_location_map_frag_title">
160+
<meta-data
161+
android:name="android.support.PARENT_ACTIVITY"
162+
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
163+
</activity>
157164
<activity
158165
android:name=".examples.camera.BoundingBoxCameraActivity"
159166
android:label="@string/activity_camera_bounding_box_title">

MapboxAndroidDemo/src/main/java/com/mapbox/mapboxandroiddemo/MainActivity.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
import com.mapbox.mapboxandroiddemo.examples.plugins.BuildingPluginActivity;
8585
import com.mapbox.mapboxandroiddemo.examples.plugins.LocalizationPluginActivity;
8686
import com.mapbox.mapboxandroiddemo.examples.plugins.LocationPluginActivity;
87+
import com.mapbox.mapboxandroiddemo.examples.plugins.LocationPluginFragmentActivity;
8788
import com.mapbox.mapboxandroiddemo.examples.plugins.PlaceSelectionPluginActivity;
8889
import com.mapbox.mapboxandroiddemo.examples.plugins.PlacesPluginActivity;
8990
import com.mapbox.mapboxandroiddemo.examples.plugins.TrafficPluginActivity;
@@ -456,6 +457,14 @@ private void listItems(int id) {
456457
new Intent(MainActivity.this, PlaceSelectionPluginActivity.class),
457458
R.string.activity_plugins_place_picker_plugin_url, true, BuildConfig.MIN_SDK_VERSION)
458459
);
460+
461+
exampleItemModels.add(new ExampleItemModel(
462+
R.string.activity_user_location_map_frag_title,
463+
R.string.activity_user_location_map_frag_plugin_description,
464+
new Intent(MainActivity.this, LocationPluginFragmentActivity.class),
465+
R.string.activity_user_location_fragment_plugin_url, true, BuildConfig.MIN_SDK_VERSION)
466+
);
467+
459468
currentCategory = R.id.nav_plugins;
460469
break;
461470

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.mapbox.mapboxandroiddemo.examples.plugins;
2+
3+
import android.os.Bundle;
4+
import android.support.annotation.NonNull;
5+
import android.support.v4.app.FragmentTransaction;
6+
import android.support.v7.app.AppCompatActivity;
7+
import android.widget.Toast;
8+
9+
import com.mapbox.android.core.permissions.PermissionsListener;
10+
import com.mapbox.android.core.permissions.PermissionsManager;
11+
import com.mapbox.mapboxandroiddemo.R;
12+
import com.mapbox.mapboxsdk.Mapbox;
13+
import com.mapbox.mapboxsdk.camera.CameraPosition;
14+
import com.mapbox.mapboxsdk.constants.Style;
15+
import com.mapbox.mapboxsdk.geometry.LatLng;
16+
import com.mapbox.mapboxsdk.maps.MapFragment;
17+
import com.mapbox.mapboxsdk.maps.MapView;
18+
import com.mapbox.mapboxsdk.maps.MapboxMap;
19+
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
20+
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
21+
import com.mapbox.mapboxsdk.maps.SupportMapFragment;
22+
import com.mapbox.mapboxsdk.plugins.locationlayer.LocationLayerPlugin;
23+
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.CameraMode;
24+
import com.mapbox.mapboxsdk.plugins.locationlayer.modes.RenderMode;
25+
26+
import java.util.List;
27+
28+
// #-code-snippet: location-plugin-fragment-activity full-java
29+
30+
public class LocationPluginFragmentActivity extends AppCompatActivity implements
31+
MapFragment.OnMapViewReadyCallback, PermissionsListener {
32+
33+
private LocationLayerPlugin locationLayerPlugin;
34+
private MapView mapView;
35+
private MapboxMap mapboxMap;
36+
private PermissionsManager permissionsManager;
37+
38+
@Override
39+
protected void onCreate(Bundle savedInstanceState) {
40+
super.onCreate(savedInstanceState);
41+
setContentView(R.layout.activity_user_location_map_frag);
42+
43+
// Mapbox access token is configured here. This needs to be called either in your application
44+
// object or in the same activity which contains the mapview.
45+
Mapbox.getInstance(this, getString(R.string.access_token));
46+
47+
// Create supportMapFragment
48+
SupportMapFragment mapFragment;
49+
if (savedInstanceState == null) {
50+
51+
// Create fragment
52+
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
53+
54+
LatLng office = new LatLng(38.899895, -77.03401);
55+
56+
// Build mapboxMap
57+
MapboxMapOptions options = new MapboxMapOptions();
58+
options.styleUrl(Style.OUTDOORS);
59+
options.camera(new CameraPosition.Builder()
60+
.target(office)
61+
.zoom(9)
62+
.build());
63+
64+
// Create map fragment
65+
mapFragment = SupportMapFragment.newInstance(options);
66+
67+
// Add map fragment to parent container
68+
transaction.add(R.id.location_frag_container, mapFragment, "com.mapbox.map");
69+
transaction.commit();
70+
} else {
71+
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag("com.mapbox.map");
72+
}
73+
74+
mapFragment.getMapAsync(new OnMapReadyCallback() {
75+
@Override
76+
public void onMapReady(MapboxMap mapboxMap) {
77+
LocationPluginFragmentActivity.this.mapboxMap = mapboxMap;
78+
enableLocationPlugin();
79+
}
80+
});
81+
}
82+
83+
@Override
84+
public void onMapViewReady(MapView mapView) {
85+
this.mapView = mapView;
86+
}
87+
88+
@SuppressWarnings( {"MissingPermission"})
89+
private void enableLocationPlugin() {
90+
// Check if permissions are enabled and if not request
91+
if (PermissionsManager.areLocationPermissionsGranted(this)) {
92+
// Create an instance of the plugin. Adding in LocationLayerOptions is also an optional
93+
// parameter
94+
locationLayerPlugin = new LocationLayerPlugin(mapView, mapboxMap);
95+
locationLayerPlugin.setCameraMode(CameraMode.TRACKING);
96+
locationLayerPlugin.setRenderMode(RenderMode.NORMAL);
97+
getLifecycle().addObserver(locationLayerPlugin);
98+
} else {
99+
permissionsManager = new PermissionsManager(this);
100+
permissionsManager.requestLocationPermissions(this);
101+
}
102+
getLifecycle().addObserver(locationLayerPlugin);
103+
}
104+
105+
@Override
106+
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
107+
permissionsManager.onRequestPermissionsResult(requestCode, permissions, grantResults);
108+
}
109+
110+
@Override
111+
public void onExplanationNeeded(List<String> permissionsToExplain) {
112+
Toast.makeText(this, R.string.user_location_permission_explanation, Toast.LENGTH_LONG).show();
113+
}
114+
115+
@Override
116+
public void onResume() {
117+
super.onResume();
118+
if (!PermissionsManager.areLocationPermissionsGranted(this)) {
119+
getLifecycle().removeObserver(locationLayerPlugin);
120+
}
121+
}
122+
123+
@Override
124+
public void onPermissionResult(boolean granted) {
125+
if (granted) {
126+
enableLocationPlugin();
127+
} else {
128+
Toast.makeText(this, R.string.user_location_permission_not_granted, Toast.LENGTH_LONG).show();
129+
finish();
130+
}
131+
}
132+
}
133+
// #-end-code-snippet: location-plugin-fragment-activity full-java
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RelativeLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:app="http://schemas.android.com/apk/res-auto"
5+
xmlns:tools="http://schemas.android.com/tools"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
android:background="@color/white_pressed"
9+
tools:context=".examples.basics.SupportMapFragmentActivity">
10+
11+
<TextView
12+
android:id="@+id/fragment_below_textview"
13+
android:layout_width="match_parent"
14+
android:layout_height="wrap_content"
15+
android:layout_marginTop="8dp"
16+
android:gravity="center_horizontal"
17+
android:text="@string/location_layer_fragment_text"/>
18+
19+
<android.support.v7.widget.CardView
20+
android:id="@+id/location_frag_cardview"
21+
android:layout_width="match_parent"
22+
android:layout_height="match_parent"
23+
android:layout_marginHorizontal="40dp"
24+
android:layout_marginVertical="50dp"
25+
android:layout_centerInParent="true"
26+
android:layout_gravity="center_horizontal"
27+
app:cardCornerRadius="30dp"
28+
app:cardElevation="@dimen/cardview_default_elevation">
29+
30+
<FrameLayout
31+
android:id="@+id/location_frag_container"
32+
android:layout_width="match_parent"
33+
android:layout_height="match_parent" />
34+
</android.support.v7.widget.CardView>
35+
</RelativeLayout>

MapboxAndroidDemo/src/main/res/values/activity_strings.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
<string name="user_location_permission_explanation">This app needs location permissions in order to show its functionality.</string>
4040
<string name="user_location_permission_not_granted">You didn\'t grant location permissions.</string>
4141

42+
<!--Location Layer on Fragment below textview in Plugin activity-->
43+
<string name="location_layer_fragment_text">User\'s current location on a map fragment</string>
44+
4245
<!-- Settings dialog -->
4346
<string name="settings_dialog_title">Settings</string>
4447
<string name="settings_dialog_positive_button_text">Save</string>

MapboxAndroidDemo/src/main/res/values/descriptions_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
<string name="activity_plugins_places_plugin_description">Add location search ("geocoding") functionality and UI to search for any place in the world</string>
7777
<string name="activity_plugins_localization_plugin_description">Use the plugin to automatically change map label text to the language set on the device.</string>
7878
<string name="activity_plugins_place_picker_plugin_description">Use the place picker function of the Places Plugin to choose a specific location in the world.</string>
79+
<string name="activity_user_location_map_frag_plugin_description">Use the location layer plugin to display a user\'s location on a map fragment.</string>
7980
<string name="activity_java_services_simplify_polyline_description">Using the polylines utility, simplify a polyline which reduces the amount of coordinates making up the polyline depending on tolerance.</string>
8081
<string name="activity_java_services_map_matching_description">Match raw GPS points to the map so they aligns with the roads/pathways.</string>
8182
<string name="activity_image_generator_snapshot_share_description">Send and share a map snapshot image.</string>

MapboxAndroidDemo/src/main/res/values/titles_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
<string name="activity_plugins_geojson_plugin_title">Load GeoJSON data</string>
7676
<string name="activity_plugins_places_plugin_title">Location search</string>
7777
<string name="activity_plugins_place_picker_plugin_title">Place picker</string>
78+
<string name="activity_user_location_map_frag_title">Show a user\'s location on a map fragment</string>
7879
<string name="activity_java_services_simplify_polyline_title">Simplify a polyline</string>
7980
<string name="activity_java_services_map_matching_title">Map Matching</string>
8081
<string name="activity_image_generator_snapshot_notification_title">Snapshot Notification</string>

MapboxAndroidDemo/src/main/res/values/urls_strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<string name="activity_plugins_geojson_plugin_url" translatable="false">http://i.imgur.com/kju0sKw.jpg</string>
7878
<string name="activity_plugins_localization_plugin_url" translatable="false">https://i.imgur.com/tsOM1am.png</string>
7979
<string name="activity_plugins_place_picker_plugin_url" translatable="false">https://i.imgur.com/fPZQg7z.png</string>
80+
<string name="activity_user_location_fragment_plugin_url" translatable="false">https://i.imgur.com/1jG8WQG.png</string>
8081
<string name="activity_java_services_simplify_polyline_url" translatable="false">http://i.imgur.com/uATgul1.png</string>
8182
<string name="activity_java_services_map_matching_url" translatable="false">http://i.imgur.com/bWvVbwC.png</string>
8283
<string name="activity_image_generator_snapshot_notification_url" translatable="false">https://i.imgur.com/OiveDFG.png</string>

scripts/exclude-activity-gen.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
"MarathonExtrusionActivity",
66
"RotationExtrusionActivity",
77
"StaticImageActivity",
8-
"PulsingLayerOpacityColorActivity"
8+
"PulsingLayerOpacityColorActivity",
9+
"LocationPluginFragmentActivity"
910
]

0 commit comments

Comments
 (0)