Add InfoWindowAdapter, had to move some parts in InfoWindow + Marker

InfoWindowAdapter example in sample app
This commit is contained in:
Tobrun
2015-09-29 15:54:48 +02:00
parent 3d834b5c40
commit 02bc2bd7be
4 changed files with 95 additions and 9 deletions

View File

@@ -157,11 +157,24 @@ public class Marker extends Annotation {
return;
}
getInfoWindow().adaptDefaultMarker(this);
getInfoWindow().open(this, getPosition(), (int) anchorU, (int) anchorV);
getInfoWindow().setBoundMarker(this);
infoWindowShown = true;
}
public void showInfoWindow(View view){
if (!isVisible() || mapView == null) {
return;
}
infoWindow = new InfoWindow(view, mapView);
infoWindow.open(this, getPosition(), (int) anchorU, (int) anchorV);
infoWindow.setBoundMarker(this);
infoWindowShown = true;
}
/**
* Use to set a custom OnTouchListener for the InfoWindow.
* By default the InfoWindow will close on touch.

View File

@@ -81,7 +81,6 @@ public final class InfoWindow {
* @return this infowindow
*/
public InfoWindow open(Marker object, LatLng position, int offsetX, int offsetY) {
onOpen(object);
MapView.LayoutParams lp = new MapView.LayoutParams(MapView.LayoutParams.WRAP_CONTENT, MapView.LayoutParams.WRAP_CONTENT);
mView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
@@ -141,7 +140,7 @@ public final class InfoWindow {
*
* @param overlayItem the tapped overlay item
*/
public void onOpen(Marker overlayItem) {
public void adaptDefaultMarker(Marker overlayItem) {
String title = overlayItem.getTitle();
((TextView) mView.findViewById(mTitleId /*R.id.title*/)).setText(title);
String snippet = overlayItem.getSnippet();

View File

@@ -192,6 +192,7 @@ public final class MapView extends FrameLayout implements LocationListener, Comp
private List<Annotation> mAnnotations = new ArrayList<>();
private List<Annotation> mAnnotationsNearLastTap = new ArrayList<>();
private Annotation mSelectedAnnotation = null;
private InfoWindowAdapter mInfoWindowAdapter;
// Used for the Mapbox Logo
private ImageView mLogoView;
@@ -395,6 +396,19 @@ public final class MapView extends FrameLayout implements LocationListener, Comp
void onMapChanged(MapChange change);
}
/**
* Interface definition for a callback to be invoked when an {@link InfoWindow} will be shown.
*/
public interface InfoWindowAdapter {
/**
* Called when the user clicks on a marker.
*
* @param marker The marker the user clicked on.
* @return View to be shown as a {@link InfoWindow}
*/
View getInfoWindow(Marker marker);
}
//
// Constructors
//
@@ -1503,12 +1517,20 @@ public final class MapView extends FrameLayout implements LocationListener, Comp
deselectAnnotation();
Marker marker = (Marker) annotation;
boolean handledClick = false;
boolean handledDefaultClick = false;
if (onMarkerClickListener != null) {
handledClick = onMarkerClickListener.onMarkerClick(marker);
// end developer has provided a custom click listener
handledDefaultClick = onMarkerClickListener.onMarkerClick(marker);
}
if (!handledClick) {
if(mInfoWindowAdapter!=null){
// end developer is using a custom InfoWindowAdapter
View content = mInfoWindowAdapter.getInfoWindow(marker);
if(content != null){
marker.showInfoWindow(content);
}
}else if (!handledDefaultClick) {
// default behaviour
marker.showInfoWindow();
}
@@ -2331,7 +2353,7 @@ public final class MapView extends FrameLayout implements LocationListener, Comp
//
/**
* Add an OnMapChangedListner
* Add an OnMapChangedListener
*
* @param listener Listener to add
*/
@@ -2341,6 +2363,15 @@ public final class MapView extends FrameLayout implements LocationListener, Comp
}
}
/**
* Add an InfoWindowAdapter
*
* @param infoWindowAdapter to set
*/
public void setInfoWindowAdapter(@NonNull InfoWindowAdapter infoWindowAdapter){
mInfoWindowAdapter = infoWindowAdapter;
}
/**
* Remove an OnMapChangedListener
*
@@ -2659,4 +2690,4 @@ public final class MapView extends FrameLayout implements LocationListener, Comp
context.startActivity(intent);
}
}
}
}

View File

@@ -1,11 +1,20 @@
package com.mapbox.mapboxsdk.testapp;
import android.graphics.Color;
import android.os.Bundle;
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.MenuItem;
import android.view.View;
import android.widget.TextView;
import com.mapbox.mapboxsdk.annotations.Marker;
import com.mapbox.mapboxsdk.annotations.MarkerOptions;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.views.MapView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@@ -24,8 +33,12 @@ public class SecondMapActivity extends AppCompatActivity {
Toolbar toolbar = (Toolbar) findViewById(R.id.secondToolBar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
ActionBar actionBar = getSupportActionBar();
if(actionBar!=null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
mMapView = (MapView) findViewById(R.id.secondMapView);
// Load the access token
@@ -40,6 +53,36 @@ public class SecondMapActivity extends AppCompatActivity {
mMapView.onCreate(savedInstanceState);
mMapView.setStyleUrl(MapView.StyleUrls.EMERALD);
mMapView.setInfoWindowAdapter(new MapView.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
TextView textView = new TextView(SecondMapActivity.this);
textView.setText(marker.getTitle());
textView.setBackgroundColor(Color.WHITE);
return textView;
}
});
mMapView.addMarker(generateMarker("Andorra", 42.505777, 1.525294));
mMapView.addMarker(generateMarker("Luxembourg", 49.815273, 6.129583));
mMapView.addMarker(generateMarker("Monaco", 43.738418, 7.424616));
mMapView.addMarker(generateMarker("Vatican City", 41.902916, 12.453389));
mMapView.addMarker(generateMarker("San Marino", 43.942360, 12.457777));
mMapView.addMarker(generateMarker("Malta", 35.892110, 14.427795));
mMapView.addMarker(generateMarker("Liechtenstein", 47.166000, 9.555373));
mMapView.setCenterCoordinate(new LatLng(47.798202, 3.573781));
mMapView.setZoomLevel(3);
}
private MarkerOptions generateMarker(String title, double lat, double lng){
MarkerOptions marker = new MarkerOptions();
marker.title(title);
marker.snippet(title);
marker.position(new LatLng(lat, lng));
return marker;
}
/**