From 2435c1ad2603432186ee61eb4c423244a088bc51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Thu, 11 Jun 2015 13:57:33 -0400 Subject: use array of structs rather than parallel arrays for annotations --- include/mbgl/annotation/point_annotation.hpp | 22 ++++++++++++++++++++++ include/mbgl/map/map.hpp | 6 +++--- platform/default/glfw_view.cpp | 9 ++++----- platform/ios/MGLMapView.mm | 14 +++++--------- src/mbgl/map/annotation.cpp | 11 +++++------ src/mbgl/map/annotation.hpp | 3 ++- src/mbgl/map/map.cpp | 8 ++++---- 7 files changed, 45 insertions(+), 28 deletions(-) create mode 100644 include/mbgl/annotation/point_annotation.hpp diff --git a/include/mbgl/annotation/point_annotation.hpp b/include/mbgl/annotation/point_annotation.hpp new file mode 100644 index 0000000000..17b6fe5369 --- /dev/null +++ b/include/mbgl/annotation/point_annotation.hpp @@ -0,0 +1,22 @@ +#ifndef MBGL_ANNOTATION_POINT_ANNOTATION +#define MBGL_ANNOTATION_POINT_ANNOTATION + +#include + +#include + +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 diff --git a/include/mbgl/map/map.hpp b/include/mbgl/map/map.hpp index 5535dbcc91..4418bcaaa4 100644 --- a/include/mbgl/map/map.hpp +++ b/include/mbgl/map/map.hpp @@ -21,6 +21,7 @@ class View; class MapData; class MapContext; class StillImage; +class PointAnnotation; namespace util { template class Thread; @@ -111,9 +112,8 @@ public: // Annotations void setDefaultPointAnnotationSymbol(const std::string&); double getTopOffsetPixelsForAnnotationSymbol(const std::string&); - uint32_t addPointAnnotation(const LatLng&, const std::string& symbol); - std::vector addPointAnnotations(const std::vector&, - const std::vector& symbols); + uint32_t addPointAnnotation(const PointAnnotation&); + std::vector addPointAnnotations(const std::vector&); void removeAnnotation(uint32_t); void removeAnnotations(const std::vector&); std::vector getAnnotationsInBounds(const LatLngBounds&); diff --git a/platform/default/glfw_view.cpp b/platform/default/glfw_view.cpp index 0f063e5925..e9da752909 100644 --- a/platform/default/glfw_view.cpp +++ b/platform/default/glfw_view.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -130,8 +131,7 @@ void GLFWView::onKey(GLFWwindow *window, int key, int /*scancode*/, int action, } void GLFWView::addRandomPointAnnotations(int count) { - std::vector points; - std::vector markers; + std::vector points; const auto sw = map->latLngForPixel({ 0, 0 }); const auto ne = map->latLngForPixel({ double(width), double(height) }); @@ -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) { diff --git a/platform/ios/MGLMapView.mm b/platform/ios/MGLMapView.mm index f9318f067b..d4c26d6c9f 100644 --- a/platform/ios/MGLMapView.mm +++ b/platform/ios/MGLMapView.mm @@ -8,6 +8,7 @@ #import #include +#include #include #include #include @@ -1669,11 +1670,8 @@ CLLocationCoordinate2D MGLLocationCoordinate2DFromLatLng(mbgl::LatLng latLng) { if ( ! annotations) return; - std::vector latLngs; - latLngs.reserve(annotations.count); - - std::vector symbols; - symbols.reserve(annotations.count); + std::vector points; + points.reserve(annotations.count); BOOL delegateImplementsSymbolLookup = [self.delegate respondsToSelector:@selector(mapView:symbolNameForAnnotation:)]; @@ -1681,8 +1679,6 @@ CLLocationCoordinate2D MGLLocationCoordinate2DFromLatLng(mbgl::LatLng latLng) { assert([annotation conformsToProtocol:@protocol(MGLAnnotation)]); - latLngs.push_back(MGLLatLngFromLocationCoordinate2D(annotation.coordinate)); - NSString *symbolName = nil; if (delegateImplementsSymbolLookup) @@ -1690,10 +1686,10 @@ CLLocationCoordinate2D MGLLocationCoordinate2DFromLatLng(mbgl::LatLng latLng) symbolName = [self.delegate mapView:self symbolNameForAnnotation:annotation]; } - symbols.push_back((symbolName ? [symbolName UTF8String] : "")); + points.emplace_back(MGLLatLngFromLocationCoordinate2D(annotation.coordinate), (symbolName ? [symbolName UTF8String] : "")); } - std::vector annotationIDs = _mbglMap->addPointAnnotations(latLngs, symbols); + std::vector annotationIDs = _mbglMap->addPointAnnotations(points); for (size_t i = 0; i < annotationIDs.size(); ++i) { diff --git a/src/mbgl/map/annotation.cpp b/src/mbgl/map/annotation.cpp index 6df49ec647..481d19cd50 100644 --- a/src/mbgl/map/annotation.cpp +++ b/src/mbgl/map/annotation.cpp @@ -85,8 +85,7 @@ vec2 AnnotationManager::projectPoint(const LatLng& point) { } std::pair, AnnotationIDs> -AnnotationManager::addPointAnnotations(const std::vector& points, - const std::vector& symbols, +AnnotationManager::addPointAnnotations(const std::vector& points, const MapData& data) { std::lock_guard lock(mtx); @@ -103,14 +102,14 @@ AnnotationManager::addPointAnnotations(const std::vector& points, std::vector 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(AnnotationType::Point, - AnnotationSegments({ { points[i] } }))); + AnnotationSegments({ { point.position } }))); const uint8_t maxZoom = data.transform.getMaxZoom(); @@ -118,7 +117,7 @@ AnnotationManager::addPointAnnotations(const std::vector& points, uint32_t z2 = 1 << maxZoom; // projection conversion into unit space - const vec2 p = projectPoint(points[i]); + const vec2 p = projectPoint(point.position); uint32_t x = p.x * z2; uint32_t y = p.y * z2; @@ -134,7 +133,7 @@ AnnotationManager::addPointAnnotations(const std::vector& points, // at render time we style the annotation according to its {sprite} field const std::map properties = { - { "sprite", (symbols[i].length() ? symbols[i] : defaultPointAnnotationSymbol) } + { "sprite", (point.icon.length() ? point.icon : defaultPointAnnotationSymbol) } }; auto feature = diff --git a/src/mbgl/map/annotation.hpp b/src/mbgl/map/annotation.hpp index 0c9a078e57..fb78a832c9 100644 --- a/src/mbgl/map/annotation.hpp +++ b/src/mbgl/map/annotation.hpp @@ -2,6 +2,7 @@ #define MBGL_MAP_ANNOTATIONS #include +#include #include #include #include @@ -29,7 +30,7 @@ public: void setDefaultPointAnnotationSymbol(const std::string& symbol); std::pair, AnnotationIDs> addPointAnnotations( - const std::vector&, const std::vector& symbols, const MapData&); + const std::vector&, const MapData&); std::vector removeAnnotations(const AnnotationIDs&, const MapData&); AnnotationIDs getAnnotationsInBounds(const LatLngBounds&, const MapData&) const; LatLngBounds getBoundsForAnnotations(const AnnotationIDs&) const; diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 47c9472d59..70194152d9 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -235,12 +235,12 @@ double Map::getTopOffsetPixelsForAnnotationSymbol(const std::string& symbol) { return context->invokeSync(&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 Map::addPointAnnotations(const std::vector& points, const std::vector& symbols) { - auto result = data->annotationManager.addPointAnnotations(points, symbols, *data); +std::vector Map::addPointAnnotations(const std::vector& annotations) { + auto result = data->annotationManager.addPointAnnotations(annotations, *data); context->invoke(&MapContext::updateAnnotationTiles, result.first); return result.second; } -- cgit v1.2.1