Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow selecting current location marker #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions library/src/com/cyrilmottier/polaris/PolarisMapView.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
package com.cyrilmottier.polaris;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
Expand Down Expand Up @@ -90,7 +94,7 @@
* <p>Most (or should I say all) map-based applications uses 9-patches as map callout background.
* While 9-patches are great in most cases, it doesn't allow variable stretching of stretchable areas.
* In general, the Polaris library contains default resources and more specifically
* {@link MapViewCallout}. {@link MapViewCallout} allows variable positioning of the anchor. This
* {@link MapCalloutView}. {@link MapCalloutView} allows variable positioning of the anchor. This
* improvement is largely used by the Polaris library to get a more polished map. While most
* applications center the map on the tapped {@link OverlayItem}, {@link PolarisMapView} shows a
* map callout trying to reduce scrolling as much as possible. The map is actually scrolled only there
Expand Down Expand Up @@ -281,7 +285,7 @@ public interface OnMapViewLongClickListener {
private OnMapViewLongClickListener mOnMapViewLongClickListener;

private OverlayContainer mOverlayContainer;
private MyLocationOverlay mMyLocationOverlay;
private PolarisMyLocationOverlay mMyLocationOverlay;
private AnnotationsOverlay mAnnotationsOverlay;

private boolean mIsInGesture;
Expand All @@ -292,6 +296,10 @@ public interface OnMapViewLongClickListener {
private MapCalloutView mMapCallouts[] = new MapCalloutView[2];
private int mMapCalloutIndex;

private Annotation mCurrentLocationAnnotation;
private String mCurrentLocationTitle;
private String mCurrentLocationSubtitle;

/**
* Create a new {@link PolarisMapView}.
*
Expand Down Expand Up @@ -490,7 +498,7 @@ public void setOnRegionChangedListenerListener(OnRegionChangedListener listener)
/**
* Set a new {@link OnMapViewLongClickListener}.
*
* @param listener The new {@link OnMapViewLongClickListener}
* @param l The new {@link OnMapViewLongClickListener}
*/
public void setOnMapViewLongClickListener(OnMapViewLongClickListener l) {
mOnMapViewLongClickListener = l;
Expand All @@ -507,6 +515,11 @@ public boolean isUserTrackingButtonEnabled() {
return mIsUserTrackingButtonEnabled;
}

public void setCurrentLocationMarker(String title, String subtitle) {
mCurrentLocationTitle = title;
mCurrentLocationSubtitle = subtitle;
}

/**
* Enable/disable user tracking on this {@link PolarisMapView}.
*
Expand All @@ -530,10 +543,28 @@ public void setUserTrackingButtonEnabled(boolean enabled) {
);
//@formatter:on
}
mMyLocationOverlay = new MyLocationOverlay(getContext(), this);
mMyLocationOverlay = new PolarisMyLocationOverlay(getContext(), this);
mMyLocationOverlay.enableMyLocation();
mOverlayContainer.setUserLocationOverlay(mMyLocationOverlay);
addView(mUserTrackingButton);

mMyLocationOverlay.setOnCurrentLocationChangedListener(new PolarisMyLocationOverlay.OnCurrentLocationChangedListener() {
@Override
public void onCurrentLocationChanged(Location location) {
final GeoPoint p = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));
if(mCurrentLocationAnnotation != null)
mAnnotationsOverlay.removeAnnotation(mCurrentLocationAnnotation);

ColorDrawable blankMarker = new ColorDrawable(Color.TRANSPARENT);
mCurrentLocationAnnotation = new Annotation(p, mCurrentLocationTitle, mCurrentLocationSubtitle, blankMarker);
if(mAnnotationsOverlay == null) {
setAnnotations(Collections.singletonList(mCurrentLocationAnnotation), blankMarker);
} else {
mAnnotationsOverlay.addAnnotation(mCurrentLocationAnnotation);
}
}
});

} else {
if (mUserTrackingButton != null) {
removeView(mUserTrackingButton);
Expand Down Expand Up @@ -609,10 +640,17 @@ public void setAnnotations(List<Annotation> annotations, int annotationMarkerId)
* @param annotationMarker The default marker
*/
public void setAnnotations(List<Annotation> annotations, Drawable annotationMarker) {
//Remove opened callauts before inserting annotations
if (mAnnotationsOverlay != null)
mAnnotationsOverlay.setSelectedAnnotation(INVALID_POSITION);

if (annotations == null) {
mOverlayContainer.setAnnotationsOverlay(null);
} else {
mAnnotationsOverlay = new AnnotationsOverlay(mMystiqueCallback, new ArrayList<Annotation>(annotations), annotationMarker);
if(mCurrentLocationAnnotation != null)
mAnnotationsOverlay.addAnnotation(mCurrentLocationAnnotation);

mOverlayContainer.setAnnotationsOverlay(mAnnotationsOverlay);
}
// Reflect the changes in the MapView
Expand Down
58 changes: 58 additions & 0 deletions library/src/com/cyrilmottier/polaris/PolarisMyLocationOverlay.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.cyrilmottier.polaris;

import android.content.Context;
import android.location.Location;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class PolarisMyLocationOverlay extends MyLocationOverlay {

private OnCurrentLocationClickListener mOnCurrentLocationClickListener;
private OnCurrentLocationChangedListener mOnCurrentLocationChangedListener;

public PolarisMyLocationOverlay(Context context, MapView mapView) {
super(context, mapView);
}

@Override
protected boolean dispatchTap() {
GeoPoint p = this.getMyLocation();

if(mOnCurrentLocationClickListener != null)
mOnCurrentLocationClickListener.onCurrentLocationClick(p);
return true;
}

@Override
public synchronized void onLocationChanged(Location location) {
super.onLocationChanged(location);
if(mOnCurrentLocationChangedListener != null)
mOnCurrentLocationChangedListener.onCurrentLocationChanged(location);
}

public void setOnCurrentLocationClickListener(OnCurrentLocationClickListener mOnCurrentLocationClickListener) {
this.mOnCurrentLocationClickListener = mOnCurrentLocationClickListener;
}

public OnCurrentLocationClickListener getOnCurrentLocationClickListener() {
return mOnCurrentLocationClickListener;
}

public OnCurrentLocationChangedListener getOnCurrentLocationChangedListener() {
return mOnCurrentLocationChangedListener;
}

public void setOnCurrentLocationChangedListener(OnCurrentLocationChangedListener listener) {
mOnCurrentLocationChangedListener = listener;
}

public static interface OnCurrentLocationClickListener {
public void onCurrentLocationClick(GeoPoint point);
}

public static interface OnCurrentLocationChangedListener {
public void onCurrentLocationChanged(Location location);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ public Annotation getAnnotation(int index) {
return mAnnotations.get(index);
}

public void addAnnotation(Annotation a) {
mAnnotations.add(a);
populate();
}

public void removeAnnotation(Annotation a) {
final int index = indexOf(a);
if(index != INVALID_POSITION) {
mAnnotations.remove(index);
populate();
}
}

private int indexOf(Annotation a) {
for(int i = 0, size = mAnnotations.size(); i < size; i++)
if(a == mAnnotations.get(i))
return i;

return INVALID_POSITION;
}

public int getSelectedAnnotation() {
return mSelectedAnnotation;
}
Expand Down