Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] #5977 - MyLocationView.setPadding example + formatting Mani…
Browse files Browse the repository at this point in the history
…fest
  • Loading branch information
tobrun committed Aug 22, 2016
1 parent f263087 commit 76a62f9
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 53 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ private boolean requiresLocationPermission(String name, String category) {
{
add(resources.getString(R.string.activity_double_map));
add(getString(R.string.activity_location_picker));
add(getString(R.string.activity_car_driving));
}
};

Expand Down
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);
}
}

}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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>
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

<!-- Navigation -->
<string name="activity_location_picker">Location Picker</string>
<string name="activity_car_driving">Driving animation</string>

<!-- Other -->
<string name="activity_double_map">Double Map Activity</string>
Expand Down Expand Up @@ -106,6 +107,7 @@
<string name="description_query_rendered_feature_properties_point">Query rendered feature properties on click</string>
<string name="description_query_rendered_features_box_count">Count all rendered features in box</string>
<string name="description_query_rendered_features_box_highlight">Hightligh buildings in box</string>
<string name="description_car_driving">MyLocationView follow map update example</string>

<string name="menuitem_title_concurrent_infowindow">Concurrent Open InfoWindows</string>
<string name="menuitem_title_deselect_markers_on_tap">Deselect Markers On Tap</string>
Expand Down
2 changes: 1 addition & 1 deletion platform/android/scripts/generate-test-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ global.camelize = function (str) {
}


const excludeActivities = ["MyLocationTrackingModeActivity","MyLocationToggleActivity","MyLocationTintActivity","MyLocationDrawableActivity","DoubleMapActivity", "LocationPickerActivity","GeoJsonClusteringActivity","RuntimeStyleTestActivity", "AnimatedMarkerActivity", "ViewPagerActivity","MapFragmentActivity","SupportMapFragmentActivity","SnapshotActivity","NavigationDrawerActivity"];
const excludeActivities = ["CarDrivingActivity","MyLocationTrackingModeActivity","MyLocationToggleActivity","MyLocationTintActivity","MyLocationDrawableActivity","DoubleMapActivity", "LocationPickerActivity","GeoJsonClusteringActivity","RuntimeStyleTestActivity", "AnimatedMarkerActivity", "ViewPagerActivity","MapFragmentActivity","SupportMapFragmentActivity","SnapshotActivity","NavigationDrawerActivity"];
const appBasePath = 'platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity';
const testBasePath = 'platform/android/MapboxGLAndroidSDKTestApp/src/androidTest/java/com/mapbox/mapboxsdk/activity/gen';
const subPackages = fs.readdirSync(appBasePath);
Expand Down

0 comments on commit 76a62f9

Please sign in to comment.