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

Commit

Permalink
[android] #4094 - bring back view package
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Feb 24, 2016
1 parent 896a62e commit 1b5190a
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.support.annotation.IntDef;

import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.UserLocationView;
import com.mapbox.mapboxsdk.maps.widgets.UserLocationView;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import android.support.annotation.IntDef;

import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.UserLocationView;
import com.mapbox.mapboxsdk.maps.widgets.UserLocationView;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.layers.CustomLayer;
import com.mapbox.mapboxsdk.maps.widgets.CompassView;
import com.mapbox.mapboxsdk.maps.widgets.UserLocationView;
import com.mapbox.mapboxsdk.telemetry.MapboxEvent;
import com.mapbox.mapboxsdk.telemetry.MapboxEventManager;
import com.mapbox.mapboxsdk.utils.ApiAccess;
Expand Down Expand Up @@ -212,9 +214,9 @@ private void initialize(@NonNull Context context, @Nullable AttributeSet attrs)
onConnectivityChanged(isConnected());

mUserLocationView = (UserLocationView) view.findViewById(R.id.userLocationView);
mUserLocationView.setMapView(this);
mUserLocationView.setMapboxMap(mMapboxMap);
mCompassView = (CompassView) view.findViewById(R.id.compassView);
mCompassView.setOnClickListener(new CompassView.CompassClickListener(this));
mCompassView.setOnClickListener(new CompassView.CompassClickListener(mMapboxMap));
mLogoView = (ImageView) view.findViewById(R.id.logoView);

// Setup Attributions control
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ private void invalidateCameraPosition() {
}
}

//
// Reset North
//

/**
*
*/
public void resetNorth() {
mMapView.resetNorth();
}

//
// Manual zoom controls
//
Expand Down Expand Up @@ -1039,6 +1050,17 @@ public void setPadding(int left, int top, int right, int bottom) {
moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder(mCameraPosition).build()));
}

/**
*
* @return
*/
public int[] getPadding() {
return new int[]{mMapView.getContentPaddingLeft(),
mMapView.getContentPaddingTop(),
mMapView.getContentPaddingRight(),
mMapView.getContentPaddingBottom()};
}

//
// Map events
//
Expand Down Expand Up @@ -1314,6 +1336,14 @@ MapView getMapView() {
return mMapView;
}

//
// Invalidate
//

public void invalidate(){
mMapView.update();
}

//
// Interfaces
//
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.mapbox.mapboxsdk.maps;

import android.graphics.PointF;
import android.graphics.RectF;
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;

import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.geometry.LatLngBounds;
import com.mapbox.mapboxsdk.geometry.VisibleRegion;
import com.mapbox.mapboxsdk.utils.MathUtils;

/**
* A projection is used to translate between on screen location and geographic coordinates on
Expand All @@ -22,6 +21,20 @@ public class Projection {
this.mMapView = mapView;
}

/**
* <p>
* Returns the distance spanned by one pixel at the specified latitude and current zoom level.
* </p>
* The distance between pixels decreases as the latitude approaches the poles.
* This relationship parallels the relationship between longitudinal coordinates at different latitudes.
*
* @param latitude The latitude for which to return the value.
* @return The distance measured in meters.
*/
public double getMetersPerPixelAtLatitude(@FloatRange(from = -180, to = 180) double latitude) {
return mMapView.getMetersPerPixelAtLatitude(latitude);
}

