diff options
author | Tobrun <tobrun.van.nuland@gmail.com> | 2015-12-18 09:03:13 +0100 |
---|---|---|
committer | Tobrun <tobrun.van.nuland@gmail.com> | 2015-12-18 09:03:13 +0100 |
commit | f31ff44d7929c225e4c9372abed9c6b7afe2a484 (patch) | |
tree | 21e4337bc3cbad33206d4f0b7e5ad5d90356f19b /platform | |
parent | 9e90934438e18f8512c9d7775bb8ed3389e46627 (diff) | |
download | qtlocation-mapboxgl-f31ff44d7929c225e4c9372abed9c6b7afe2a484.tar.gz |
[android] #3317 #3239 - SpriteFactory Singleton, added different custom markers to InfoWindowActivity
Diffstat (limited to 'platform')
7 files changed, 57 insertions, 35 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/SpriteFactory.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/SpriteFactory.java index 3395c5039d..948f03ebb8 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/SpriteFactory.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/SpriteFactory.java @@ -7,12 +7,13 @@ import android.graphics.Canvas; import android.graphics.Rect; import android.graphics.drawable.Drawable; import android.os.Build; +import android.support.annotation.DrawableRes; +import android.support.annotation.NonNull; import android.util.DisplayMetrics; import android.view.WindowManager; import com.mapbox.mapboxsdk.R; import com.mapbox.mapboxsdk.exceptions.TooManySpritesException; -import com.mapbox.mapboxsdk.views.MapView; import java.io.FileInputStream; import java.io.FileNotFoundException; @@ -23,17 +24,25 @@ public final class SpriteFactory { private static final String SPRITE_ID_PREFIX = "com.mapbox.sprites.sprite_"; - private MapView mMapView; + private Context mContext; + private static SpriteFactory sInstance; private Sprite mDefaultMarker; private BitmapFactory.Options mOptions; private int mNextId = 0; - public SpriteFactory(MapView mapView) { - mMapView = mapView; + public static synchronized SpriteFactory getInstance(@NonNull Context context) { + if (sInstance == null) { + sInstance = new SpriteFactory(context.getApplicationContext()); + } + return sInstance; + } + + private SpriteFactory(@NonNull Context context) { + mContext = context; DisplayMetrics realMetrics = null; DisplayMetrics metrics = new DisplayMetrics(); - WindowManager wm = (WindowManager) mMapView.getContext().getSystemService(Context.WINDOW_SERVICE); + WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { realMetrics = new DisplayMetrics(); @@ -48,31 +57,23 @@ public final class SpriteFactory { if (realMetrics != null) { mOptions.inScreenDensity = realMetrics.densityDpi; } - } - public Sprite fromBitmap(Bitmap bitmap) { - if (bitmap == null) { - return null; - } - + public Sprite fromBitmap(@NonNull Bitmap bitmap) { if (mNextId < 0) { throw new TooManySpritesException(); } String id = SPRITE_ID_PREFIX + ++mNextId; - return new Sprite(id, bitmap); } - public Sprite fromDrawable(Drawable drawable) { + public Sprite fromDrawable(@NonNull Drawable drawable) { int width = drawable.getIntrinsicWidth(); int height = drawable.getIntrinsicHeight(); - return fromDrawable(drawable, width, height); } - - public Sprite fromDrawable(Drawable drawable, int width, int height) { + public Sprite fromDrawable(@NonNull Drawable drawable, int width, int height) { if ((width < 0) || (height < 0)) { return null; } @@ -84,12 +85,11 @@ public final class SpriteFactory { drawable.setBounds(bounds); drawable.draw(canvas); drawable.setBounds(temp); - return fromBitmap(bitmap); } - public Sprite fromResource(int resourceId) { - Bitmap bitmap = BitmapFactory.decodeResource(mMapView.getResources(), resourceId); + public Sprite fromResource(@DrawableRes int resourceId) { + Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), resourceId); return fromBitmap(bitmap); } @@ -100,30 +100,30 @@ public final class SpriteFactory { return mDefaultMarker; } - private Sprite fromInputStream(InputStream is) { + private Sprite fromInputStream(@NonNull InputStream is) { Bitmap bitmap = BitmapFactory.decodeStream(is, null, mOptions); return fromBitmap(bitmap); } - public Sprite fromAsset(String assetName) { + public Sprite fromAsset(@NonNull String assetName) { InputStream is; try { - is = mMapView.getContext().getAssets().open(assetName); + is = mContext.getAssets().open(assetName); } catch (IOException e) { return null; } return fromInputStream(is); } - public Sprite fromPath(String absolutePath) { + public Sprite fromPath(@NonNull String absolutePath) { Bitmap bitmap = BitmapFactory.decodeFile(absolutePath, mOptions); return fromBitmap(bitmap); } - public Sprite fromFile(String fileName) { + public Sprite fromFile(@NonNull String fileName) { FileInputStream is; try { - is = mMapView.getContext().openFileInput(fileName); + is = mContext.openFileInput(fileName); } catch (FileNotFoundException e) { return null; } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/MapView.java index 670e5d473a..67ac9c48f3 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/views/MapView.java @@ -219,8 +219,7 @@ public final class MapView extends FrameLayout { private List<Marker> mSelectedMarkers = new ArrayList<>(); private List<InfoWindow> mInfoWindows = new ArrayList<>(); private InfoWindowAdapter mInfoWindowAdapter; - private SpriteFactory mSpriteFactory; - private ArrayList<Sprite> mSprites = new ArrayList<>(); + private List<Sprite> mSprites = new ArrayList<>(); // Used for the Mapbox Logo private ImageView mLogoView; @@ -229,7 +228,7 @@ public final class MapView extends FrameLayout { private ImageView mAttributionsView; // Used to manage MapChange event listeners - private ArrayList<OnMapChangedListener> mOnMapChangedListener = new ArrayList<>(); + private List<OnMapChangedListener> mOnMapChangedListener = new ArrayList<>(); // Used to manage map click event listeners private OnMapClickListener mOnMapClickListener; @@ -1834,10 +1833,7 @@ public final class MapView extends FrameLayout { // public SpriteFactory getSpriteFactory() { - if (mSpriteFactory == null) { - mSpriteFactory = new SpriteFactory(this); - } - return mSpriteFactory; + return SpriteFactory.getInstance(getContext()); } private void loadSprite(Sprite sprite) { diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/assets/commercial-24.png b/platform/android/MapboxGLAndroidSDKTestApp/src/main/assets/commercial-24.png Binary files differnew file mode 100755 index 0000000000..61f570fc4e --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/assets/commercial-24.png diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/assets/london-underground-24.png b/platform/android/MapboxGLAndroidSDKTestApp/src/main/assets/london-underground-24.png Binary files differnew file mode 100755 index 0000000000..d3b353db51 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/assets/london-underground-24.png diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/assets/town-hall-24.png b/platform/android/MapboxGLAndroidSDKTestApp/src/main/assets/town-hall-24.png Binary files differnew file mode 100755 index 0000000000..d7986df298 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/assets/town-hall-24.png diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/InfoWindowActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/InfoWindowActivity.java index 5764b67017..43d90b5ebb 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/InfoWindowActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/InfoWindowActivity.java @@ -1,21 +1,25 @@ package com.mapbox.mapboxsdk.testapp; import android.os.Bundle; -import android.support.design.widget.Snackbar; +import android.support.annotation.NonNull; 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.annotations.Marker; import com.mapbox.mapboxsdk.annotations.MarkerOptions; -import com.mapbox.mapboxsdk.constants.Style; +import com.mapbox.mapboxsdk.annotations.SpriteFactory; import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.utils.ApiAccess; import com.mapbox.mapboxsdk.views.MapView; +import java.text.DecimalFormat; + public class InfoWindowActivity extends AppCompatActivity { private MapView mMapView; + private Marker mCustomMarker; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,16 +45,38 @@ public class InfoWindowActivity extends AppCompatActivity { mMapView.addMarker(new MarkerOptions() .title("Intersection") .snippet("H St NW with 15th St NW") + .icon(SpriteFactory.getInstance(this).fromAsset("london-underground-24.png")) .position(new LatLng(38.9002073, -77.03364419))); mMapView.addMarker(new MarkerOptions() .title("White House") .snippet("The official residence and principal workplace of the President of the United States, located at 1600 Pennsylvania Avenue NW in Washington, D.C. It has been the residence of every U.S. president since John Adams in 1800.") + .icon(mMapView.getSpriteFactory().fromAsset("town-hall-24.png")) .position(new LatLng(38.897705003219784, -77.03655168667463))); mMapView.addMarker(new MarkerOptions().title("Intersection") .snippet("E St NW with 17th St NW") + .icon(mMapView.getSpriteFactory().fromAsset("commercial-24.png")) .position(new LatLng(38.8954236, -77.0394623))); + + final DecimalFormat formatter = new DecimalFormat("#.#####"); + mMapView.setOnMapLongClickListener(new MapView.OnMapLongClickListener() { + @Override + public void onMapLongClick(@NonNull LatLng point) { + + // Remove previous added marker + if (mCustomMarker != null) { + mMapView.removeAnnotation(mCustomMarker); + mCustomMarker = null; + } + + // Add marker on long click location with default marker image + mCustomMarker = mMapView.addMarker(new MarkerOptions() + .title("Custom Marker") + .snippet(formatter.format(point.getLatitude()) + ", " + formatter.format(point.getLongitude())) + .position(point)); + } + }); } @Override diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/InfoWindowAdapterActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/InfoWindowAdapterActivity.java index 293893069b..79f0877d15 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/InfoWindowAdapterActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/InfoWindowAdapterActivity.java @@ -45,7 +45,7 @@ public class InfoWindowAdapterActivity extends AppCompatActivity { mMapView.setAccessToken(ApiAccess.getToken(this)); mMapView.onCreate(savedInstanceState); - mSpriteFactory = new SpriteFactory(mMapView); + mSpriteFactory = SpriteFactory.getInstance(this); mIconDrawable = ContextCompat.getDrawable(this, R.drawable.ic_location_city_24dp); mMapView.setInfoWindowAdapter(new MapView.InfoWindowAdapter() { |