This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] #5238 - marker view z ordering example
- Loading branch information
Showing
4 changed files
with
267 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
228 changes: 228 additions & 0 deletions
228
...in/java/com/mapbox/mapboxsdk/testapp/activity/annotation/MarkerViewZOrderingActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
package com.mapbox.mapboxsdk.testapp.activity.annotation; | ||
|
||
import android.animation.Animator; | ||
import android.animation.AnimatorInflater; | ||
import android.animation.AnimatorListenerAdapter; | ||
import android.content.Context; | ||
import android.os.Bundle; | ||
import android.os.PersistableBundle; | ||
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.LayoutInflater; | ||
import android.view.MenuItem; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.widget.TextView; | ||
|
||
import com.mapbox.mapboxsdk.annotations.MarkerView; | ||
import com.mapbox.mapboxsdk.annotations.MarkerViewManager; | ||
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.model.annotations.TextMarkerView; | ||
import com.mapbox.mapboxsdk.testapp.model.annotations.TextMarkerViewOptions; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class MarkerViewZOrderingActivity extends AppCompatActivity { | ||
|
||
private static final String KEY_PARCEABLE_MARKERVIEWOPTIONS = "com.mapbox.markerviewoptions"; | ||
|
||
private MapView mapView; | ||
private ArrayList<TextMarkerViewOptions> markerViewOptions; | ||
|
||
@Override | ||
protected void onCreate(final Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_marker_view_z); | ||
|
||
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | ||
setSupportActionBar(toolbar); | ||
|
||
ActionBar actionBar = getSupportActionBar(); | ||
if (actionBar != null) { | ||
actionBar.setDisplayHomeAsUpEnabled(true); | ||
actionBar.setDisplayShowHomeEnabled(true); | ||
} | ||
|
||
mapView = (MapView) findViewById(R.id.mapView); | ||
mapView.onCreate(savedInstanceState); | ||
mapView.getMapAsync(new OnMapReadyCallback() { | ||
@Override | ||
public void onMapReady(MapboxMap mapboxMap) { | ||
|
||
// add marker view adapter | ||
MarkerViewManager markerViewManager = mapboxMap.getMarkerViewManager(); | ||
markerViewManager.addMarkerViewAdapter(new TextAdapter(MarkerViewZOrderingActivity.this, mapboxMap)); | ||
|
||
if (savedInstanceState == null) { | ||
markerViewOptions = new ArrayList<>(); | ||
|
||
TextMarkerViewOptions diplomaticRoomOptions = new TextMarkerViewOptions().position(new LatLng(38.897605, -77.036580)).text("Diplomatic Room"); | ||
MarkerView diplomaticRoom = mapboxMap.addMarker(diplomaticRoomOptions); | ||
markerViewOptions.add(diplomaticRoomOptions); | ||
|
||
TextMarkerViewOptions kitchenOptions = new TextMarkerViewOptions().position(new LatLng(38.897745, -77.036784)).text("Kitchen"); | ||
mapboxMap.addMarker(kitchenOptions); | ||
markerViewOptions.add(kitchenOptions); | ||
|
||
TextMarkerViewOptions library = new TextMarkerViewOptions().position(new LatLng(38.897751, -77.036407)).text("Library"); | ||
mapboxMap.addMarker(library); | ||
markerViewOptions.add(library); | ||
|
||
} else { | ||
// restore markers | ||
markerViewOptions = savedInstanceState.getParcelableArrayList(KEY_PARCEABLE_MARKERVIEWOPTIONS); | ||
if (markerViewOptions != null) { | ||
for (TextMarkerViewOptions textMarkerViewOptions : markerViewOptions) { | ||
mapboxMap.addMarker(textMarkerViewOptions); | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
super.onResume(); | ||
mapView.onResume(); | ||
} | ||
|
||
@Override | ||
protected void onPause() { | ||
super.onPause(); | ||
mapView.onPause(); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
mapView.onDestroy(); | ||
} | ||
|
||
@Override | ||
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) { | ||
super.onSaveInstanceState(outState, outPersistentState); | ||
mapView.onSaveInstanceState(outState); | ||
outState.putParcelableArrayList(KEY_PARCEABLE_MARKERVIEWOPTIONS, markerViewOptions); | ||
} | ||
|
||
@Override | ||
public void onLowMemory() { | ||
super.onLowMemory(); | ||
mapView.onLowMemory(); | ||
} | ||
|
||
@Override | ||
public boolean onOptionsItemSelected(MenuItem item) { | ||
switch (item.getItemId()) { | ||
case android.R.id.home: | ||
onBackPressed(); | ||
return true; | ||
default: | ||
return super.onOptionsItemSelected(item); | ||
} | ||
} | ||
|
||
private static class TextAdapter extends MapboxMap.MarkerViewAdapter<TextMarkerView> { | ||
|
||
private LayoutInflater inflater; | ||
private MapboxMap mapboxMap; | ||
|
||
public TextAdapter(@NonNull Context context, @NonNull MapboxMap mapboxMap) { | ||
super(context); | ||
this.inflater = LayoutInflater.from(context); | ||
this.mapboxMap = mapboxMap; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public View getView(@NonNull TextMarkerView marker, @Nullable View convertView, @NonNull ViewGroup parent) { | ||
ViewHolder viewHolder; | ||
if (convertView == null) { | ||
viewHolder = new ViewHolder(); | ||
convertView = inflater.inflate(R.layout.view_text_marker, parent, false); | ||
viewHolder.textView = (TextView) convertView.findViewById(R.id.textView); | ||
convertView.setTag(viewHolder); | ||
} else { | ||
viewHolder = (ViewHolder) convertView.getTag(); | ||
} | ||
viewHolder.textView.setText(marker.getText()); | ||
return convertView; | ||
} | ||
|
||
@Override | ||
public boolean onSelect(@NonNull final TextMarkerView marker, @NonNull final View convertView, boolean reselectionForViewReuse) { | ||
animateGrow(marker, convertView, 0); | ||
|
||
// false indicates that we are calling selectMarker after our animation ourselves | ||
// true will let the system call it for you, which will result in showing an InfoWindow instantly | ||
return false; | ||
} | ||
|
||
@Override | ||
public void onDeselect(@NonNull TextMarkerView marker, @NonNull final View convertView) { | ||
animateShrink(convertView, 350); | ||
} | ||
|
||
@Override | ||
public boolean prepareViewForReuse(@NonNull MarkerView marker, @NonNull View convertView) { | ||
// this method is called before a view will be reused, we need to restore view state | ||
// as we have scaled the view in onSelect. If not correctly applied other MarkerView will | ||
// become large since these have been recycled | ||
|
||
// cancel ongoing animation | ||
convertView.animate().cancel(); | ||
|
||
if (marker.isSelected()) { | ||
// shrink view to be able to be reused | ||
animateShrink(convertView, 0); | ||
} | ||
|
||
// true if you want reuse to occur automatically, false if you want to manage this yourself | ||
return true; | ||
} | ||
|
||
private void animateGrow(@NonNull final MarkerView marker, @NonNull final View convertView, int duration) { | ||
convertView.setLayerType(View.LAYER_TYPE_HARDWARE, null); | ||
Animator animator = AnimatorInflater.loadAnimator(convertView.getContext(), R.animator.scale_up); | ||
animator.setDuration(duration); | ||
animator.addListener(new AnimatorListenerAdapter() { | ||
@Override | ||
public void onAnimationEnd(Animator animation) { | ||
super.onAnimationEnd(animation); | ||
convertView.setLayerType(View.LAYER_TYPE_NONE, null); | ||
mapboxMap.selectMarker(marker); | ||
} | ||
}); | ||
animator.setTarget(convertView); | ||
animator.start(); | ||
} | ||
|
||
private void animateShrink(@NonNull final View convertView, int duration) { | ||
convertView.setLayerType(View.LAYER_TYPE_HARDWARE, null); | ||
Animator animator = AnimatorInflater.loadAnimator(convertView.getContext(), R.animator.scale_down); | ||
animator.setDuration(duration); | ||
animator.addListener(new AnimatorListenerAdapter() { | ||
@Override | ||
public void onAnimationEnd(Animator animation) { | ||
super.onAnimationEnd(animation); | ||
convertView.setLayerType(View.LAYER_TYPE_NONE, null); | ||
} | ||
}); | ||
animator.setTarget(convertView); | ||
animator.start(); | ||
} | ||
|
||
private static class ViewHolder { | ||
TextView textView; | ||
} | ||
} | ||
} | ||
|
24 changes: 24 additions & 0 deletions
24
platform/android/MapboxGLAndroidSDKTestApp/src/main/res/layout/activity_marker_view_z.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout 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.support.v7.widget.Toolbar | ||
android:id="@id/toolbar" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:background="@color/primary" | ||
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> | ||
|
||
<com.mapbox.mapboxsdk.maps.MapView | ||
android:id="@id/mapView" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:layout_below="@id/toolbar" | ||
app:center_latitude="38.897686" | ||
app:center_longitude="-77.036589" | ||
app:style_url="@string/style_mapbox_streets" | ||
app:zoom="18" /> | ||
|
||
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters