All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mapbox.mapboxsdk.maps.MapView Maven / Gradle / Ivy

There is a newer version: 9.2.1
Show newest version
package com.mapbox.mapboxsdk.maps;

import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.PointF;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.location.Location;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.CallSuper;
import android.support.annotation.FloatRange;
import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.ScaleGestureDetectorCompat;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.TextureView;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ZoomButtonsController;

import com.almeros.android.multitouch.gesturedetectors.RotateGestureDetector;
import com.almeros.android.multitouch.gesturedetectors.ShoveGestureDetector;
import com.almeros.android.multitouch.gesturedetectors.TwoFingerGestureDetector;
import com.mapbox.mapboxsdk.MapboxAccountManager;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.annotations.Annotation;
import com.mapbox.mapboxsdk.annotations.Icon;
import com.mapbox.mapboxsdk.annotations.IconFactory;
import com.mapbox.mapboxsdk.annotations.InfoWindow;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerView;
import com.mapbox.mapboxsdk.annotations.Polygon;
import com.mapbox.mapboxsdk.annotations.Polyline;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.constants.MyBearingTracking;
import com.mapbox.mapboxsdk.constants.MyLocationTracking;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.exceptions.IconBitmapChangedException;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.location.LocationListener;
import com.mapbox.mapboxsdk.location.LocationServices;
import com.mapbox.mapboxsdk.maps.widgets.CompassView;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationView;
import com.mapbox.mapboxsdk.maps.widgets.MyLocationViewSettings;
import com.mapbox.mapboxsdk.telemetry.MapboxEvent;
import com.mapbox.mapboxsdk.telemetry.MapboxEventManager;
import com.mapbox.mapboxsdk.utils.ColorUtils;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import static com.mapbox.mapboxsdk.utils.MathUtils.convertNativeBearing;

