diff options
author | Tobrun <tobrun@mapbox.com> | 2016-04-21 10:50:01 +0200 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2016-05-20 12:00:01 +0200 |
commit | 028ef7a32356ffce8ba66b203016445e02637644 (patch) | |
tree | b04e8d8e228c29125533a18919335fb7df232f7f | |
parent | ef257c257d8ba681005c3b7ce4618b09caaa0f37 (diff) | |
download | qtlocation-mapboxgl-028ef7a32356ffce8ba66b203016445e02637644.tar.gz |
[android] #3276 - WIP Android View Annotations, showing an Android SDK view on the screen
15 files changed, 391 insertions, 1 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Marker.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Marker.java index 8a6ff519ad..9b7e7eb1ac 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Marker.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/Marker.java @@ -16,6 +16,8 @@ import com.mapbox.mapboxsdk.maps.MapView; */ public class Marker extends Annotation { + private MarkerView markerView; + private LatLng position; private String snippet; private Icon icon; @@ -78,6 +80,10 @@ public class Marker extends Annotation { if (map != null) { map.updateMarker(this); } + + if(markerView!=null){ + markerView.setLatLng(position); + } } void setSnippet(String snippet) { @@ -141,6 +147,19 @@ public class Marker extends Annotation { return infoWindow; } + public MarkerView getMarkerView() { + return markerView; + } + + void setMarkerView(MarkerView markerView) { + MapboxMap map = getMapboxMap(); + if (map != null) { + this.markerView = markerView; + this.markerView.setLatLng(position); + map.addMarkerView(markerView); + } + } + /** * Do not use this method. Used internally by the SDK. */ diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerView.java new file mode 100644 index 0000000000..3e9d120343 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/annotations/MarkerView.java @@ -0,0 +1,69 @@ +package com.mapbox.mapboxsdk.annotations; + +import android.content.Context; +import android.graphics.PointF; +import android.util.AttributeSet; +import android.util.Log; +import android.view.ViewGroup; +import android.widget.FrameLayout; + +import com.mapbox.mapboxsdk.constants.MapboxConstants; +import com.mapbox.mapboxsdk.geometry.LatLng; +import com.mapbox.mapboxsdk.maps.MapView; +import com.mapbox.mapboxsdk.maps.Projection; + +public class MarkerView extends FrameLayout { + + private LatLng latLng; + private Projection projection; + private Marker marker; + + private float widthOffset; + private float heightOffset; + + public MarkerView(Context context) { + super(context); + init(context); + } + + public MarkerView(Context context, AttributeSet attrs) { + super(context, attrs); + init(context); + } + + public MarkerView(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(context); + } + + private void init(Context context){ + setLayoutParams(new MapView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + } + + public void setProjection(Projection projection) { + this.projection = projection; + } + + public void setLatLng(LatLng latLng) { + this.latLng = latLng; + } + + public void setMarker(Marker marker) { + this.marker = marker; + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + widthOffset = getMeasuredWidth() / 2; + heightOffset = getMeasuredHeight() / 2; + + Log.v(MapboxConstants.TAG,"Measure "+widthOffset + heightOffset); + } + + public void update() { + PointF point = projection.toScreenLocation(latLng); + setX(point.x - widthOffset); + setY(point.y - heightOffset); + } +} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java index be4819064e..960b478f90 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapView.java @@ -65,6 +65,7 @@ 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; @@ -1319,6 +1320,11 @@ public class MapView extends FrameLayout { mCompassView.update(getDirection()); mMyLocationView.update(); + + for (MarkerView view : mMapboxMap.getMarkerViews()) { + view.update(); + } + for (InfoWindow infoWindow : mMapboxMap.getInfoWindows()) { infoWindow.update(); } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java index 0e763a9989..b12371b3ab 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapboxMap.java @@ -20,6 +20,7 @@ import com.mapbox.mapboxsdk.annotations.Icon; import com.mapbox.mapboxsdk.annotations.InfoWindow; import com.mapbox.mapboxsdk.annotations.Marker; import com.mapbox.mapboxsdk.annotations.MarkerOptions; +import com.mapbox.mapboxsdk.annotations.MarkerView; import com.mapbox.mapboxsdk.annotations.Polygon; import com.mapbox.mapboxsdk.annotations.PolygonOptions; import com.mapbox.mapboxsdk.annotations.Polyline; @@ -58,9 +59,13 @@ public class MapboxMap { private CameraPosition mCameraPosition; private boolean mInvalidCameraPosition; private LongSparseArray<Annotation> mAnnotations; + private List<Marker> mSelectedMarkers; + private List<MarkerView> mMarkerViews; private List<InfoWindow> mInfoWindows; + private MapboxMap.InfoWindowAdapter mInfoWindowAdapter; + private MapboxMap.MarkerViewAdapter mMarkerViewAdapter; private boolean mMyLocationEnabled; private boolean mAllowConcurrentMultipleInfoWindows; @@ -89,6 +94,7 @@ public class MapboxMap { mProjection = new Projection(mapView); mAnnotations = new LongSparseArray<>(); mSelectedMarkers = new ArrayList<>(); + mMarkerViews = new ArrayList<>(); mInfoWindows = new ArrayList<>(); } @@ -613,6 +619,12 @@ public class MapboxMap { // Annotations // + public void addMarkerView(MarkerView markerView){ + markerView.setProjection(mProjection); + mMarkerViews.add(markerView); + mMapView.addView(markerView); + } + /** * <p> * Adds a marker to this map. @@ -643,6 +655,14 @@ public class MapboxMap { @NonNull public Marker addMarker(@NonNull BaseMarkerOptions markerOptions) { Marker marker = prepareMarker(markerOptions); + + if(mMarkerViewAdapter!=null){ + MarkerView view = mMarkerViewAdapter.getView(marker); + if(view!=null) { + mMarkerViews.add(view); + } + } + long id = mMapView.addMarker(marker); marker.setMapboxMap(this); marker.setId(id); @@ -1001,6 +1021,18 @@ public class MapboxMap { return markers; } + @Nullable + public MarkerView getMarkerView(long id) { + MarkerView markerView = null; + List<Marker> markers = getMarkers(); + for (Marker m : markers) { + if (m.getId() == id) { + markerView = m.getMarkerView(); + } + } + return markerView; + } + /** * Returns a list of all the polygons on the map. * @@ -1133,6 +1165,10 @@ public class MapboxMap { return marker; } + public void setMarkerViewAdapter(@Nullable MarkerViewAdapter markerViewAdapter){ + mMarkerViewAdapter = markerViewAdapter; + } + // // InfoWindow // @@ -1188,6 +1224,11 @@ public class MapboxMap { return mInfoWindows; } + // used by MapView + List<MarkerView> getMarkerViews(){ + return mMarkerViews; + } + private boolean isInfoWindowValidForMarker(@NonNull Marker marker) { return !TextUtils.isEmpty(marker.getTitle()) || !TextUtils.isEmpty(marker.getSnippet()); } @@ -1715,6 +1756,12 @@ public class MapboxMap { View getInfoWindow(@NonNull Marker marker); } + public interface MarkerViewAdapter { + + @Nullable + MarkerView getView(@NonNull Marker marker); + } + /** * Interface definition for a callback to be invoked when the the My Location view changes location. * diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml index 5411643842..d31915f217 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/AndroidManifest.xml @@ -253,6 +253,16 @@ android:value="@string/category_maplayout" /> </activity> + <activity + android:name=".activity.annotation.ViewMarkerActivity" + android:description="@string/description_view_marker" + android:label="@string/activity_view_marker"> + <meta-data + android:name="@string/category" + android:value="@string/category_annotation" /> + </activity> + + <!-- Configuration Settings --> <meta-data android:name="com.mapbox.TestEventsServer" diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/ViewMarkerActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/ViewMarkerActivity.java new file mode 100644 index 0000000000..27615da5dd --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/annotation/ViewMarkerActivity.java @@ -0,0 +1,123 @@ +package com.mapbox.mapboxsdk.testapp.activity.annotation; + +import android.os.Bundle; +import android.support.annotation.DrawableRes; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v7.app.ActionBar; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; +import android.view.MenuItem; +import android.view.View; + +import com.mapbox.mapboxsdk.annotations.Marker; +import com.mapbox.mapboxsdk.annotations.MarkerOptions; +import com.mapbox.mapboxsdk.annotations.MarkerView; +import com.mapbox.mapboxsdk.geometry.LatLng; +import com.mapbox.mapboxsdk.maps.MapView; +import com.mapbox.mapboxsdk.maps.MapboxMap; +import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; +import com.mapbox.mapboxsdk.testapp.R; +import com.mapbox.mapboxsdk.testapp.view.CustomMarkerView; + +public class ViewMarkerActivity extends AppCompatActivity implements OnMapReadyCallback { + + private MapView mMapView; + private Marker brazil, germany, china, us; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_marker_view); + + Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.setDisplayHomeAsUpEnabled(true); + actionBar.setDisplayShowHomeEnabled(true); + } + + mMapView = (MapView) findViewById(R.id.mapView); + mMapView.onCreate(savedInstanceState); + mMapView.getMapAsync(this); + } + + @Override + public void onMapReady(MapboxMap mapboxMap) { + + // adapt GL-marker to View-marker + mapboxMap.setMarkerViewAdapter(new MapboxMap.MarkerViewAdapter() { + @Nullable + @Override + public MarkerView getView(@NonNull Marker marker) { + if (marker.equals(brazil)) { + return createMarkerView("br", R.drawable.ic_brazil); + } else if (marker.equals(germany)) { + return createMarkerView("de", R.drawable.ic_germany); + } else if (marker.equals(china)) { + return createMarkerView("ch", R.drawable.ic_china); + } else if (marker.equals(us)) { + return createMarkerView("us", R.drawable.ic_us); + } + return null; + } + }); + + // add markers + china = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(31.230416, 121.473701))); + brazil = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(-15.798200, -47.922363))); + us = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(38.907192, -77.036871))); + germany = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(52.520007, 13.404954))); + } + + private MarkerView createMarkerView(String countryAbbrev, @DrawableRes int countryIconRes) { + CustomMarkerView customMarkerView = new CustomMarkerView(this); + customMarkerView.setText(countryAbbrev); + customMarkerView.setImage(countryIconRes); + return customMarkerView; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + onBackPressed(); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + + @Override + public void onResume() { + super.onResume(); + mMapView.onResume(); + } + + @Override + public void onPause() { + super.onPause(); + mMapView.onPause(); + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + super.onSaveInstanceState(outState); + mMapView.onSaveInstanceState(outState); + } + + @Override + protected void onDestroy() { + super.onDestroy(); + mMapView.onDestroy(); + } + + @Override + public void onLowMemory() { + super.onLowMemory(); + mMapView.onLowMemory(); + } + +}
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/view/CustomMarkerView.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/view/CustomMarkerView.java new file mode 100644 index 0000000000..761500aa43 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/view/CustomMarkerView.java @@ -0,0 +1,36 @@ +package com.mapbox.mapboxsdk.testapp.view; + +import android.content.Context; +import android.support.annotation.DrawableRes; +import android.support.annotation.NonNull; +import android.view.LayoutInflater; +import android.widget.ImageView; +import android.widget.TextView; + +import com.mapbox.mapboxsdk.annotations.MarkerView; +import com.mapbox.mapboxsdk.testapp.R; + +public class CustomMarkerView extends MarkerView { + + private TextView textView; + private ImageView imageView; + + public CustomMarkerView(Context context) { + super(context); + init(context); + } + + private void init(Context context) { + LayoutInflater.from(context).inflate(R.layout.view_custom_marker, this); + textView = (TextView) findViewById(R.id.textView); + imageView = (ImageView) findViewById(R.id.imageView); + } + + public void setText(@NonNull String text) { + textView.setText(text); + } + + public void setImage(@DrawableRes int drawableRes) { + imageView.setImageResource(drawableRes); + } +} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable-xxhdpi/ic_brazil.png b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable-xxhdpi/ic_brazil.png Binary files differnew file mode 100644 index 0000000000..13bce838ff --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable-xxhdpi/ic_brazil.png diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable-xxhdpi/ic_china.png b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable-xxhdpi/ic_china.png Binary files differnew file mode 100644 index 0000000000..d75026aac0 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable-xxhdpi/ic_china.png diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable-xxhdpi/ic_germany.png b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable-xxhdpi/ic_germany.png Binary files differnew file mode 100644 index 0000000000..07707aa0ff --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable-xxhdpi/ic_germany.png diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable-xxhdpi/ic_us.png b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable-xxhdpi/ic_us.png Binary files differnew file mode 100644 index 0000000000..57f3cbe654 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/drawable-xxhdpi/ic_us.png diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_marker_view.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_marker_view.xml new file mode 100644 index 0000000000..687592502f --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_marker_view.xml @@ -0,0 +1,58 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical"> + + <android.support.v7.widget.Toolbar + android:id="@id/toolbar" + android:layout_width="match_parent" + android:layout_height="?attr/actionBarSize" + android:background="@color/primary" + android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> + + </android.support.v7.widget.Toolbar> + + <FrameLayout + android:id="@id/container" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <android.support.design.widget.CoordinatorLayout + android:id="@+id/coordinator_layout" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + <com.mapbox.mapboxsdk.maps.MapView + android:id="@id/mapView" + android:layout_width="match_parent" + app:access_token="@string/mapbox_access_token" + app:style_url="@string/style_light" + android:layout_height="match_parent" /> + + <!--<android.support.design.widget.FloatingActionButton--> + <!--android:id="@id/fab"--> + <!--android:layout_width="wrap_content"--> + <!--android:layout_height="wrap_content"--> + <!--android:layout_gravity="end|bottom"--> + <!--android:layout_marginBottom="82dp"--> + <!--android:layout_marginRight="@dimen/fab_margin"--> + <!--android:src="@drawable/ic_refresh_24dp"--> + <!--app:backgroundTint="@color/accent"--> + <!--app:layout_anchorGravity="top" />--> + + <android.support.design.widget.FloatingActionButton + android:id="@id/fab" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="end|bottom" + android:layout_margin="@dimen/fab_margin" + android:src="@drawable/ic_layers_24dp" + app:backgroundTint="@color/primary" /> + + </android.support.design.widget.CoordinatorLayout> + + </FrameLayout> + +</LinearLayout> diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/view_custom_marker.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/view_custom_marker.xml new file mode 100644 index 0000000000..08caf1df66 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/view_custom_marker.xml @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="utf-8"?> +<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="wrap_content" + android:layout_height="wrap_content"> + + <ImageView + android:id="@id/imageView" + android:layout_width="64dp" + android:layout_height="64dp"/> + + <TextView + android:id="@id/textView" + android:layout_width="wrap_content" + android:textColor="@android:color/white" + android:layout_height="wrap_content" + android:textStyle="bold" + android:layout_centerInParent="true" /> + +</RelativeLayout>
\ No newline at end of file diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/ids.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/ids.xml index 9b316d0c31..120bf49fca 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/ids.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/ids.xml @@ -4,6 +4,7 @@ <item name="fab" type="id" /> <item name="progress" type="id" /> <item name="imageView" type="id" /> + <item name="textView" type="id" /> <item name="toolbar" type="id" /> <item name="container" type="id" /> <item name="item_click_support" type="id" /> diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml index 835b1479f1..840427af09 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/res/values/strings.xml @@ -17,7 +17,8 @@ <string name="activity_polyline">Polyline</string> <string name="activity_polygon">Polygon</string> <string name="activity_press_for_marker">Press Map For Marker</string> - + <string name="activity_view_marker">x</string> + <!-- InfoWindow--> <string name="activity_info_window">Standard InfoWindow example</string> <string name="activity_infowindow_adapter">Custom InfoWindow Adapter</string> @@ -76,6 +77,7 @@ <string name="description_scroll_by">Scroll with pixels in x,y direction</string> <string name="description_snapshot">Example to make a snapshot of the map</string> <string name="description_doublemap">2 maps in a view hierarchy</string> + <string name="description_view_marker">x</string> <string name="menuitem_title_concurrent_infowindow">Concurrent Open InfoWindows</string>r <string name="menuitem_title_tracking_mode_dismiss_on_gesture">Dismiss location tracking on gesture</string> |