From 4f6f4bcd6bf26f6a81ba8725ef2d724d46a68284 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Tue, 4 Aug 2015 18:47:25 -0500 Subject: [PATCH] #1940 - DRYing out GPS Marker setup. Confirming that On / Off toggle and setup is done in correct order. --- .../com/mapbox/mapboxgl/views/MapView.java | 53 ++++++++++++------- .../mapbox/mapboxgl/testapp/MainActivity.java | 1 + 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java index de4c7b92187..f4e1bedd680 100644 --- a/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java +++ b/android/java/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxgl/views/MapView.java @@ -244,12 +244,6 @@ private void initialize(Context context, AttributeSet attrs) { onConnectivityChanged(isConnected); } - // Setup User Location UI - mGpsMarker = new ImageView(getContext()); -// mGpsMarker.setVisibility(View.INVISIBLE); - mGpsMarker.setImageResource(R.drawable.location_marker); - mGpsMarker.setLayoutParams(new FrameLayout.LayoutParams((int) (27.0f * mScreenDensity), (int) (27.0f * mScreenDensity))); - addView(mGpsMarker); // Setup Location Services mLocationClient = new LostApiClient.Builder(getContext()).build(); @@ -257,6 +251,16 @@ private void initialize(Context context, AttributeSet attrs) { .setInterval(1000) .setSmallestDisplacement(1) .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); + + setOnMapChangedListener(new OnMapChangedListener() { + @Override + public void onMapChanged() { + // TODO - doesn't seem to be firing like it does client side + + Log.i(TAG, "onMapChanged!"); + updateMap(); + } + }); } // @@ -1425,11 +1429,12 @@ private void toggleGps(boolean enableGps) { } else { if (mIsGpsOn) { mIsGpsOn = false; -// LocationServices.FusedLocationApi.removeLocationUpdates(this); + LocationServices.FusedLocationApi.removeLocationUpdates(this); mLocationClient.disconnect(); mGpsLocation = null; } } + updateMap(); } /** @@ -1438,7 +1443,7 @@ private void toggleGps(boolean enableGps) { */ @Override public void onLocationChanged(Location location) { - Log.i(TAG, "onLocationChanged: " + location); + Log.i(TAG, "onLocationChanged(): " + location); updateLocation(location); } @@ -1446,20 +1451,37 @@ public void onLocationChanged(Location location) { private void updateLocation(Location location) { if (location != null) { mGpsLocation = location; + updateMap(); } - - updateMap(); } // Updates the UI to match the current map's position private void updateMap() { + Log.i(TAG, "updateMap()"); // rotateImageView(mCompassView, (float) mapView.getDirection()); - if (mGpsLocation != null) { + if (isMyLocationEnabled() && mGpsLocation != null) { + if (mGpsMarker == null) { + // Setup User Location UI + // NOTE: mIsGpsOn == false to begin with + mGpsMarker = new ImageView(getContext()); + mGpsMarker.setImageResource(R.drawable.location_marker); + addView(mGpsMarker); + } + mGpsMarker.setVisibility(View.VISIBLE); LatLng coordinate = new LatLng(mGpsLocation); PointF screenLocation = toScreenLocation(coordinate); + float iconSize = 27.0f * mScreenDensity; + // Update Location + FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams((int) iconSize, (int) iconSize); + lp.leftMargin = (int) (screenLocation.x - iconSize / 2.0f); + lp.topMargin = getHeight() - (int) (screenLocation.y + iconSize / 2.0f); + mGpsMarker.setLayoutParams(lp); +// rotateImageView(mGpsMarker, 0.0f); + mGpsMarker.requestLayout(); + /* if (mGpsLocation.hasBearing() || mCompassValid) { mGpsMarker.setImageResource(R.drawable.direction_arrow); @@ -1473,15 +1495,6 @@ private void updateMap() { mGpsMarker.requestLayout(); } else { */ - mGpsMarker.setImageResource(R.drawable.location_marker); - float iconSize = 27.0f * mScreenDensity; - FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams((int) iconSize, (int) iconSize); - lp.leftMargin = (int) (screenLocation.x - iconSize / 2.0f); - lp.topMargin = getHeight() - (int) (screenLocation.y + iconSize / 2.0f); - mGpsMarker.setLayoutParams(lp); -// rotateImageView(mGpsMarker, 0.0f); - mGpsMarker.requestLayout(); -// } } else { mGpsMarker.setVisibility(View.INVISIBLE); } diff --git a/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java b/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java index 6dd300f4cba..3c366b4d1fc 100644 --- a/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java +++ b/android/java/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxgl/testapp/MainActivity.java @@ -624,6 +624,7 @@ private class MyOnMapChangedListener implements MapView.OnMapChangedListener { @Override public void onMapChanged() { + Log.i(TAG, "onMapChanged()"); updateMap(); } }