Improve support for Andrioid M
Checks for and requests runtime location permissions (fixes #2110) Improved the styling of the location FAB Removed redudant drawables Fixed various Java and Android warnings and deprecated functions A few other tweaks
@@ -5,7 +5,5 @@
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||
|
||||
</manifest>
|
||||
|
||||
@@ -119,15 +119,7 @@ public final class BoundingBox implements Parcelable, Serializable {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringBuffer().append("N:")
|
||||
.append(this.mLatNorth)
|
||||
.append("; E:")
|
||||
.append(this.mLonEast)
|
||||
.append("; S:")
|
||||
.append(this.mLatSouth)
|
||||
.append("; W:")
|
||||
.append(this.mLonWest)
|
||||
.toString();
|
||||
return "N:" + this.mLatNorth + "; E:" + this.mLonEast + "; S:" + this.mLatSouth + "; W:" + this.mLonWest;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -164,17 +156,8 @@ public final class BoundingBox implements Parcelable, Serializable {
|
||||
*/
|
||||
public boolean equals(final BoundingBox other) {
|
||||
|
||||
if (other == null) {
|
||||
return false;
|
||||
}
|
||||
if (other == this) {
|
||||
return true;
|
||||
}
|
||||
return other != null && (other == this || mLatNorth == other.getLatNorth() && mLatSouth == other.getLatSouth() && mLonEast == other.getLonEast() && mLonWest == other.getLonWest());
|
||||
|
||||
return mLatNorth == other.getLatNorth()
|
||||
&& mLatSouth == other.getLatSouth()
|
||||
&& mLonEast == other.getLonEast()
|
||||
&& mLonWest == other.getLonWest();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -97,18 +97,8 @@ public class LatLng implements ILatLng, Parcelable, Serializable {
|
||||
|
||||
LatLng latLng = (LatLng) o;
|
||||
|
||||
if (Double.compare(latLng.altitude, altitude) != 0) {
|
||||
return false;
|
||||
}
|
||||
return Double.compare(latLng.altitude, altitude) == 0 && Double.compare(latLng.latitude, latitude) == 0 && Double.compare(latLng.longitude, longitude) == 0;
|
||||
|
||||
if (Double.compare(latLng.latitude, latitude) != 0) {
|
||||
return false;
|
||||
}
|
||||
if (Double.compare(latLng.longitude, longitude) != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -56,11 +56,8 @@ public class LatLngZoom extends LatLng implements Parcelable, Serializable {
|
||||
|
||||
LatLngZoom that = (LatLngZoom) o;
|
||||
|
||||
if (Double.compare(that.zoom, zoom) != 0) {
|
||||
return false;
|
||||
}
|
||||
return Double.compare(that.zoom, zoom) == 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -56,14 +56,8 @@ public class ProjectedMeters implements IProjectedMeters, Parcelable, Serializab
|
||||
|
||||
ProjectedMeters projectedMeters = (ProjectedMeters) o;
|
||||
|
||||
if (Double.compare(projectedMeters.easting, easting) != 0) {
|
||||
return false;
|
||||
}
|
||||
if (Double.compare(projectedMeters.northing, northing) != 0) {
|
||||
return false;
|
||||
}
|
||||
return Double.compare(projectedMeters.easting, easting) == 0 && Double.compare(projectedMeters.northing, northing) == 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -22,6 +22,7 @@ import android.net.NetworkInfo;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v4.view.GestureDetectorCompat;
|
||||
import android.support.v4.view.ScaleGestureDetectorCompat;
|
||||
import android.text.TextUtils;
|
||||
@@ -104,7 +105,6 @@ public class MapView extends FrameLayout implements LocationListener {
|
||||
//
|
||||
|
||||
// Used to call JNI NativeMapView
|
||||
private SurfaceView mSurfaceView;
|
||||
private NativeMapView mNativeMapView;
|
||||
|
||||
// Used to handle DPI scaling
|
||||
@@ -135,7 +135,7 @@ public class MapView extends FrameLayout implements LocationListener {
|
||||
private Location mGpsLocation;
|
||||
|
||||
public enum UserLocationTrackingMode {
|
||||
NONE, FOLLOW, FOLLOW_BEARING;
|
||||
NONE, FOLLOW, FOLLOW_BEARING
|
||||
}
|
||||
private UserLocationTrackingMode mUserLocationTrackingMode = UserLocationTrackingMode.FOLLOW;
|
||||
|
||||
@@ -215,8 +215,8 @@ public class MapView extends FrameLayout implements LocationListener {
|
||||
// Save the context
|
||||
mContext = context;
|
||||
|
||||
mSurfaceView = new SurfaceView(mContext);
|
||||
addView(mSurfaceView);
|
||||
SurfaceView surfaceView = new SurfaceView(mContext);
|
||||
addView(surfaceView);
|
||||
|
||||
// Check if we are in Eclipse UI editor
|
||||
if (isInEditMode()) {
|
||||
@@ -252,9 +252,9 @@ public class MapView extends FrameLayout implements LocationListener {
|
||||
|
||||
// Register the SurfaceHolder callbacks
|
||||
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
|
||||
mSurfaceView.getHolder().addCallback(new Callbacks2());
|
||||
surfaceView.getHolder().addCallback(new Callbacks2());
|
||||
} else {
|
||||
mSurfaceView.getHolder().addCallback(new Callbacks());
|
||||
surfaceView.getHolder().addCallback(new Callbacks());
|
||||
}
|
||||
|
||||
// Touch gesture detectors
|
||||
@@ -294,7 +294,7 @@ public class MapView extends FrameLayout implements LocationListener {
|
||||
mCompassListener = new CompassListener();
|
||||
|
||||
mCompassView = new ImageView(mContext);
|
||||
mCompassView.setImageDrawable(getResources().getDrawable(R.drawable.compass));
|
||||
mCompassView.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.compass));
|
||||
mCompassView.setContentDescription(getResources().getString(R.string.compassContentDescription));
|
||||
LayoutParams lp = new FrameLayout.LayoutParams((int)(48 * mScreenDensity), (int)(48 * mScreenDensity));
|
||||
lp.gravity = Gravity.TOP | Gravity.END;
|
||||
@@ -306,7 +306,7 @@ public class MapView extends FrameLayout implements LocationListener {
|
||||
|
||||
// Setup Support For Listener Tracking
|
||||
// MapView's internal listener is setup in onCreate()
|
||||
mOnMapChangedListener = new ArrayList<OnMapChangedListener>();
|
||||
mOnMapChangedListener = new ArrayList<>();
|
||||
|
||||
// Load the attributes
|
||||
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MapView, 0, 0);
|
||||
@@ -560,7 +560,6 @@ public class MapView extends FrameLayout implements LocationListener {
|
||||
}
|
||||
|
||||
private void validateStyleUrl(String url) {
|
||||
String[] schemes = {"http", "https", "asset"};
|
||||
url = url.replaceFirst("asset://", "http://");
|
||||
HttpUrl parsedUrl = HttpUrl.parse(url);
|
||||
if (parsedUrl == null) {
|
||||
@@ -723,7 +722,9 @@ public class MapView extends FrameLayout implements LocationListener {
|
||||
getContext().unregisterReceiver(mConnectivityReceiver);
|
||||
mConnectivityReceiver = null;
|
||||
|
||||
toggleGps(!mIsMyLocationEnabled);
|
||||
if (mIsMyLocationEnabled) {
|
||||
toggleGps(false);
|
||||
};
|
||||
|
||||
mNativeMapView.pause();
|
||||
}
|
||||
@@ -736,7 +737,9 @@ public class MapView extends FrameLayout implements LocationListener {
|
||||
mConnectivityReceiver = new ConnectivityReceiver();
|
||||
mContext.registerReceiver(mConnectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
|
||||
|
||||
toggleGps(mIsMyLocationEnabled);
|
||||
if (mIsMyLocationEnabled) {
|
||||
toggleGps(true);
|
||||
}
|
||||
|
||||
mNativeMapView.resume();
|
||||
}
|
||||
|
||||
@@ -23,4 +23,6 @@
|
||||
</application>
|
||||
|
||||
<uses-permission android:name="android.permission.INTERNET" />
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
|
||||
</manifest>
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
package com.mapbox.mapboxgl.testapp;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.PointF;
|
||||
import android.location.Location;
|
||||
import android.os.Bundle;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.design.widget.NavigationView;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v4.view.GravityCompat;
|
||||
import android.support.v4.widget.DrawerLayout;
|
||||
import android.support.v7.app.ActionBar;
|
||||
@@ -16,13 +20,10 @@ import android.view.GestureDetector;
|
||||
import android.view.MenuItem;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.widget.AdapterView;
|
||||
import android.widget.TextView;
|
||||
import com.crashlytics.android.Crashlytics;
|
||||
import com.mapbox.mapboxgl.annotations.MarkerOptions;
|
||||
import com.mapbox.mapboxgl.annotations.Polygon;
|
||||
import com.mapbox.mapboxgl.annotations.PolygonOptions;
|
||||
import com.mapbox.mapboxgl.annotations.Polyline;
|
||||
import com.mapbox.mapboxgl.annotations.PolylineOptions;
|
||||
import com.mapbox.mapboxgl.geometry.LatLng;
|
||||
import com.mapbox.mapboxgl.views.MapView;
|
||||
@@ -44,6 +45,9 @@ public class MainActivity extends AppCompatActivity {
|
||||
private static final String STATE_IS_ANNOTATIONS_ON = "isAnnotationsOn";
|
||||
private static final String STATE_SELECTED_STYLE = "selectedStyle";
|
||||
|
||||
// Used for permissions requests
|
||||
private static final int PERMISSIONS_LOCATION = 0;
|
||||
|
||||
//
|
||||
// Instance members
|
||||
//
|
||||
@@ -78,8 +82,10 @@ public class MainActivity extends AppCompatActivity {
|
||||
setSupportActionBar(toolbar);
|
||||
|
||||
final ActionBar ab = getSupportActionBar();
|
||||
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
|
||||
ab.setDisplayHomeAsUpEnabled(true);
|
||||
if (ab != null) {
|
||||
ab.setHomeAsUpIndicator(R.drawable.ic_menu);
|
||||
ab.setDisplayHomeAsUpEnabled(true);
|
||||
}
|
||||
|
||||
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
|
||||
|
||||
@@ -229,6 +235,17 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
|
||||
switch (requestCode) {
|
||||
case PERMISSIONS_LOCATION: {
|
||||
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
enableGps();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void setupDrawerContent(NavigationView navigationView) {
|
||||
navigationView.setNavigationItemSelectedListener(
|
||||
new NavigationView.OnNavigationItemSelectedListener() {
|
||||
@@ -331,14 +348,24 @@ public class MainActivity extends AppCompatActivity {
|
||||
*/
|
||||
private void toggleGps(boolean enableGps) {
|
||||
if (enableGps) {
|
||||
mMapView.setMyLocationEnabled(enableGps);
|
||||
mLocationFAB.setColorFilter(getResources().getColor(R.color.primary));
|
||||
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 {
|
||||
enableGps();
|
||||
}
|
||||
} else {
|
||||
mMapView.setMyLocationEnabled(enableGps);
|
||||
mMapView.setMyLocationEnabled(false);
|
||||
mLocationFAB.setColorFilter(Color.TRANSPARENT);
|
||||
}
|
||||
}
|
||||
|
||||
private void enableGps() {
|
||||
mMapView.setMyLocationEnabled(true);
|
||||
mLocationFAB.setColorFilter(ContextCompat.getColor(this, R.color.primary));
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable / Disable Annotations.
|
||||
*
|
||||
@@ -364,7 +391,6 @@ public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private void addMarkers() {
|
||||
LatLng backLot = new LatLng(38.649441, -121.369064);
|
||||
MapView map = mMapView;
|
||||
mMapView.addMarker(new MarkerOptions()
|
||||
.position(backLot)
|
||||
.title("Back Lot")
|
||||
@@ -383,7 +409,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
String geojsonStr = Util.loadStringFromAssets(this, "small_line.geojson");
|
||||
LatLng[] latLngs = Util.parseGeoJSONCoordinates(geojsonStr);
|
||||
MapView map = mMapView;
|
||||
Polyline line = map.addPolyline(new PolylineOptions()
|
||||
map.addPolyline(new PolylineOptions()
|
||||
.add(latLngs)
|
||||
.width(2)
|
||||
.color(Color.RED));
|
||||
@@ -394,17 +420,16 @@ public class MainActivity extends AppCompatActivity {
|
||||
}
|
||||
|
||||
private void addPolygon() {
|
||||
String geojsonStr = null;
|
||||
try {
|
||||
geojsonStr = Util.loadStringFromAssets(this, "small_poly.geojson");
|
||||
String geojsonStr = Util.loadStringFromAssets(this, "small_poly.geojson");
|
||||
LatLng[] latLngs = Util.parseGeoJSONCoordinates(geojsonStr);
|
||||
MapView map = mMapView;
|
||||
ArrayList<PolygonOptions> opts = new ArrayList<PolygonOptions>();
|
||||
ArrayList<PolygonOptions> opts = new ArrayList<>();
|
||||
opts.add(new PolygonOptions()
|
||||
.add(latLngs)
|
||||
.strokeColor(Color.MAGENTA)
|
||||
.fillColor(Color.BLUE));
|
||||
Polygon polygon = map.addPolygons(opts).get(0);
|
||||
map.addPolygons(opts).get(0);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Error adding Polygon: "+ e);
|
||||
e.printStackTrace();
|
||||
@@ -415,76 +440,6 @@ public class MainActivity extends AppCompatActivity {
|
||||
mMapView.removeAnnotations();
|
||||
}
|
||||
|
||||
// This class handles outdoor class change events
|
||||
private class OutdoorClassSpinnerListener implements AdapterView.OnItemSelectedListener {
|
||||
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
ArrayList<String> styleClasses = new ArrayList<>(1);
|
||||
|
||||
switch (position) {
|
||||
// Day
|
||||
case 0:
|
||||
styleClasses.add("day");
|
||||
mMapView.setStyleClasses(styleClasses);
|
||||
break;
|
||||
|
||||
// Night
|
||||
case 1:
|
||||
styleClasses.add("night");
|
||||
mMapView.setStyleClasses(styleClasses);
|
||||
break;
|
||||
|
||||
default:
|
||||
onNothingSelected(parent);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
mMapView.removeAllStyleClasses();
|
||||
}
|
||||
}
|
||||
|
||||
// This class handles satellite class change events
|
||||
private class SatelliteClassSpinnerListener implements AdapterView.OnItemSelectedListener {
|
||||
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||
ArrayList<String> styleClasses = new ArrayList<>(2);
|
||||
|
||||
switch (position) {
|
||||
// Labels + Contours
|
||||
case 0:
|
||||
styleClasses.add("labels");
|
||||
styleClasses.add("contours");
|
||||
mMapView.setStyleClasses(styleClasses);
|
||||
break;
|
||||
|
||||
// Labels Only
|
||||
case 1:
|
||||
styleClasses.add("labels");
|
||||
mMapView.setStyleClasses(styleClasses);
|
||||
break;
|
||||
|
||||
// No Labels
|
||||
case 2:
|
||||
mMapView.setStyleClasses(styleClasses);
|
||||
break;
|
||||
|
||||
default:
|
||||
onNothingSelected(parent);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNothingSelected(AdapterView<?> parent) {
|
||||
mMapView.removeAllStyleClasses();
|
||||
}
|
||||
}
|
||||
|
||||
// Called when FPS changes
|
||||
private class MyOnFpsChangedListener implements MapView.OnFpsChangedListener {
|
||||
|
||||
|
||||
|
Before Width: | Height: | Size: 7.9 KiB |
|
Before Width: | Height: | Size: 683 B |
|
Before Width: | Height: | Size: 722 B |
|
Before Width: | Height: | Size: 641 B |
|
Before Width: | Height: | Size: 471 B |
|
After Width: | Height: | Size: 549 B |
|
Before Width: | Height: | Size: 546 B |
|
Before Width: | Height: | Size: 5.2 KiB |
|
Before Width: | Height: | Size: 6.0 KiB |
|
Before Width: | Height: | Size: 465 B |
|
Before Width: | Height: | Size: 484 B |
|
Before Width: | Height: | Size: 482 B |
|
Before Width: | Height: | Size: 323 B |
|
After Width: | Height: | Size: 341 B |
|
Before Width: | Height: | Size: 350 B |
|
Before Width: | Height: | Size: 10 KiB |
|
Before Width: | Height: | Size: 860 B |
|
Before Width: | Height: | Size: 994 B |
|
Before Width: | Height: | Size: 854 B |
|
Before Width: | Height: | Size: 622 B |
|
After Width: | Height: | Size: 660 B |
|
Before Width: | Height: | Size: 687 B |
|
Before Width: | Height: | Size: 14 KiB |
|
Before Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 1.4 KiB |
|
Before Width: | Height: | Size: 1.2 KiB |
|
Before Width: | Height: | Size: 918 B |
|
After Width: | Height: | Size: 976 B |
|
Before Width: | Height: | Size: 1012 B |
|
Before Width: | Height: | Size: 20 KiB |
|
Before Width: | Height: | Size: 1.2 KiB |
|
After Width: | Height: | Size: 1.3 KiB |
|
Before Width: | Height: | Size: 1.3 KiB |
@@ -47,13 +47,13 @@
|
||||
|
||||
<android.support.design.widget.FloatingActionButton
|
||||
android:id="@+id/locationFAB"
|
||||
android:src="@drawable/ic_my_location_white_24dp"
|
||||
android:src="@drawable/ic_my_location_black_24dp"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="end|bottom"
|
||||
android:layout_margin="@dimen/fab_margin"
|
||||
app:backgroundTint="@color/white"
|
||||
app:borderWidth="0dp"
|
||||
app:elevation="0dp"
|
||||
/>
|
||||
</android.support.design.widget.CoordinatorLayout>
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
<item
|
||||
android:id="@+id/action_debug"
|
||||
android:icon="@drawable/ic_action_about"
|
||||
android:icon="@android:drawable/ic_menu_info_details"
|
||||
android:title="@string/action_debug"
|
||||
android:checkable="true"
|
||||
/>
|
||||
@@ -14,7 +14,7 @@
|
||||
android:checkable="true"
|
||||
/>
|
||||
<item android:id="@+id/action_compass"
|
||||
android:icon="@drawable/ic_explore_white_24dp"
|
||||
android:icon="@android:drawable/ic_menu_compass"
|
||||
android:title="@string/action_compass"
|
||||
android:checkable="true"
|
||||
/>
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
<string name="app_name">Mapbox GL</string>
|
||||
|
||||
<string name="action_gps">Toggle GPS location</string>
|
||||
<string name="action_location_none">Toggle Location Bearing Tracking</string>
|
||||
<string name="action_location_follow">Follow</string>
|
||||
<string name="action_location_bearing">Bearing</string>
|
||||
<string name="action_compass">Toggle Compass</string>
|
||||
<string name="action_location_none">Disable tracking</string>
|
||||
<string name="action_location_follow">Use follow tracking mode</string>
|
||||
<string name="action_location_bearing">Use bearing tracking mode tracking</string>
|
||||
<string name="action_compass">Toggle compass</string>
|
||||
<string name="action_debug">Toggle debug mode</string>
|
||||
<string name="action_point_annotations">Toggle point annotations</string>
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
<item name="colorPrimary">@color/primary</item>
|
||||
<item name="colorPrimaryDark">@color/primaryDark</item>
|
||||
<item name="colorAccent">@color/accent</item>
|
||||
<item name="android:windowBackground">@color/white</item>
|
||||
</style>
|
||||
|
||||
</resources>
|
||||
|
||||