/**
 * 

* A {@code MapView} provides an embeddable map interface. * You use this class to display map information and to manipulate the map contents from your application. * You can center the map on a given coordinate, specify the size of the area you want to display, * and style the features of the map to fit your application's use case. *

*

* Use of {@code MapView} requires a Mapbox API access token. * Obtain an access token on the Mapbox account page. *

* Warning: Please note that you are responsible for getting permission to use the map data, * and for ensuring your use adheres to the relevant terms of use. */ public class MapView extends FrameLayout { private MapboxMap mapboxMap; private boolean initialLoad; private boolean destroyed; private List icons; private int averageIconHeight; private int averageIconWidth; private NativeMapView nativeMapView; private boolean hasSurface = false; private ViewGroup markerViewContainer; private CompassView compassView; private ImageView logoView; private ImageView attributionsView; private MyLocationView myLocationView; private LocationListener myLocationListener; private Projection projection; private CopyOnWriteArrayList onMapChangedListener; private ZoomButtonsController zoomButtonsController; private ConnectivityReceiver connectivityReceiver; private float screenDensity = 1.0f; private TrackballLongPressTimeOut currentTrackballLongPressTimeOut; private GestureDetectorCompat gestureDetector; private ScaleGestureDetector scaleGestureDetector; private RotateGestureDetector rotateGestureDetector; private ShoveGestureDetector shoveGestureDetector; private boolean twoTap = false; private boolean zoomStarted = false; private boolean dragStarted = false; private boolean quickZoom = false; private boolean scrollInProgress = false; private int contentPaddingLeft; private int contentPaddingTop; private int contentPaddingRight; private int contentPaddingBottom; private PointF focalPoint; private String styleUrl = Style.MAPBOX_STREETS; private boolean styleWasSet = false; private List onMapReadyCallbackList; private MapboxMap.CancelableCallback cameraCancelableCallback; private SnapshotRequest snapshotRequest; @UiThread public MapView(@NonNull Context context) { super(context); initialize(context, MapboxMapOptions.createFromAttributes(context, null)); } @UiThread public MapView(@NonNull Context context, @Nullable AttributeSet attrs) { super(context, attrs); initialize(context, MapboxMapOptions.createFromAttributes(context, attrs)); } @UiThread public MapView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initialize(context, MapboxMapOptions.createFromAttributes(context, attrs)); } @UiThread public MapView(@NonNull Context context, @Nullable MapboxMapOptions options) { super(context); initialize(context, options); } private void initialize(@NonNull Context context, @NonNull MapboxMapOptions options) { if (isInEditMode()) { // if we are in an editor mode we show an image of a map LayoutInflater.from(context).inflate(R.layout.mapview_preview, this); return; } initialLoad = true; onMapReadyCallbackList = new ArrayList<>(); onMapChangedListener = new CopyOnWriteArrayList<>(); mapboxMap = new MapboxMap(this); projection = mapboxMap.getProjection(); icons = new ArrayList<>(); View view = LayoutInflater.from(context).inflate(R.layout.mapview_internal, this); setWillNotDraw(false); if (options.getTextureMode()) { TextureView textureView = new TextureView(context); textureView.setSurfaceTextureListener(new SurfaceTextureListener()); addView(textureView, 0); } else { SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView); surfaceView.getHolder().addCallback(new SurfaceCallback()); surfaceView.setVisibility(View.VISIBLE); } nativeMapView = new NativeMapView(this); // load transparent icon for MarkerView to trace actual markers, see #6352 loadIcon(IconFactory.recreate(IconFactory.ICON_MARKERVIEW_ID, IconFactory.ICON_MARKERVIEW_BITMAP)); // Ensure this view is interactable setClickable(true); setLongClickable(true); setFocusable(true); setFocusableInTouchMode(true); requestFocus(); // Touch gesture detectors gestureDetector = new GestureDetectorCompat(context, new GestureListener()); gestureDetector.setIsLongpressEnabled(true); scaleGestureDetector = new ScaleGestureDetector(context, new ScaleGestureListener()); ScaleGestureDetectorCompat.setQuickScaleEnabled(scaleGestureDetector, true); rotateGestureDetector = new RotateGestureDetector(context, new RotateGestureListener()); shoveGestureDetector = new ShoveGestureDetector(context, new ShoveGestureListener()); zoomButtonsController = new ZoomButtonsController(this); zoomButtonsController.setZoomSpeed(MapboxConstants.ANIMATION_DURATION); zoomButtonsController.setOnZoomListener(new OnZoomListener()); // Connectivity onConnectivityChanged(isConnected()); markerViewContainer = (ViewGroup) view.findViewById(R.id.markerViewContainer); myLocationView = (MyLocationView) view.findViewById(R.id.userLocationView); myLocationView.setMapboxMap(mapboxMap); compassView = (CompassView) view.findViewById(R.id.compassView); compassView.setMapboxMap(mapboxMap); logoView = (ImageView) view.findViewById(R.id.logoView); // Setup Attributions control attributionsView = (ImageView) view.findViewById(R.id.attributionView); attributionsView.setOnClickListener(new AttributionOnClickListener(this)); screenDensity = context.getResources().getDisplayMetrics().density; setInitialState(options); // Shows the zoom controls if (!context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN_MULTITOUCH)) { mapboxMap.getUiSettings().setZoomControlsEnabled(true); } } private void setInitialState(MapboxMapOptions options) { mapboxMap.setDebugActive(options.getDebugActive()); CameraPosition position = options.getCamera(); if (position != null) { mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(position)); myLocationView.setTilt(position.tilt); } // api base url String apiBaseUrl = options.getApiBaseUrl(); if (!TextUtils.isEmpty(apiBaseUrl)) { setApiBaseUrl(apiBaseUrl); } // access token String accessToken = options.getAccessToken(); if (!TextUtils.isEmpty(accessToken)) { mapboxMap.setAccessToken(accessToken); } // style url String style = options.getStyle(); if (!TextUtils.isEmpty(style)) { styleUrl = style; } // MyLocationView MyLocationViewSettings myLocationViewSettings = mapboxMap.getMyLocationViewSettings(); myLocationViewSettings.setForegroundDrawable( options.getMyLocationForegroundDrawable(), options.getMyLocationForegroundBearingDrawable()); myLocationViewSettings.setForegroundTintColor(options.getMyLocationForegroundTintColor()); myLocationViewSettings.setBackgroundDrawable( options.getMyLocationBackgroundDrawable(), options.getMyLocationBackgroundPadding()); myLocationViewSettings.setBackgroundTintColor(options.getMyLocationBackgroundTintColor()); myLocationViewSettings.setAccuracyAlpha(options.getMyLocationAccuracyAlpha()); myLocationViewSettings.setAccuracyTintColor(options.getMyLocationAccuracyTintColor()); mapboxMap.setMyLocationEnabled(options.getLocationEnabled()); // Enable gestures UiSettings uiSettings = mapboxMap.getUiSettings(); uiSettings.setZoomGesturesEnabled(options.getZoomGesturesEnabled()); uiSettings.setZoomGestureChangeAllowed(options.getZoomGesturesEnabled()); uiSettings.setScrollGesturesEnabled(options.getScrollGesturesEnabled()); uiSettings.setScrollGestureChangeAllowed(options.getScrollGesturesEnabled()); uiSettings.setRotateGesturesEnabled(options.getRotateGesturesEnabled()); uiSettings.setRotateGestureChangeAllowed(options.getRotateGesturesEnabled()); uiSettings.setTiltGesturesEnabled(options.getTiltGesturesEnabled()); uiSettings.setTiltGestureChangeAllowed(options.getTiltGesturesEnabled()); // Ui Controls uiSettings.setZoomControlsEnabled(options.getZoomControlsEnabled()); // Zoom mapboxMap.setMaxZoom(options.getMaxZoom()); mapboxMap.setMinZoom(options.getMinZoom()); // Compass uiSettings.setCompassEnabled(options.getCompassEnabled()); uiSettings.setCompassGravity(options.getCompassGravity()); int[] compassMargins = options.getCompassMargins(); if (compassMargins != null) { uiSettings.setCompassMargins(compassMargins[0], compassMargins[1], compassMargins[2], compassMargins[3]); } else { int tenDp = (int) getResources().getDimension(R.dimen.ten_dp); uiSettings.setCompassMargins(tenDp, tenDp, tenDp, tenDp); } uiSettings.setCompassFadeFacingNorth(options.getCompassFadeFacingNorth()); // Logo uiSettings.setLogoEnabled(options.getLogoEnabled()); uiSettings.setLogoGravity(options.getLogoGravity()); int[] logoMargins = options.getLogoMargins(); if (logoMargins != null) { uiSettings.setLogoMargins(logoMargins[0], logoMargins[1], logoMargins[2], logoMargins[3]); } else { int sixteenDp = (int) getResources().getDimension(R.dimen.sixteen_dp); uiSettings.setLogoMargins(sixteenDp, sixteenDp, sixteenDp, sixteenDp); } // Attribution uiSettings.setAttributionEnabled(options.getAttributionEnabled()); uiSettings.setAttributionGravity(options.getAttributionGravity()); int[] attributionMargins = options.getAttributionMargins(); if (attributionMargins != null) { uiSettings.setAttributionMargins(attributionMargins[0], attributionMargins[1], attributionMargins[2], attributionMargins[3]); } else { Resources resources = getResources(); int sevenDp = (int) resources.getDimension(R.dimen.seven_dp); int seventySixDp = (int) resources.getDimension(R.dimen.seventy_six_dp); uiSettings.setAttributionMargins(seventySixDp, sevenDp, sevenDp, sevenDp); } int attributionTintColor = options.getAttributionTintColor(); uiSettings.setAttributionTintColor(attributionTintColor != -1 ? attributionTintColor : ColorUtils.getPrimaryColor(getContext())); } // // Lifecycle events // /** *

* You must call this method from the parent's {@link android.app.Activity#onCreate(Bundle)} or * {@link android.app.Fragment#onCreate(Bundle)}. *

* You must set a valid access token with {@link MapView#setAccessToken(String)} before you this method * or an exception will be thrown. * * @param savedInstanceState Pass in the parent's savedInstanceState. * @see MapView#setAccessToken(String) */ @UiThread public void onCreate(@Nullable Bundle savedInstanceState) { String accessToken = mapboxMap.getAccessToken(); if (TextUtils.isEmpty(accessToken)) { accessToken = MapboxAccountManager.getInstance().getAccessToken(); mapboxMap.setAccessToken(accessToken); } else { // user provided access token through xml attributes, need to start MapboxAccountManager MapboxAccountManager.start(getContext(), accessToken); } // Force a check for an access token MapboxAccountManager.validateAccessToken(accessToken); if (savedInstanceState != null && savedInstanceState.getBoolean(MapboxConstants.STATE_HAS_SAVED_STATE)) { // Get previous camera position CameraPosition cameraPosition = savedInstanceState.getParcelable(MapboxConstants.STATE_CAMERA_POSITION); if (cameraPosition != null) { mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder(cameraPosition).build())); } UiSettings uiSettings = mapboxMap.getUiSettings(); uiSettings.setZoomGesturesEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_ZOOM_ENABLED)); uiSettings.setZoomGestureChangeAllowed(savedInstanceState.getBoolean(MapboxConstants.STATE_ZOOM_ENABLED_CHANGE)); uiSettings.setScrollGesturesEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_SCROLL_ENABLED)); uiSettings.setScrollGestureChangeAllowed(savedInstanceState.getBoolean(MapboxConstants.STATE_SCROLL_ENABLED_CHANGE)); uiSettings.setRotateGesturesEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_ROTATE_ENABLED)); uiSettings.setRotateGestureChangeAllowed(savedInstanceState.getBoolean(MapboxConstants.STATE_ROTATE_ENABLED_CHANGE)); uiSettings.setTiltGesturesEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_TILT_ENABLED)); uiSettings.setTiltGestureChangeAllowed(savedInstanceState.getBoolean(MapboxConstants.STATE_TILT_ENABLED_CHANGE)); uiSettings.setZoomControlsEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_ZOOM_CONTROLS_ENABLED)); // Compass uiSettings.setCompassEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_COMPASS_ENABLED)); uiSettings.setCompassGravity(savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_GRAVITY)); uiSettings.setCompassMargins(savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_MARGIN_LEFT), savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_MARGIN_TOP), savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_MARGIN_RIGHT), savedInstanceState.getInt(MapboxConstants.STATE_COMPASS_MARGIN_BOTTOM)); uiSettings.setCompassFadeFacingNorth(savedInstanceState.getBoolean(MapboxConstants.STATE_COMPASS_FADE_WHEN_FACING_NORTH)); // Logo uiSettings.setLogoEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_LOGO_ENABLED)); uiSettings.setLogoGravity(savedInstanceState.getInt(MapboxConstants.STATE_LOGO_GRAVITY)); uiSettings.setLogoMargins(savedInstanceState.getInt(MapboxConstants.STATE_LOGO_MARGIN_LEFT) , savedInstanceState.getInt(MapboxConstants.STATE_LOGO_MARGIN_TOP) , savedInstanceState.getInt(MapboxConstants.STATE_LOGO_MARGIN_RIGHT) , savedInstanceState.getInt(MapboxConstants.STATE_LOGO_MARGIN_BOTTOM)); // Attribution uiSettings.setAttributionEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_ATTRIBUTION_ENABLED)); uiSettings.setAttributionGravity(savedInstanceState.getInt(MapboxConstants.STATE_ATTRIBUTION_GRAVITY)); uiSettings.setAttributionMargins(savedInstanceState.getInt(MapboxConstants.STATE_ATTRIBUTION_MARGIN_LEFT) , savedInstanceState.getInt(MapboxConstants.STATE_ATTRIBUTION_MARGIN_TOP) , savedInstanceState.getInt(MapboxConstants.STATE_ATTRIBUTION_MARGIN_RIGHT) , savedInstanceState.getInt(MapboxConstants.STATE_ATTRIBUTION_MARGIN_BOTTOM)); mapboxMap.setDebugActive(savedInstanceState.getBoolean(MapboxConstants.STATE_DEBUG_ACTIVE)); styleUrl = savedInstanceState.getString(MapboxConstants.STATE_STYLE_URL); // User location try { mapboxMap.setMyLocationEnabled(savedInstanceState.getBoolean(MapboxConstants.STATE_MY_LOCATION_ENABLED)); } catch (SecurityException ignore) { // User did not accept location permissions } TrackingSettings trackingSettings = mapboxMap.getTrackingSettings(); //noinspection ResourceType trackingSettings.setMyLocationTrackingMode( savedInstanceState.getInt(MapboxConstants.STATE_MY_LOCATION_TRACKING_MODE, MyLocationTracking.TRACKING_NONE)); //noinspection ResourceType trackingSettings.setMyBearingTrackingMode( savedInstanceState.getInt(MapboxConstants.STATE_MY_BEARING_TRACKING_MODE, MyBearingTracking.NONE)); trackingSettings.setDismissLocationTrackingOnGesture( savedInstanceState.getBoolean(MapboxConstants.STATE_MY_LOCATION_TRACKING_DISMISS, true)); trackingSettings.setDismissBearingTrackingOnGesture( savedInstanceState.getBoolean(MapboxConstants.STATE_MY_BEARING_TRACKING_DISMISS, true)); } else if (savedInstanceState == null) { // Start Telemetry (authorization determined in initial MapboxEventManager constructor) Log.i(MapView.class.getCanonicalName(), "MapView start Telemetry..."); MapboxEventManager eventManager = MapboxEventManager.getMapboxEventManager(); eventManager.initialize(getContext(), getAccessToken()); } // Initialize EGL nativeMapView.initializeDisplay(); nativeMapView.initializeContext(); // Add annotation deselection listener addOnMapChangedListener(new OnMapChangedListener() { @Override public void onMapChanged(@MapChange int change) { if (change == DID_FINISH_LOADING_STYLE && initialLoad) { initialLoad = false; reloadIcons(); reloadMarkers(); adjustTopOffsetPixels(); // Notify listeners the map is ready if (onMapReadyCallbackList.size() > 0) { Iterator iterator = onMapReadyCallbackList.iterator(); while (iterator.hasNext()) { OnMapReadyCallback callback = iterator.next(); callback.onMapReady(mapboxMap); iterator.remove(); } } // invalidate camera to update overlain views with correct tilt value invalidateCameraPosition(); } else if (change == REGION_IS_CHANGING || change == REGION_DID_CHANGE || change == DID_FINISH_LOADING_MAP) { mapboxMap.getMarkerViewManager().scheduleViewMarkerInvalidation(); compassView.update(getDirection()); myLocationView.update(); mapboxMap.getMarkerViewManager().update(); for (InfoWindow infoWindow : mapboxMap.getInfoWindows()) { infoWindow.update(); } } } }); // Fire MapLoad if (savedInstanceState == null) { Hashtable evt = new Hashtable<>(); evt.put(MapboxEvent.ATTRIBUTE_EVENT, MapboxEvent.TYPE_MAP_LOAD); evt.put(MapboxEvent.ATTRIBUTE_CREATED, MapboxEventManager.generateCreateDate()); MapboxEventManager.getMapboxEventManager().pushEvent(evt); } } /** * You must call this method from the parent's {@link android.app.Activity#onSaveInstanceState(Bundle)} * or {@link android.app.Fragment#onSaveInstanceState(Bundle)}. * * @param outState Pass in the parent's outState. */ @UiThread public void onSaveInstanceState(@NonNull Bundle outState) { outState.putBoolean(MapboxConstants.STATE_HAS_SAVED_STATE, true); outState.putParcelable(MapboxConstants.STATE_CAMERA_POSITION, mapboxMap.getCameraPosition()); outState.putBoolean(MapboxConstants.STATE_DEBUG_ACTIVE, mapboxMap.isDebugActive()); outState.putString(MapboxConstants.STATE_STYLE_URL, styleUrl); outState.putBoolean(MapboxConstants.STATE_MY_LOCATION_ENABLED, mapboxMap.isMyLocationEnabled()); // TrackingSettings TrackingSettings trackingSettings = mapboxMap.getTrackingSettings(); outState.putInt(MapboxConstants.STATE_MY_LOCATION_TRACKING_MODE, trackingSettings.getMyLocationTrackingMode()); outState.putInt(MapboxConstants.STATE_MY_BEARING_TRACKING_MODE, trackingSettings.getMyBearingTrackingMode()); outState.putBoolean(MapboxConstants.STATE_MY_LOCATION_TRACKING_DISMISS, trackingSettings.isDismissLocationTrackingOnGesture()); outState.putBoolean(MapboxConstants.STATE_MY_BEARING_TRACKING_DISMISS, trackingSettings.isDismissBearingTrackingOnGesture()); // UiSettings UiSettings uiSettings = mapboxMap.getUiSettings(); outState.putBoolean(MapboxConstants.STATE_ZOOM_ENABLED, uiSettings.isZoomGesturesEnabled()); outState.putBoolean(MapboxConstants.STATE_ZOOM_ENABLED_CHANGE, uiSettings.isZoomGestureChangeAllowed()); outState.putBoolean(MapboxConstants.STATE_SCROLL_ENABLED, uiSettings.isScrollGesturesEnabled()); outState.putBoolean(MapboxConstants.STATE_SCROLL_ENABLED_CHANGE, uiSettings.isScrollGestureChangeAllowed()); outState.putBoolean(MapboxConstants.STATE_ROTATE_ENABLED, uiSettings.isRotateGesturesEnabled()); outState.putBoolean(MapboxConstants.STATE_ROTATE_ENABLED_CHANGE, uiSettings.isRotateGestureChangeAllowed()); outState.putBoolean(MapboxConstants.STATE_TILT_ENABLED, uiSettings.isTiltGesturesEnabled()); outState.putBoolean(MapboxConstants.STATE_TILT_ENABLED_CHANGE, uiSettings.isTiltGestureChangeAllowed()); outState.putBoolean(MapboxConstants.STATE_ZOOM_CONTROLS_ENABLED, uiSettings.isZoomControlsEnabled()); // UiSettings - Compass outState.putBoolean(MapboxConstants.STATE_COMPASS_ENABLED, uiSettings.isCompassEnabled()); outState.putInt(MapboxConstants.STATE_COMPASS_GRAVITY, uiSettings.getCompassGravity()); outState.putInt(MapboxConstants.STATE_COMPASS_MARGIN_LEFT, uiSettings.getCompassMarginLeft()); outState.putInt(MapboxConstants.STATE_COMPASS_MARGIN_TOP, uiSettings.getCompassMarginTop()); outState.putInt(MapboxConstants.STATE_COMPASS_MARGIN_BOTTOM, uiSettings.getCompassMarginBottom()); outState.putInt(MapboxConstants.STATE_COMPASS_MARGIN_RIGHT, uiSettings.getCompassMarginRight()); outState.putBoolean(MapboxConstants.STATE_COMPASS_FADE_WHEN_FACING_NORTH, uiSettings.isCompassFadeWhenFacingNorth()); // UiSettings - Logo outState.putInt(MapboxConstants.STATE_LOGO_GRAVITY, uiSettings.getLogoGravity()); outState.putInt(MapboxConstants.STATE_LOGO_MARGIN_LEFT, uiSettings.getLogoMarginLeft()); outState.putInt(MapboxConstants.STATE_LOGO_MARGIN_TOP, uiSettings.getLogoMarginTop()); outState.putInt(MapboxConstants.STATE_LOGO_MARGIN_RIGHT, uiSettings.getLogoMarginRight()); outState.putInt(MapboxConstants.STATE_LOGO_MARGIN_BOTTOM, uiSettings.getLogoMarginBottom()); outState.putBoolean(MapboxConstants.STATE_LOGO_ENABLED, uiSettings.isLogoEnabled()); // UiSettings - Attribution outState.putInt(MapboxConstants.STATE_ATTRIBUTION_GRAVITY, uiSettings.getAttributionGravity()); outState.putInt(MapboxConstants.STATE_ATTRIBUTION_MARGIN_LEFT, uiSettings.getAttributionMarginLeft()); outState.putInt(MapboxConstants.STATE_ATTRIBUTION_MARGIN_TOP, uiSettings.getAttributionMarginTop()); outState.putInt(MapboxConstants.STATE_ATTRIBUTION_MARGIN_RIGHT, uiSettings.getAttributionMarginRight()); outState.putInt(MapboxConstants.STATE_ATTRIBUTION_MARGIN_BOTTOM, uiSettings.getAttributionMarginBottom()); outState.putBoolean(MapboxConstants.STATE_ATTRIBUTION_ENABLED, uiSettings.isAttributionEnabled()); } /** * You must call this method from the parent's {@link Activity#onDestroy()} or {@link Fragment#onDestroy()}. */ @UiThread public void onDestroy() { destroyed = true; nativeMapView.terminateContext(); nativeMapView.terminateDisplay(); nativeMapView.destroySurface(); nativeMapView.destroy(); nativeMapView = null; } /** * You must call this method from the parent's {@link Activity#onPause()} or {@link Fragment#onPause()}. */ @UiThread public void onPause() { // Unregister for connectivity changes if (connectivityReceiver != null) { getContext().unregisterReceiver(connectivityReceiver); connectivityReceiver = null; } myLocationView.onPause(); } /** * You must call this method from the parent's {@link Activity#onResume()} or {@link Fragment#onResume()}. */ @UiThread public void onResume() { // Register for connectivity changes connectivityReceiver = new ConnectivityReceiver(); getContext().registerReceiver(connectivityReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); nativeMapView.update(); myLocationView.onResume(); // In case that no style was set or was loaded through MapboxMapOptions if (!styleWasSet) { setStyleUrl(styleUrl); } } void setFocalPoint(PointF focalPoint) { if (focalPoint == null) { // resetting focal point, UiSettings uiSettings = mapboxMap.getUiSettings(); // need to validate if we need to reset focal point with user provided one if (uiSettings.getFocalPoint() != null) { focalPoint = uiSettings.getFocalPoint(); } } this.focalPoint = focalPoint; } /** * You must call this method from the parent's {@link Activity#onLowMemory()} or {@link Fragment#onLowMemory()}. */ @UiThread public void onLowMemory() { nativeMapView.onLowMemory(); } // Called when debug mode is enabled to update a FPS counter // Called via JNI from NativeMapView // Forward to any listener protected void onFpsChanged(final double fps) { post(new Runnable() { @Override public void run() { MapboxMap.OnFpsChangedListener listener = mapboxMap.getOnFpsChangedListener(); if (listener != null) { listener.onFpsChanged(fps); } } }); } // // LatLng / CenterCoordinate // LatLng getLatLng() { return nativeMapView.getLatLng(); } // // Pitch / Tilt // double getTilt() { return nativeMapView.getPitch(); } void setTilt(Double pitch) { mapboxMap.getMarkerViewManager().setTilt(pitch.floatValue()); myLocationView.setTilt(pitch); nativeMapView.setPitch(pitch, 0); } // // Direction // double getDirection() { if (destroyed) { return 0; } return convertNativeBearing(nativeMapView.getBearing()); } void setDirection(@FloatRange(from = MapboxConstants.MINIMUM_DIRECTION, to = MapboxConstants.MAXIMUM_DIRECTION) double direction) { if (destroyed) { return; } setDirection(direction, false); } void setDirection(@FloatRange(from = MapboxConstants.MINIMUM_DIRECTION, to = MapboxConstants.MAXIMUM_DIRECTION) double direction, boolean animated) { if (destroyed) { return; } long duration = animated ? MapboxConstants.ANIMATION_DURATION : 0; cancelTransitions(); // Out of range directions are normalised in setBearing nativeMapView.setBearing(-direction, duration); } void resetNorth() { if (destroyed) { return; } myLocationView.setBearing(0); cancelTransitions(); nativeMapView.resetNorth(); } // // Content padding // int getContentPaddingLeft() { return contentPaddingLeft; } int getContentPaddingTop() { return contentPaddingTop; } int getContentPaddingRight() { return contentPaddingRight; } int getContentPaddingBottom() { return contentPaddingBottom; } int getContentWidth() { return getWidth() - contentPaddingLeft - contentPaddingRight; } int getContentHeight() { return getHeight() - contentPaddingBottom - contentPaddingTop; } // // Zoom // double getZoom() { if (destroyed) { return 0; } return nativeMapView.getZoom(); } void setMinZoom(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM, to = MapboxConstants.MAXIMUM_ZOOM) double minZoom) { if (destroyed) { return; } nativeMapView.setMinZoom(minZoom); } double getMinZoom() { if (destroyed) { return 0; } return nativeMapView.getMinZoom(); } void setMaxZoom(@FloatRange(from = MapboxConstants.MINIMUM_ZOOM, to = MapboxConstants.MAXIMUM_ZOOM) double maxZoom) { if (destroyed) { return; } nativeMapView.setMaxZoom(maxZoom); } double getMaxZoom() { if (destroyed) { return 0; } return nativeMapView.getMaxZoom(); } // Zoom in or out private void zoom(boolean zoomIn) { zoom(zoomIn, -1.0f, -1.0f); } private void zoom(boolean zoomIn, float x, float y) { // Cancel any animation cancelTransitions(); if (zoomIn) { nativeMapView.scaleBy(2.0, x / screenDensity, y / screenDensity, MapboxConstants.ANIMATION_DURATION); } else { nativeMapView.scaleBy(0.5, x / screenDensity, y / screenDensity, MapboxConstants.ANIMATION_DURATION); } // work around to invalidate camera position postDelayed(new ZoomInvalidator(mapboxMap), MapboxConstants.ANIMATION_DURATION); } // // Debug // boolean isDebugActive() { if (destroyed) { return false; } return nativeMapView.getDebug(); } void setDebugActive(boolean debugActive) { if (destroyed) { return; } nativeMapView.setDebug(debugActive); } void cycleDebugOptions() { if (destroyed) { return; } nativeMapView.cycleDebugOptions(); } // // Styling // /** *

* Loads a new map style from the specified URL. *

* {@code url} can take the following forms: *
    *
  • {@code Style.*}: load one of the bundled styles in {@link Style}.
  • *
  • {@code mapbox://styles//