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

Commit

Permalink
[android] Refactor annotations functions
Browse files Browse the repository at this point in the history
Fixes #2539
  • Loading branch information
Leith Bade committed Oct 6, 2015
1 parent b869771 commit fdcef9a
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1555,9 +1555,9 @@ public Marker addMarker(MarkerOptions markerOptions) {

// Load the default marker sprite
if (marker.getSprite() == null) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) ContextCompat.getDrawable(mContext, R.drawable.default_marker);
Bitmap bitmap = bitmapDrawable.getBitmap();
setSprite(DEFAULT_SPRITE, bitmap);
//BitmapDrawable bitmapDrawable = (BitmapDrawable) ContextCompat.getDrawable(mContext, R.drawable.default_marker);
//Bitmap bitmap = bitmapDrawable.getBitmap();
//setSprite(DEFAULT_SPRITE, bitmap);

// Red default marker is currently broken
marker.setSprite("default_marker");
Expand All @@ -1571,17 +1571,33 @@ public Marker addMarker(MarkerOptions markerOptions) {
return marker;
}

public List<Marker> addMarkers(List<Marker> markers) {
public List<Marker> addMarkers(List<MarkerOptions> markerOptionsList) {
List<Marker> markers = new ArrayList<>(markerOptionsList.size());
for (MarkerOptions markerOptions : markerOptionsList) {
Marker marker = markerOptions.getMarker();

// Load the default marker sprite
if (marker.getSprite() == null) {
//BitmapDrawable bitmapDrawable = (BitmapDrawable) ContextCompat.getDrawable(mContext, R.drawable.default_marker);
//Bitmap bitmap = bitmapDrawable.getBitmap();
//setSprite(DEFAULT_SPRITE, bitmap);

// Red default marker is currently broken
marker.setSprite("default_marker");
//marker.setSprite(DEFAULT_SPRITE);
}
markers.add(markerOptions.getMarker());
}

long[] ids = mNativeMapView.addMarkers(markers);
Marker m;
int count = markers.size();
for (int i = 0; i < count; i++) {
m = markers.get(i);
m.setId(ids[i]);
m.setMapView(this);
mAnnotations.add(m);

for (int i = 0; i < markers.size(); i++) {
markers.get(i).setId(ids[i]);
markers.get(i).setMapView(this);
mAnnotations.add(markers.get(i));
}
return markers;

return Collections.unmodifiableList(markers);
}

public Polyline addPolyline(PolylineOptions polylineOptions) {
Expand All @@ -1593,6 +1609,16 @@ public Polyline addPolyline(PolylineOptions polylineOptions) {
return polyline;
}

public List<Polyline> addPolylines(List<PolylineOptions> polylineOptionsList) {
// TODO make faster in JNI
List<Polyline> polylines = new ArrayList<>(polylineOptionsList.size());
for (PolylineOptions polylineOptions : polylineOptionsList) {
polylines.add(addPolyline(polylineOptions));
}

return Collections.unmodifiableList(polylines);
}

public Polygon addPolygon(PolygonOptions polygonOptions) {
Polygon polygon = polygonOptions.getPolygon();
long id = mNativeMapView.addPolygon(polygon);
Expand All @@ -1602,10 +1628,10 @@ public Polygon addPolygon(PolygonOptions polygonOptions) {
return polygon;
}

public List<Polygon> addPolygons(List<PolygonOptions> polygonOptions) {
List<Polygon> polygons = new ArrayList<>();
for (PolygonOptions popts : polygonOptions) {
polygons.add(popts.getPolygon());
public List<Polygon> addPolygons(List<PolygonOptions> polygonOptionsList) {
List<Polygon> polygons = new ArrayList<>(polygonOptionsList.size());
for (PolygonOptions polygonOptions : polygonOptionsList) {
polygons.add(polygonOptions.getPolygon());
}

long[] ids = mNativeMapView.addPolygons(polygons);
Expand Down Expand Up @@ -1640,13 +1666,22 @@ public void removeAnnotation(Annotation annotation) {
mAnnotations.remove(annotation);
}

public void removeAnnotation(long annotationId) {
public void removeAnnotations(List<Annotation> annotationList) {
// TODO make faster in JNI
for (Annotation annotation : annotationList) {
removeAnnotation(annotation);
}
}

// TODO why using ids here?
private void removeAnnotation(long annotationId) {
mNativeMapView.removeAnnotation(annotationId);
removeAnnotationsWithId(annotationId);
}

public void removeAnnotations() {
public void removeAllAnnotations() {
long[] ids = new long[mAnnotations.size()];

for (int i = 0; i < mAnnotations.size(); i++) {
Annotation annotation = mAnnotations.get(i);
long id = annotation.getId();
Expand All @@ -1655,28 +1690,33 @@ public void removeAnnotations() {
((Marker) annotation).hideInfoWindow();
}
}

mNativeMapView.removeAnnotations(ids);
mAnnotations.clear();
}

public List<Annotation> getAnnotations() {
public List<Annotation> getAllAnnotations() {
return Collections.unmodifiableList(mAnnotations);
}

// TODO only returns markers? rename?
public List<Annotation> getAnnotationsInBounds(BoundingBox bbox) {
List<Annotation> annotations = new ArrayList<>();
long[] ids = mNativeMapView.getAnnotationsInBounds(bbox);
List<Long> idsList = new ArrayList<>();

List<Long> idsList = new ArrayList<>(ids.length);
for (long id : ids) {
idsList.add(id);
}

List<Annotation> annotations = new ArrayList<>(ids.length);
for (int i = 0; i < mAnnotations.size(); i++) {
Annotation annotation = mAnnotations.get(i);
if (annotation instanceof Marker && idsList.contains(annotation.getId())) {
annotations.add(annotation);
}
}
return annotations;

return Collections.unmodifiableList(annotations);
}

// Used by InfoWindow
Expand All @@ -1687,10 +1727,11 @@ public List<Annotation> getAnnotationsInBounds(BoundingBox bbox) {
/**
* Returns the distance spanned by one pixel at the specified latitude and current zoom level.
* <p/>
* The distance between pixels decreases as the latitude approaches the poles. This relationship parallels the relationship between longitudinal coordinates at different latitudes.
* The distance between pixels decreases as the latitude approaches the poles.
* This relationship parallels the relationship between longitudinal coordinates at different latitudes.
*
* @param latitude The latitude for which to return the value.
* @return The distance (in meters) spanned by a single pixel.
* @return The distance measured in meters.
*/
public double getMetersPerPixelAtLatitude(double latitude) {
return mNativeMapView.getMetersPerPixelAtLatitude(latitude, getZoomLevel());
Expand All @@ -1702,7 +1743,6 @@ float getScreenDensity() {
}

private void selectAnnotation(Annotation annotation) {

if (annotation == null) {
return;
}
Expand Down Expand Up @@ -1749,7 +1789,6 @@ private void deselectAnnotation() {
}
}


//
// Rendering
//
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,34 +410,34 @@ private void toggleAnnotations(boolean enableAnnotations) {
}

private void addMarkers() {
List<Marker> markerList = new ArrayList<>();
List<MarkerOptions> markerOptionsList = new ArrayList<>();

final Marker backLot = generateMarker("Back Lot", "The back lot behind my house", null, 38.649441, -121.369064);
markerList.add(backLot);
final MarkerOptions backLot = generateMarker("Back Lot", "The back lot behind my house", null, 38.649441, -121.369064);
markerOptionsList.add(backLot);

final Marker cheeseRoom = generateMarker("Cheese Room", "The only air conditioned room on the property", "dog-park-15", 38.531577, -122.010646);
markerList.add(cheeseRoom);
final MarkerOptions cheeseRoom = generateMarker("Cheese Room", "The only air conditioned room on the property", "dog-park-15", 38.531577, -122.010646);
markerOptionsList.add(cheeseRoom);

mMapView.addMarkers(markerList);
List<Marker> markers = mMapView.addMarkers(markerOptionsList);

// need to call this after adding markers to map, click event hook into InfoWindow needs refactoring
backLot.setInfoWindowOnTouchListener(new View.OnTouchListener() {
final Marker backLotMarker = markers.get(0);
backLotMarker.setInfoWindowOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Toast.makeText(getApplicationContext(), "Custom Info Touch Listener!!", Toast.LENGTH_SHORT).show();
backLot.hideInfoWindow();
backLotMarker.hideInfoWindow();
return true;
}
});
}

private Marker generateMarker(String title, String snippet, String sprite, double lat, double lng){
private MarkerOptions generateMarker(String title, String snippet, String sprite, double lat, double lng){
return new MarkerOptions()
.position(new LatLng(lat,lng))
.position(new LatLng(lat, lng))
.title(title)
.sprite(sprite)
.snippet(snippet)
.getMarker();
.snippet(snippet);
}

private void addPolyline() {
Expand Down Expand Up @@ -473,7 +473,7 @@ private void addPolygon() {
}

private void removeAnnotations() {
mMapView.removeAnnotations();
mMapView.removeAllAnnotations();
}

// Called when FPS changes
Expand Down

0 comments on commit fdcef9a

Please sign in to comment.