This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] #5977 - MyLocationView.setPadding example + formatting Mani…
…fest
- Loading branch information
Showing
7 changed files
with
274 additions
and
53 deletions.
There are no files selected for viewing
111 changes: 59 additions & 52 deletions
111
platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml
Large diffs are not rendered by default.
Oops, something went wrong.
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
187 changes: 187 additions & 0 deletions
187
...pp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/navigation/CarDrivingActivity.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,187 @@ | ||
package com.mapbox.mapboxsdk.testapp.activity.navigation; | ||
|
||
import android.Manifest; | ||
import android.content.pm.PackageManager; | ||
import android.graphics.Color; | ||
import android.graphics.PorterDuff; | ||
import android.graphics.drawable.Drawable; | ||
import android.location.Location; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.support.annotation.Nullable; | ||
import android.support.annotation.UiThread; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.support.v4.content.ContextCompat; | ||
import android.support.v4.content.res.ResourcesCompat; | ||
import android.support.v7.app.ActionBar; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.support.v7.widget.Toolbar; | ||
import android.view.MenuItem; | ||
|
||
|
||
import com.mapbox.mapboxsdk.camera.CameraPosition; | ||
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory; | ||
import com.mapbox.mapboxsdk.constants.MyBearingTracking; | ||
import com.mapbox.mapboxsdk.constants.MyLocationTracking; | ||
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.TrackingSettings; | ||
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings; | ||
import com.mapbox.mapboxsdk.testapp.R; | ||
|
||
public class CarDrivingActivity extends AppCompatActivity implements MapboxMap.OnMyLocationChangeListener { | ||
|
||
private MapView mMapView; | ||
private MapboxMap mapboxMap; | ||
private Location mLocation; | ||
private static final int PERMISSIONS_LOCATION = 0; | ||
|
||
@Override | ||
protected void onCreate(final Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_car_driving); | ||
|
||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | ||
setSupportActionBar(toolbar); | ||
|
||
final ActionBar actionBar = getSupportActionBar(); | ||
if (actionBar != null) { | ||
actionBar.setDisplayHomeAsUpEnabled(true); | ||
actionBar.setDisplayShowHomeEnabled(true); | ||
} | ||
|
||
mMapView = (MapView) findViewById(R.id.mapView); | ||
mMapView.onCreate(savedInstanceState); | ||
mMapView.getMapAsync(new OnMapReadyCallback() { | ||
@Override | ||
public void onMapReady(@NonNull MapboxMap mapboxMap) { | ||
CarDrivingActivity.this.mapboxMap = mapboxMap; | ||
|
||
// view settings | ||
MyLocationViewSettings settings = mapboxMap.getMyLocationViewSettings(); | ||
settings.setPadding(getWindow().getDecorView().getMeasuredHeight() / 3, getWindow().getDecorView().getMeasuredHeight() / 3, 0, 0); | ||
|
||
// get car | ||
Drawable car = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_taxi_top_small, getTheme()); | ||
settings.setForegroundTintColor(Color.TRANSPARENT); | ||
settings.setForegroundDrawable(car, car); | ||
|
||
// remove accuracy circle | ||
settings.setAccuracyAlpha(0); | ||
|
||
// disable dismissal when a gesture occurs | ||
TrackingSettings trackingSettings = mapboxMap.getTrackingSettings(); | ||
trackingSettings.setDismissLocationTrackingOnGesture(false); | ||
trackingSettings.setDismissBearingTrackingOnGesture(false); | ||
|
||
mapboxMap.setOnMyLocationChangeListener(CarDrivingActivity.this); | ||
|
||
if (savedInstanceState == null) { | ||
toggleGps(true); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@UiThread | ||
public void toggleGps(boolean enableGps) { | ||
if (enableGps) { | ||
if ((ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) || | ||
(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) { | ||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSIONS_LOCATION); | ||
} else { | ||
enableLocation(true); | ||
} | ||
} else { | ||
enableLocation(false); | ||
} | ||
} | ||
|
||
private void enableLocation(boolean enabled) { | ||
if (enabled) { | ||
mapboxMap.setMyLocationEnabled(true); | ||
Location location = mapboxMap.getMyLocation(); | ||
if (location != null) { | ||
setInitialPosition(new LatLng(location)); | ||
} | ||
} else { | ||
mapboxMap.setMyLocationEnabled(false); | ||
} | ||
} | ||
|
||
@Override | ||
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { | ||
switch (requestCode) { | ||
case PERMISSIONS_LOCATION: { | ||
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | ||
enableLocation(true); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void setInitialPosition(LatLng latLng) { | ||
mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition( | ||
new CameraPosition.Builder().target(latLng).zoom(15).tilt(20f).build())); | ||
mapboxMap.setMyLocationEnabled(true); | ||
|
||
TrackingSettings trackingSettings = mapboxMap.getTrackingSettings(); | ||
trackingSettings.setMyLocationTrackingMode(MyLocationTracking.TRACKING_FOLLOW); | ||
//§ trackingSettings.setMyBearingTrackingMode(MyBearingTracking.GPS); | ||
} | ||
|
||
@Override | ||
public void onMyLocationChange(@Nullable Location location) { | ||
if (location != null) { | ||
if (mLocation == null) { | ||
// initial location to reposition map | ||
setInitialPosition(new LatLng(location)); | ||
} | ||
mLocation = location; | ||
} | ||
} | ||
|
||
@Override | ||
public void onResume() { | ||
super.onResume(); | ||
mMapView.onResume(); | ||
} | ||
|
||
@Override | ||
public void onPause() { | ||
super.onPause(); | ||
mMapView.onPause(); | ||
} | ||
|
||
@Override | ||
protected void onSaveInstanceState(Bundle outState) { | ||
super.onSaveInstanceState(outState); | ||
mMapView.onSaveInstanceState(outState); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
mMapView.onDestroy(); | ||
} | ||
|
||
@Override | ||
public void onLowMemory() { | ||
super.onLowMemory(); | ||
mMapView.onLowMemory(); | ||
} | ||
|
||
@Override | ||
public boolean onOptionsItemSelected(MenuItem item) { | ||
switch (item.getItemId()) { | ||
case android.R.id.home: | ||
onBackPressed(); | ||
return true; | ||
default: | ||
return super.onOptionsItemSelected(item); | ||
} | ||
} | ||
|
||
} |
Binary file added
BIN
+9.85 KB
...d/MapboxGLAndroidSDKTestApp/src/main/res/drawable-xxxhdpi/ic_taxi_top_small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions
24
platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_car_driving.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,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:orientation="vertical"> | ||
|
||
<android.support.v7.widget.Toolbar | ||
android:id="@+id/toolbar" | ||
android:layout_width="match_parent" | ||
android:layout_height="?attr/actionBarSize" | ||
android:background="@color/primary" | ||
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> | ||
|
||
<com.mapbox.mapboxsdk.maps.MapView | ||
android:id="@+id/mapView" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
app:my_location_foreground_tint="@color/primary" | ||
app:my_location_accuracy_tint="@color/primary" | ||
app:style_url="@string/style_mapbox_streets" | ||
app:zoom="15" /> | ||
|
||
</LinearLayout> |
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