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

Commit

Permalink
[all] Rationalize annotation API
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jun 2, 2016
1 parent cfd6757 commit 0fba70d
Show file tree
Hide file tree
Showing 32 changed files with 459 additions and 543 deletions.
41 changes: 41 additions & 0 deletions include/mbgl/annotation/annotation.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,52 @@
#pragma once

#include <mbgl/style/types.hpp>

#include <mbgl/util/geometry.hpp>
#include <mbgl/util/variant.hpp>

#include <cstdint>
#include <vector>
#include <string>

namespace mbgl {

using AnnotationID = uint32_t;
using AnnotationIDs = std::vector<AnnotationID>;

class SymbolAnnotation {
public:
Geometry<double> geometry;
std::string icon;
};

class LineAnnotation {
public:
Geometry<double> geometry;
float opacity = 1;
float width = 1;
Color color = {{ 0, 0, 0, 1 }};
};

class FillAnnotation {
public:
Geometry<double> geometry;
float opacity = 1;
Color color = {{ 0, 0, 0, 1 }};
Color outlineColor = {{ 0, 0, 0, -1 }};
};

// An annotation whose type and properties are sourced from a style layer.
class StyleSourcedAnnotation {
public:
Geometry<double> geometry;
std::string layerID;
};

using Annotation = variant<
SymbolAnnotation,
LineAnnotation,
FillAnnotation,
StyleSourcedAnnotation>;

} // namespace mbgl
18 changes: 0 additions & 18 deletions include/mbgl/annotation/point_annotation.hpp

This file was deleted.

37 changes: 0 additions & 37 deletions include/mbgl/annotation/shape_annotation.hpp

This file was deleted.

