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

Use array of structs rather than parallel arrays for annotations #1710

Merged
merged 1 commit into from
Jun 11, 2015
Merged
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
22 changes: 22 additions & 0 deletions include/mbgl/annotation/point_annotation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef MBGL_ANNOTATION_POINT_ANNOTATION
#define MBGL_ANNOTATION_POINT_ANNOTATION

#include <mbgl/util/geo.hpp>

#include <string>

namespace mbgl {

class PointAnnotation {
public:
inline PointAnnotation(const LatLng& position_, const std::string& icon_ = "")
: position(position_), icon(icon_) {
}

const LatLng position;
const std::string icon;
};

}

#endif
6 changes: 3 additions & 3 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class View;
class MapData;
class MapContext;
class StillImage;
class PointAnnotation;

namespace util {
template <class T> class Thread;
Expand Down Expand Up @@ -111,9 +112,8 @@ class Map : private util::noncopyable {
// Annotations
void setDefaultPointAnnotationSymbol(const std::string&);
double getTopOffsetPixelsForAnnotationSymbol(const std::string&);
uint32_t addPointAnnotation(const LatLng&, const std::string& symbol);
std::vector<uint32_t> addPointAnnotations(const std::vector<LatLng>&,
const std::vector<std::string>& symbols);
uint32_t addPointAnnotation(const PointAnnotation&);
std::vector<uint32_t> addPointAnnotations(const std::vector<PointAnnotation>&);
void removeAnnotation(uint32_t);
void removeAnnotations(const std::vector<uint32_t>&);
std::vector<uint32_t> getAnnotationsInBounds(const LatLngBounds&);
Expand Down
9 changes: 4 additions & 5 deletions platform/default/glfw_view.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <mbgl/annotation/point_annotation.hpp>
#include <mbgl/platform/default/glfw_view.hpp>
#include <mbgl/platform/gl.hpp>
#include <mbgl/platform/log.hpp>
Expand Down Expand Up @@ -130,8 +131,7 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action,
}

void GLFWView::addRandomPointAnnotations(int count) {
std::vector<mbgl::LatLng> points;
std::vector<std::string> markers;
std::vector<mbgl::PointAnnotation> points;

const auto sw = map->latLngForPixel({ 0, 0 });
const auto ne = map->latLngForPixel({ double(width), double(height) });
Expand All @@ -140,11 +140,10 @@ void GLFWView::addRandomPointAnnotations(int count) {
const double lon = sw.longitude + (ne.longitude - sw.longitude) * (double(std::rand()) / RAND_MAX);
const double lat = sw.latitude + (ne.latitude - sw.latitude) * (double(std::rand()) / RAND_MAX);

points.push_back({ lat, lon });
markers.push_back("default_marker");
points.emplace_back(mbgl::LatLng{ lat, lon }, "default_marker");
}

map->addPointAnnotations(points, markers);
map->addPointAnnotations(points);
}

void GLFWView::onScroll(GLFWwindow *window, double /*xOffset*/, double yOffset) {
Expand Down
14 changes: 5 additions & 9 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import <OpenGLES/EAGL.h>

#include <mbgl/mbgl.hpp>
#include <mbgl/annotation/point_annotation.hpp>
#include <mbgl/platform/platform.hpp>
#include <mbgl/platform/darwin/reachability.h>
#include <mbgl/storage/default_file_source.hpp>
Expand Down Expand Up @@ -1669,31 +1670,26 @@ - (void)addAnnotations:(NSArray *)annotations
{
if ( ! annotations) return;

std::vector<mbgl::LatLng> latLngs;
latLngs.reserve(annotations.count);

std::vector<std::string> symbols;
symbols.reserve(annotations.count);
std::vector<mbgl::PointAnnotation> points;
points.reserve(annotations.count);

BOOL delegateImplementsSymbolLookup = [self.delegate respondsToSelector:@selector(mapView:symbolNameForAnnotation:)];

for (id <MGLAnnotation> annotation in annotations)
{
assert([annotation conformsToProtocol:@protocol(MGLAnnotation)]);

latLngs.push_back(MGLLatLngFromLocationCoordinate2D(annotation.coordinate));

NSString *symbolName = nil;

if (delegateImplementsSymbolLookup)
{
symbolName = [self.delegate mapView:self symbolNameForAnnotation:annotation];
}

symbols.push_back((symbolName ? [symbolName UTF8String] : ""));
points.emplace_back(MGLLatLngFromLocationCoordinate2D(annotation.coordinate), (symbolName ? [symbolName UTF8String] : ""));
}

std::vector<uint32_t> annotationIDs = _mbglMap->addPointAnnotations(latLngs, symbols);
std::vector<uint32_t> annotationIDs = _mbglMap->addPointAnnotations(points);

for (size_t i = 0; i < annotationIDs.size(); ++i)
{
Expand Down
11 changes: 5 additions & 6 deletions src/mbgl/map/annotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ vec2<double> AnnotationManager::projectPoint(const LatLng& point) {
}

std::pair<std::vector<TileID>, AnnotationIDs>
AnnotationManager::addPointAnnotations(const std::vector<LatLng>& points,
const std::vector<std::string>& symbols,
AnnotationManager::addPointAnnotations(const std::vector<PointAnnotation>& points,
const MapData& data) {
std::lock_guard<std::mutex> lock(mtx);

Expand All @@ -103,22 +102,22 @@ AnnotationManager::addPointAnnotations(const std::vector<LatLng>& points,

std::vector<TileID> affectedTiles;

for (size_t i = 0; i < points.size(); ++i) {
for (const PointAnnotation& point : points) {
const uint32_t annotationID = nextID();

// track the annotation global ID and its geometry
auto anno_it = annotations.emplace(
annotationID,
std::make_unique<Annotation>(AnnotationType::Point,
AnnotationSegments({ { points[i] } })));
AnnotationSegments({ { point.position } })));

const uint8_t maxZoom = data.transform.getMaxZoom();

// side length of map at this zoom
uint32_t z2 = 1 << maxZoom;

// projection conversion into unit space
const vec2<double> p = projectPoint(points[i]);
const vec2<double> p = projectPoint(point.position);

uint32_t x = p.x * z2;
uint32_t y = p.y * z2;
Expand All @@ -134,7 +133,7 @@ AnnotationManager::addPointAnnotations(const std::vector<LatLng>& points,

// at render time we style the annotation according to its {sprite} field
const std::map<std::string, std::string> properties = {
{ "sprite", (symbols[i].length() ? symbols[i] : defaultPointAnnotationSymbol) }
{ "sprite", (point.icon.length() ? point.icon : defaultPointAnnotationSymbol) }
};

auto feature =
Expand Down
3 changes: 2 additions & 1 deletion src/mbgl/map/annotation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define MBGL_MAP_ANNOTATIONS

#include <mbgl/map/tile_id.hpp>
#include <mbgl/annotation/point_annotation.hpp>
#include <mbgl/util/geo.hpp>
#include <mbgl/util/noncopyable.hpp>
#include <mbgl/util/vec.hpp>
Expand Down Expand Up @@ -29,7 +30,7 @@ class AnnotationManager : private util::noncopyable {

void setDefaultPointAnnotationSymbol(const std::string& symbol);
std::pair<std::vector<TileID>, AnnotationIDs> addPointAnnotations(
const std::vector<LatLng>&, const std::vector<std::string>& symbols, const MapData&);
const std::vector<PointAnnotation>&, const MapData&);
std::vector<TileID> removeAnnotations(const AnnotationIDs&, const MapData&);
AnnotationIDs getAnnotationsInBounds(const LatLngBounds&, const MapData&) const;
LatLngBounds getBoundsForAnnotations(const AnnotationIDs&) const;
Expand Down
8 changes: 4 additions & 4 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,12 @@ double Map::getTopOffsetPixelsForAnnotationSymbol(const std::string& symbol) {
return context->invokeSync<double>(&MapContext::getTopOffsetPixelsForAnnotationSymbol, symbol);
}

uint32_t Map::addPointAnnotation(const LatLng& point, const std::string& symbol) {
return addPointAnnotations({ point }, { symbol }).front();
uint32_t Map::addPointAnnotation(const PointAnnotation& annotation) {
return addPointAnnotations({ annotation }).front();
}

std::vector<uint32_t> Map::addPointAnnotations(const std::vector<LatLng>& points, const std::vector<std::string>& symbols) {
auto result = data->annotationManager.addPointAnnotations(points, symbols, *data);
std::vector<uint32_t> Map::addPointAnnotations(const std::vector<PointAnnotation>& annotations) {
auto result = data->annotationManager.addPointAnnotations(annotations, *data);
context->invoke(&MapContext::updateAnnotationTiles, result.first);
return result.second;
}
Expand Down