[android] #3276 - remove Marker View implementation

This commit is contained in:
Tobrun
2016-05-09 11:17:52 +02:00
parent d5c63e6c82
commit b32c44ce73
6 changed files with 115 additions and 22 deletions

View File

@@ -456,14 +456,7 @@ public class MapView extends FrameLayout {
}
} else if (change == REGION_IS_CHANGING || change == REGION_DID_CHANGE) {
if (mMapboxMap.getMarkerViewAdapter() != null) {
long currentTime = SystemClock.elapsedRealtime();
if (mViewMarkersUpdateRunning || currentTime < mViewMarkerBoundsUpdateTime) {
return;
}
mViewMarkerBoundsUpdateTime = currentTime + 300;
new MarkerInBoundsTask().execute();
invalidateViewMarkers();
}
}
}
@@ -478,8 +471,18 @@ public class MapView extends FrameLayout {
}
}
void invalidateViewMarkers(){
long currentTime = SystemClock.elapsedRealtime();
public class MarkerInBoundsTask extends AsyncTask<Void, Void, MarkerInBoundsTask.Result> {
if (mViewMarkersUpdateRunning || currentTime < mViewMarkerBoundsUpdateTime) {
return;
}
mViewMarkerBoundsUpdateTime = currentTime + 300;
new ViewMarkerInBoundsTask().execute();
}
public class ViewMarkerInBoundsTask extends AsyncTask<Void, Void, ViewMarkerInBoundsTask.Result> {
@Override
protected void onPreExecute() {
@@ -1454,12 +1457,14 @@ public class MapView extends FrameLayout {
mViewHolder = viewMarkers.valueAt(i);
if (mViewHolder != null) {
Marker marker = (Marker) mMapboxMap.getAnnotation(viewMarkers.keyAt(i));
PointF point = mMapboxMap.getProjection().toScreenLocation(marker.getPosition());
mViewHolder.setX(point.x - (mViewHolder.getMeasuredWidth() / 2));
mViewHolder.setY(point.y - (mViewHolder.getMeasuredHeight() / 2));
if(marker!=null) {
PointF point = mMapboxMap.getProjection().toScreenLocation(marker.getPosition());
mViewHolder.setX(point.x - (mViewHolder.getMeasuredWidth() / 2));
mViewHolder.setY(point.y - (mViewHolder.getMeasuredHeight() / 2));
if (mViewHolder.getVisibility() == View.GONE) {
mViewHolder.setVisibility(View.VISIBLE);
if (mViewHolder.getVisibility() == View.GONE) {
mViewHolder.setVisibility(View.VISIBLE);
}
}
}
}

View File

@@ -636,9 +636,11 @@ public class MapboxMap {
// Annotations
//
void setViewMarkersBoundsTaskResult(MapView.MarkerInBoundsTask.Result result) {
void setViewMarkersBoundsTaskResult(MapView.ViewMarkerInBoundsTask.Result result) {
Map<Marker, View> outBoundsMarker = result.getOutBounds();
View convertView;
// out of bounds markers
for (Map.Entry<Marker, View> outBoundsEntry : outBoundsMarker.entrySet()) {
convertView = outBoundsEntry.getValue();
if (convertView != null) {
@@ -648,6 +650,7 @@ public class MapboxMap {
}
}
// in bounds markers
List<Marker> inBoundsMarkers = result.getInBounds();
for (final Marker marker : inBoundsMarkers) {
convertView = viewSimplePool.acquire();
@@ -953,13 +956,24 @@ public class MapboxMap {
@UiThread
public void removeAnnotation(@NonNull Annotation annotation) {
if (annotation instanceof Marker) {
((Marker) annotation).hideInfoWindow();
Marker marker = (Marker) annotation;
marker.hideInfoWindow();
removeMarkerView(annotation.getId());
}
long id = annotation.getId();
mMapView.removeAnnotation(id);
mAnnotations.remove(id);
}
private void removeMarkerView(long id){
View viewHolder = mMarkerViews.get(id);
if(viewHolder!=null) {
viewHolder.setVisibility(View.GONE);
viewSimplePool.release(viewHolder);
}
mMarkerViews.remove(id);
}
/**
* Removes an annotation from the map
*
@@ -984,6 +998,7 @@ public class MapboxMap {
Annotation annotation = annotationList.get(i);
if (annotation instanceof Marker) {
((Marker) annotation).hideInfoWindow();
removeMarkerView(annotation.getId());
}
ids[i] = annotationList.get(i).getId();
}
@@ -1198,6 +1213,9 @@ public class MapboxMap {
public void setMarkerViewAdapter(@Nullable MarkerViewAdapter markerViewAdapter) {
mMarkerViewAdapter = markerViewAdapter;
if(markerViewAdapter!=null){
mMapView.invalidateViewMarkers();
}
}
public MarkerViewAdapter getMarkerViewAdapter() {

View File

@@ -5,22 +5,29 @@ import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
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.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import com.mapbox.mapboxsdk.annotations.BaseMarkerOptions;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
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.CountryMarker;
import com.mapbox.mapboxsdk.testapp.utils.GeoParseUtil;
import com.mapbox.mapboxsdk.maps.MapView;
@@ -58,6 +65,8 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
public void onMapReady(@NonNull MapboxMap mapboxMap) {
mMapboxMap = mapboxMap;
mMapboxMap.setMarkerViewAdapter(new TextAdapter(BulkMarkerActivity.this));
if (actionBar != null) {
ArrayAdapter<CharSequence> spinnerAdapter = ArrayAdapter.createFromResource(actionBar.getThemedContext(), R.array.bulk_marker_list, android.R.layout.simple_spinner_item);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
@@ -67,9 +76,38 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
}
}
});
}
private static class TextAdapter implements MapboxMap.MarkerViewAdapter<Marker> {
private LayoutInflater inflater;
public TextAdapter(@NonNull Context context) {
this.inflater = LayoutInflater.from(context);
}
@Nullable
@Override
public View getView(@NonNull Marker 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.title = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.title.setText(marker.getTitle());
return convertView;
}
private static class ViewHolder {
TextView title;
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int markersAmount = Integer.valueOf(getResources().getStringArray(R.array.bulk_marker_list)[position]);
@@ -155,7 +193,7 @@ public class BulkMarkerActivity extends AppCompatActivity implements AdapterView
location = locations.get(i);
markerOptions.add(new MarkerOptions()
.position(location)
.title("Marker")
.title(String.valueOf(i))
.snippet(formatter.format(location.getLatitude()) + ", " + formatter.format(location.getLongitude())));
}

View File

@@ -57,12 +57,12 @@ public class ViewMarkerActivity extends AppCompatActivity implements OnMapReadyC
public void onMapReady(final MapboxMap mapboxMap) {
// Add country markers
List<BaseMarkerOptions> countries = new ArrayList<>();
final List<BaseMarkerOptions> countries = new ArrayList<>();
countries.add(new CountryMarkerOptions().title("China").abbrevName("ch").flagRes(R.drawable.ic_china).position(new LatLng(31.230416, 121.473701)));
countries.add(new CountryMarkerOptions().title("United States").abbrevName("us").flagRes(R.drawable.ic_us).position(new LatLng(38.907192, -77.036871)));
countries.add(new CountryMarkerOptions().title("Brazil").abbrevName("br").flagRes(R.drawable.ic_brazil).position(new LatLng(-15.798200, -47.922363)));
countries.add(new CountryMarkerOptions().title("Germany").abbrevName("de").flagRes(R.drawable.ic_germany).position(new LatLng(52.520007, 13.404954)));
mapboxMap.addMarkers(countries);
final List<Marker> markers = mapboxMap.addMarkers(countries);
// Add view marker adapter
mapboxMap.setMarkerViewAdapter(new CountryAdapter(this));
@@ -72,6 +72,13 @@ public class ViewMarkerActivity extends AppCompatActivity implements OnMapReadyC
Log.d(MapboxConstants.TAG, "Country clicked " + ((CountryMarker) marker).getAbbrevName());
}
});
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mapboxMap.removeAnnotation(markers.get(2));
}
});
}

View File

@@ -27,10 +27,19 @@
<com.mapbox.mapboxsdk.maps.MapView
android:id="@id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:access_token="@string/mapbox_access_token"
app:style_url="@string/style_light"
app:zoom="1" />
app:zoom="1"
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_margin="@dimen/fab_margin"
android:src="@drawable/ic_layers_24dp"
app:backgroundTint="@color/primary" />
</android.support.design.widget.CoordinatorLayout>

View File

@@ -0,0 +1,16 @@
<?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"
android:background="@color/mapbox_green">
<TextView
android:id="@id/textView"
android:layout_width="wrap_content"
android:textColor="@android:color/white"
android:layout_height="wrap_content"
android:textStyle="bold"
android:padding="4dp"
android:layout_centerInParent="true" />
</RelativeLayout>