13 changes: 2 additions & 11 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ namespace mbgl {
class FileSource;
class View;
class SpriteImage;
class PointAnnotation;
class ShapeAnnotation;
struct CameraOptions;
struct AnimationOptions;

Expand Down Expand Up @@ -142,16 +140,9 @@ class Map : private util::noncopyable {
void removeAnnotationIcon(const std::string&);
double getTopOffsetPixelsForAnnotationIcon(const std::string&);

AnnotationID addPointAnnotation(const PointAnnotation&);
AnnotationIDs addPointAnnotations(const std::vector<PointAnnotation>&);

AnnotationID addShapeAnnotation(const ShapeAnnotation&);
AnnotationIDs addShapeAnnotations(const std::vector<ShapeAnnotation>&);

void updatePointAnnotation(AnnotationID, const PointAnnotation&);

AnnotationID addAnnotation(const Annotation&);
void updateAnnotation(AnnotationID, const Annotation&);
void removeAnnotation(AnnotationID);
void removeAnnotations(const AnnotationIDs&);

AnnotationIDs getPointAnnotationsInBounds(const LatLngBounds&);

Expand Down
1 change: 0 additions & 1 deletion include/mbgl/platform/default/glfw_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ class GLFWView : public mbgl::View {
void report(float duration);

private:
mbgl::LatLng makeRandomLatLng() const;
mbgl::Point<double> makeRandomPoint() const;
static std::shared_ptr<const mbgl::SpriteImage>
makeSpriteImage(int width, int height, float pixelRatio);
Expand Down
File renamed without changes.
79 changes: 36 additions & 43 deletions platform/android/src/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@

#include <mbgl/map/map.hpp>
#include <mbgl/map/camera.hpp>
#include <mbgl/annotation/point_annotation.hpp>
#include <mbgl/annotation/shape_annotation.hpp>
#include <mbgl/annotation/annotation.hpp>
#include <mbgl/sprite/sprite_image.hpp>
#include <mbgl/platform/event.hpp>
#include <mbgl/platform/log.hpp>
Expand Down Expand Up @@ -681,8 +680,10 @@ void nativeUpdateMarker(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr,
jdouble latitude = jni::GetField<jdouble>(*env, position, *latLngLatitudeId);
jdouble longitude = jni::GetField<jdouble>(*env, position, *latLngLongitudeId);

// Because Java only has int, not unsigned int, we need to bump the annotation id up to a long.
nativeMapView->getMap().updatePointAnnotation(markerId, mbgl::PointAnnotation(mbgl::LatLng(latitude, longitude), iconId));
nativeMapView->getMap().updateAnnotation(markerId, mbgl::SymbolAnnotation {
mbgl::Point<double>(longitude, latitude),
iconId
});
}

jni::jarray<jlong>* nativeAddMarkers(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jarray<jni::jobject>* jarray) {
Expand All @@ -693,30 +694,30 @@ jni::jarray<jlong>* nativeAddMarkers(JNIEnv *env, jni::jobject* obj, jlong nativ
NullCheck(*env, jarray);
std::size_t len = jni::GetArrayLength(*env, *jarray);

std::vector<mbgl::PointAnnotation> markers;
markers.reserve(len);
std::vector<mbgl::AnnotationID> ids;
ids.reserve(len);

for (std::size_t i = 0; i < len; i++) {
jni::jobject* marker = jni::GetObjectArrayElement(*env, *jarray, i);
jni::jobject* position = jni::GetField<jni::jobject*>(*env, marker, *markerPositionId);
jni::jobject* icon = jni::GetField<jni::jobject*>(*env, marker, *markerIconId);
jni::DeleteLocalRef(*env, marker);

jni::jstring* jid = reinterpret_cast<jni::jstring*>(jni::GetField<jni::jobject*>(*env, icon, *iconIdId));
jni::DeleteLocalRef(*env, icon);

std::string id = std_string_from_jstring(env, jid);
jni::DeleteLocalRef(*env, jid);

jdouble latitude = jni::GetField<jdouble>(*env, position, *latLngLatitudeId);
jdouble longitude = jni::GetField<jdouble>(*env, position, *latLngLongitudeId);
jni::DeleteLocalRef(*env, position);

markers.emplace_back(mbgl::PointAnnotation(mbgl::LatLng(latitude, longitude), id));
ids.push_back(nativeMapView->getMap().addAnnotation(mbgl::SymbolAnnotation {
mbgl::Point<double>(longitude, latitude),
std_string_from_jstring(env, jid)
}));

jni::DeleteLocalRef(*env, position);
jni::DeleteLocalRef(*env, jid);
jni::DeleteLocalRef(*env, icon);
jni::DeleteLocalRef(*env, marker);
}

std::vector<uint32_t> pointAnnotationIDs = nativeMapView->getMap().addPointAnnotations(markers);
return std_vector_uint_to_jobject(env, pointAnnotationIDs);
return std_vector_uint_to_jobject(env, ids);
}

static mbgl::Color toColor(jint color) {
Expand Down Expand Up @@ -762,24 +763,23 @@ jni::jarray<jlong>* nativeAddPolylines(JNIEnv *env, jni::jobject* obj, jlong nat
NullCheck(*env, jarray);
std::size_t len = jni::GetArrayLength(*env, *jarray);

std::vector<mbgl::ShapeAnnotation> shapes;
shapes.reserve(len);
std::vector<mbgl::AnnotationID> ids;
ids.reserve(len);

for (std::size_t i = 0; i < len; i++) {
jni::jobject* polyline = jni::GetObjectArrayElement(*env, *jarray, i);

mbgl::LineAnnotationProperties lineProperties;
lineProperties.opacity = jni::GetField<jfloat>(*env, polyline, *polylineAlphaId);
lineProperties.color = toColor(jni::GetField<jint>(*env, polyline, *polylineColorId));
lineProperties.width = jni::GetField<jfloat>(*env, polyline, *polylineWidthId);

jni::jobject* points = jni::GetField<jni::jobject*>(*env, polyline, *polylinePointsId);
shapes.emplace_back(toGeometry<mbgl::LineString<double>>(env, points), lineProperties);

mbgl::LineAnnotation annotation { toGeometry<mbgl::LineString<double>>(env, points) };
annotation.opacity = jni::GetField<jfloat>(*env, polyline, *polylineAlphaId);
annotation.color = toColor(jni::GetField<jint>(*env, polyline, *polylineColorId));
annotation.width = jni::GetField<jfloat>(*env, polyline, *polylineWidthId);
ids.push_back(nativeMapView->getMap().addAnnotation(annotation));

jni::DeleteLocalRef(*env, polyline);
}

return std_vector_uint_to_jobject(env, nativeMapView->getMap().addShapeAnnotations(shapes));
return std_vector_uint_to_jobject(env, ids);
}

jni::jarray<jlong>* nativeAddPolygons(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jarray<jni::jobject>* jarray) {
Expand All @@ -790,24 +790,23 @@ jni::jarray<jlong>* nativeAddPolygons(JNIEnv *env, jni::jobject* obj, jlong nati
NullCheck(*env, jarray);
std::size_t len = jni::GetArrayLength(*env, *jarray);

std::vector<mbgl::ShapeAnnotation> shapes;
shapes.reserve(len);
std::vector<mbgl::AnnotationID> ids;
ids.reserve(len);

for (std::size_t i = 0; i < len; i++) {
jni::jobject* polygon = jni::GetObjectArrayElement(*env, *jarray, i);

mbgl::FillAnnotationProperties fillProperties;
fillProperties.opacity = jni::GetField<jfloat>(*env, polygon, *polygonAlphaId);
fillProperties.outlineColor = toColor(jni::GetField<jint>(*env, polygon, *polygonStrokeColorId));
fillProperties.color = toColor(jni::GetField<jint>(*env, polygon, *polygonFillColorId));

jni::jobject* points = jni::GetField<jni::jobject*>(*env, polygon, *polygonPointsId);
shapes.emplace_back(mbgl::Polygon<double> { toGeometry<mbgl::LinearRing<double>>(env, points) }, fillProperties);

mbgl::FillAnnotation annotation { mbgl::Polygon<double> { toGeometry<mbgl::LinearRing<double>>(env, points) } };
annotation.opacity = jni::GetField<jfloat>(*env, polygon, *polygonAlphaId);
annotation.outlineColor = toColor(jni::GetField<jint>(*env, polygon, *polygonStrokeColorId));
annotation.color = toColor(jni::GetField<jint>(*env, polygon, *polygonFillColorId));
ids.push_back(nativeMapView->getMap().addAnnotation(annotation));

jni::DeleteLocalRef(*env, polygon);
}

return std_vector_uint_to_jobject(env, nativeMapView->getMap().addShapeAnnotations(shapes));
return std_vector_uint_to_jobject(env, ids);
}

void nativeRemoveAnnotations(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jarray<jlong>* jarray) {
Expand All @@ -817,20 +816,14 @@ void nativeRemoveAnnotations(JNIEnv *env, jni::jobject* obj, jlong nativeMapView

NullCheck(*env, jarray);
std::size_t len = jni::GetArrayLength(*env, *jarray);

std::vector<uint32_t> ids;
ids.reserve(len);

auto elements = jni::GetArrayElements(*env, *jarray);
jlong* jids = std::get<0>(elements).get();

for (std::size_t i = 0; i < len; i++) {
if(jids[i] == -1L)
continue;
ids.push_back(static_cast<uint32_t>(jids[i]));
nativeMapView->getMap().removeAnnotation(jids[i]);
}

nativeMapView->getMap().removeAnnotations(ids);
}

jni::jarray<jlong>* nativeGetAnnotationsInBounds(JNIEnv *env, jni::jobject* obj, jlong nativeMapViewPtr, jni::jobject* latLngBounds_) {
Expand Down
5 changes: 5 additions & 0 deletions platform/darwin/src/MGLGeometry_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#endif

#import <mbgl/util/geo.hpp>
#import <mbgl/util/geometry.hpp>

/// Returns the smallest rectangle that contains both the given rectangle and
/// the given point.
Expand All @@ -15,6 +16,10 @@ NS_INLINE mbgl::LatLng MGLLatLngFromLocationCoordinate2D(CLLocationCoordinate2D
return mbgl::LatLng(coordinate.latitude, coordinate.longitude);
}

NS_INLINE mbgl::Point<double> MGLPointFromLocationCoordinate2D(CLLocationCoordinate2D coordinate) {
return mbgl::Point<double>(coordinate.longitude, coordinate.latitude);
}

NS_INLINE CLLocationCoordinate2D MGLLocationCoordinate2DFromLatLng(mbgl::LatLng latLng) {
return CLLocationCoordinate2DMake(latLng.latitude, latLng.longitude);
}
Expand Down
4 changes: 2 additions & 2 deletions platform/darwin/src/MGLMultiPoint_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#import "MGLGeometry.h"
#import "MGLTypes.h"

#import <mbgl/annotation/shape_annotation.hpp>
#import <mbgl/annotation/annotation.hpp>
#import <vector>

#import <CoreGraphics/CoreGraphics.h>
Expand All @@ -22,7 +22,7 @@ NS_ASSUME_NONNULL_BEGIN
- (BOOL)intersectsOverlayBounds:(MGLCoordinateBounds)overlayBounds;

/** Constructs a shape annotation object, asking the delegate for style values. */
- (mbgl::ShapeAnnotation)shapeAnnotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate;
- (mbgl::Annotation)annotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate;

@end

Expand Down
14 changes: 7 additions & 7 deletions platform/darwin/src/MGLPolygon.mm
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ - (instancetype)initWithCoordinates:(CLLocationCoordinate2D *)coords count:(NSUI
return result;
}

- (mbgl::ShapeAnnotation)shapeAnnotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate {
mbgl::FillAnnotationProperties fillProperties;
fillProperties.opacity = [delegate alphaForShapeAnnotation:self];
fillProperties.outlineColor = [delegate strokeColorForShapeAnnotation:self];
fillProperties.color = [delegate fillColorForPolygonAnnotation:self];

- (mbgl::Annotation)annotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate {
mbgl::Polygon<double> geometry;
geometry.push_back(self.ring);
for (MGLPolygon *polygon in self.interiorPolygons) {
geometry.push_back(polygon.ring);
}

return mbgl::ShapeAnnotation(geometry, fillProperties);
mbgl::FillAnnotation annotation { geometry };
annotation.opacity = [delegate alphaForShapeAnnotation:self];
annotation.outlineColor = [delegate strokeColorForShapeAnnotation:self];
annotation.color = [delegate fillColorForPolygonAnnotation:self];

return annotation;
}

@end
Expand Down
14 changes: 7 additions & 7 deletions platform/darwin/src/MGLPolyline.mm
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ + (instancetype)polylineWithCoordinates:(CLLocationCoordinate2D *)coords
return [[self alloc] initWithCoordinates:coords count:count];
}

- (mbgl::ShapeAnnotation)shapeAnnotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate {
mbgl::LineAnnotationProperties lineProperties;
lineProperties.opacity = [delegate alphaForShapeAnnotation:self];
lineProperties.color = [delegate strokeColorForShapeAnnotation:self];
lineProperties.width = [delegate lineWidthForPolylineAnnotation:self];

- (mbgl::Annotation)annotationObjectWithDelegate:(id <MGLMultiPointDelegate>)delegate {
NSUInteger count = self.pointCount;
CLLocationCoordinate2D *coordinates = self.coordinates;

Expand All @@ -28,7 +23,12 @@ + (instancetype)polylineWithCoordinates:(CLLocationCoordinate2D *)coords
geometry.push_back(mbgl::Point<double>(coordinates[i].longitude, coordinates[i].latitude));
}

return mbgl::ShapeAnnotation(geometry, lineProperties);
mbgl::LineAnnotation annotation { geometry };
annotation.opacity = [delegate alphaForShapeAnnotation:self];
annotation.color = [delegate strokeColorForShapeAnnotation:self];
annotation.width = [delegate lineWidthForPolylineAnnotation:self];

return annotation;
}

@end
Expand Down
Loading

0 comments on commit 0fba70d

Please sign in to comment.