/**
* Returns the geographic location that corresponds to a screen location.
* The screen location is specified in screen pixels (not display pixels) relative to the
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mapbox.mapboxsdk.maps;
package com.mapbox.mapboxsdk.maps.widgets;

import android.content.Context;
import android.support.v4.content.ContextCompat;
Expand All @@ -11,6 +11,8 @@
import android.widget.ImageView;

import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;

import java.lang.ref.WeakReference;
import java.util.Timer;
Expand All @@ -21,7 +23,7 @@
* when it isn't true north (0.0). Tapping the compass resets the bearing to true
* north and hides the compass.
*/
public class CompassView extends ImageView {
public final class CompassView extends ImageView {

private Timer mNorthTimer;
private double mDirection = 0.0f;
Expand Down Expand Up @@ -139,17 +141,17 @@ public void onAnimationEnd(View view) {

public static class CompassClickListener implements View.OnClickListener {

private WeakReference<MapView> mMapView;
private WeakReference<MapboxMap> mMapboxMap;

public CompassClickListener(final MapView mapView) {
mMapView = new WeakReference<>(mapView);
public CompassClickListener(final MapboxMap mapboxMap) {
mMapboxMap = new WeakReference<>(mapboxMap);
}

@Override
public void onClick(View v) {
final MapView mapView = mMapView.get();
if (mapView != null) {
mapView.resetNorth();
final MapboxMap mapboxMap = mMapboxMap.get();
if (mapboxMap != null) {
mapboxMap.resetNorth();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.mapbox.mapboxsdk.maps;
package com.mapbox.mapboxsdk.maps.widgets;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
Expand Down Expand Up @@ -33,6 +33,8 @@
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.location.LocationListener;
import com.mapbox.mapboxsdk.location.LocationService;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.Projection;

import java.lang.ref.WeakReference;

Expand All @@ -43,9 +45,8 @@

public final class UserLocationView extends View {

private MapView mMapView;

private float mDensity;
private MapboxMap mMapboxMap;
private Projection mProjection;

private boolean mShowMarker;
private boolean mShowDirection;
Expand Down Expand Up @@ -97,7 +98,6 @@ public final class UserLocationView extends View {
@MyBearingTracking.Mode
private int mMyBearingTrackingMode;


// Compass data
private MyBearingListener mBearingChangeListener;

Expand Down Expand Up @@ -132,9 +132,9 @@ private void initialize(Context context) {

// Setup the custom paint
Resources resources = context.getResources();
int accuracyColor = resources.getColor(R.color.my_location_ring);
int accuracyColor = ContextCompat.getColor(context,R.color.my_location_ring);

mDensity = resources.getDisplayMetrics().density;
float density = resources.getDisplayMetrics().density;
mMarkerCoordinate = new LatLng(0.0, 0.0);
mMarkerScreenPoint = new PointF();
mMarkerScreenMatrix = new Matrix();
Expand All @@ -148,7 +148,7 @@ private void initialize(Context context) {
mAccuracyPaintStroke = new Paint();
mAccuracyPaintStroke.setAntiAlias(true);
mAccuracyPaintStroke.setStyle(Paint.Style.STROKE);
mAccuracyPaintStroke.setStrokeWidth(0.5f * mDensity);
mAccuracyPaintStroke.setStrokeWidth(0.5f * density);
mAccuracyPaintStroke.setColor(accuracyColor);
mAccuracyPaintStroke.setAlpha((int) (255 * 0.5f));

Expand Down Expand Up @@ -195,8 +195,9 @@ private void initialize(Context context) {
mUserLocationStaleDrawable.setBounds(mUserLocationStaleDrawableBounds);
}

public void setMapView(MapView mapView) {
mMapView = mapView;
public void setMapboxMap(MapboxMap mapboxMap) {
mMapboxMap = mapboxMap;
mProjection = mapboxMap.getProjection();
}

public void onStart() {
Expand Down Expand Up @@ -247,7 +248,7 @@ public void setMyLocationTrackingMode(@MyLocationTracking.Mode int myLocationTra
if (myLocationTrackingMode != MyLocationTracking.TRACKING_NONE && mUserLocation != null) {
// center map directly if we have a location fix
mMarkerCoordinate = new LatLng(mUserLocation.getLatitude(), mUserLocation.getLongitude());
mMapView.getMapboxMap().moveCamera(CameraUpdateFactory.newLatLng(new LatLng(mUserLocation)));
mMapboxMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(mUserLocation)));

// center view directly
mMarkerScreenMatrix.reset();
Expand Down Expand Up @@ -283,11 +284,11 @@ public void update() {
if (mShowDirection) {
bearing = mMyBearingTrackingMode == MyBearingTracking.COMPASS ? mBearingChangeListener.getCompassBearing() : mUserLocation.getBearing();
} else {
bearing = (float) mMapView.getBearing();
bearing = mMapboxMap.getCameraPosition().bearing;
}

if (mCurrentMapViewCoordinate == null) {
mCurrentMapViewCoordinate = mMapView.getMapboxMap().getCameraPosition().target;
mCurrentMapViewCoordinate = mMapboxMap.getCameraPosition().target;
}

// only update if there is an actual change
Expand All @@ -296,7 +297,7 @@ public void update() {
.target(mMarkerCoordinate)
.bearing(bearing)
.build();
mMapView.getMapboxMap().animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 300, null);
mMapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 300, null);
mMarkerScreenMatrix.reset();
mMarkerScreenPoint = getMarkerScreenPoint();
mMarkerScreenMatrix.setTranslate(mMarkerScreenPoint.x, mMarkerScreenPoint.y);
Expand All @@ -310,10 +311,10 @@ public void update() {
// rotate so arrow in points to bearing
if (mShowDirection) {
if (mMyBearingTrackingMode == MyBearingTracking.COMPASS && mMyLocationTrackingMode == MyLocationTracking.TRACKING_NONE) {
mMarkerScreenMatrix.preRotate(mCompassMarkerDirection + (float) mMapView.getDirection());
mMarkerScreenMatrix.preRotate(mCompassMarkerDirection + mMapboxMap.getCameraPosition().bearing);
} else if (mMyBearingTrackingMode == MyBearingTracking.GPS) {
if (mMyLocationTrackingMode == MyLocationTracking.TRACKING_NONE) {
mMarkerScreenMatrix.preRotate(mGpsMarkerDirection + (float) mMapView.getDirection());
mMarkerScreenMatrix.preRotate(mGpsMarkerDirection + mMapboxMap.getCameraPosition().bearing);
} else {
mMarkerScreenMatrix.preRotate(mGpsMarkerDirection);
}
Expand All @@ -324,7 +325,7 @@ public void update() {
if (mShowAccuracy && !mStaleMarker) {
mAccuracyPath.reset();
mAccuracyPath.addCircle(0.0f, 0.0f,
(float) (mMarkerAccuracy / mMapView.getMetersPerPixelAtLatitude(
(float) (mMarkerAccuracy / mMapboxMap.getProjection().getMetersPerPixelAtLatitude(
mMarkerCoordinate.getLatitude())),
Path.Direction.CW);

Expand Down Expand Up @@ -471,9 +472,8 @@ public void onSensorChanged(SensorEvent event) {
SensorManager.getRotationMatrix(mR, null, mLastAccelerometer, mLastMagnetometer);
SensorManager.getOrientation(mR, mOrientation);
float azimuthInRadians = mOrientation[0];
float azimuthInDegress = (float) (Math.toDegrees(azimuthInRadians) + 360) % 360;

mCompassBearing = azimuthInDegress;
mCompassBearing = (float) (Math.toDegrees(azimuthInRadians) + 360) % 360;
if (mCompassBearing < 0) {
// only allow positive degrees
mCompassBearing += 360;
Expand Down Expand Up @@ -658,7 +658,7 @@ private void setCompass(float bearing) {
}

void updateOnNextFrame() {
mMapView.update();
mMapboxMap.invalidate();
}

/**
Expand Down Expand Up @@ -760,10 +760,11 @@ public boolean isPaused() {

public PointF getMarkerScreenPoint() {
if (mMyLocationTrackingMode == MyLocationTracking.TRACKING_NONE) {
mMarkerScreenPoint = mMapView.toScreenLocation(mMarkerCoordinate);
mMarkerScreenPoint = mProjection.toScreenLocation(mMarkerCoordinate);
} else {
mMarkerScreenPoint = new PointF(((getMeasuredWidth() + mMapView.getContentPaddingLeft() - mMapView.getContentPaddingRight()) / 2)
, ((getMeasuredHeight() - mMapView.getContentPaddingBottom() + mMapView.getContentPaddingTop()) / 2));
int[] contentPadding = mMapboxMap.getPadding();
mMarkerScreenPoint = new PointF(((getMeasuredWidth() + contentPadding[0] - contentPadding[2]) / 2)
, ((getMeasuredHeight() - contentPadding[3] + contentPadding[1]) / 2));
}
return mMarkerScreenPoint;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent" />

<com.mapbox.mapboxsdk.maps.CompassView
<com.mapbox.mapboxsdk.maps.widgets.CompassView
android:id="@+id/compassView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Expand All @@ -29,7 +29,7 @@
android:src="@drawable/ic_info_outline_24dp_selector"
android:background="@drawable/bg_default_selector"/>

<com.mapbox.mapboxsdk.maps.UserLocationView
<com.mapbox.mapboxsdk.maps.widgets.UserLocationView
android:id="@+id/userLocationView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Expand Down

0 comments on commit 1b5190a

Please sign in to comment.