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

Commit

Permalink
[android] #5238 - marker view z ordering example
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Aug 10, 2016
1 parent 71e9285 commit a0bd9a0
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,24 @@
android:value="@string/category_fragment" />
</activity>

<activity android:name=".activity.style.RuntimeStyleActivity"
android:description="@string/description_runtime_style"
android:label="@string/activity_runtime_style">
<activity
android:name=".activity.style.RuntimeStyleActivity"
android:description="@string/description_runtime_style"
android:label="@string/activity_runtime_style">
<meta-data
android:name="@string/category"
android:value="@string/category_style" />
</activity>

<activity
android:name=".activity.annotation.MarkerViewZOrderingActivity"
android:description="@string/description_marker_view_z_ordering"
android:label="@string/activity_view_marker_z_ordering">
<meta-data
android:name="@string/category"
android:value="@string/category_annotation" />
</activity>

<!-- Configuration Settings -->
<meta-data
android:name="com.mapbox.TestEventsServer"
Expand Down
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;
}
}
}

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>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<string name="activity_press_for_marker">Press Map For Marker</string>
<string name="activity_view_marker">View Marker API</string>
<string name="activity_view_marker_scale">Scaling in the View Marker API</string>
<string name="activity_view_marker_z_ordering">View Marker Z ordering</string>

<!-- InfoWindow-->
<string name="activity_info_window">Standard InfoWindow</string>
Expand Down Expand Up @@ -94,6 +95,7 @@
<string name="description_viewpager">Use SupportMapFragments in a ViewPager</string>
<string name="description_navigation_drawer">Test for Navigation Drawer support</string>
<string name="description_runtime_style">Adopt the map style on the fly</string>
<string name="description_marker_view_z_ordering">Marker View Z ordering</string>

<string name="menuitem_title_concurrent_infowindow">Concurrent Open InfoWindows</string>
<string name="menuitem_title_deselect_markers_on_tap">Deselect Markers On Tap</string>
Expand Down

0 comments on commit a0bd9a0

Please sign in to comment.