Merge branch 'master' of github.com:mapbox/mapbox-gl-native into 1033-metrics
Conflicts: platform/ios/MGLMapView.mm
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
#define MBGL_MAP_ANNOTATIONS
|
||||
|
||||
#include <mbgl/map/tile.hpp>
|
||||
#include <mbgl/map/live_tile.hpp>
|
||||
#include <mbgl/util/geo.hpp>
|
||||
#include <mbgl/util/noncopyable.hpp>
|
||||
#include <mbgl/util/std.hpp>
|
||||
@@ -10,63 +9,47 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
class Annotation;
|
||||
class Map;
|
||||
class LiveTile;
|
||||
|
||||
typedef std::vector<LatLng> AnnotationSegment;
|
||||
|
||||
enum class AnnotationType : uint8_t {
|
||||
Point,
|
||||
Shape
|
||||
};
|
||||
using AnnotationIDs = std::vector<uint32_t>;
|
||||
|
||||
class AnnotationManager : private util::noncopyable {
|
||||
public:
|
||||
AnnotationManager();
|
||||
~AnnotationManager();
|
||||
|
||||
void setDefaultPointAnnotationSymbol(std::string& symbol) { defaultPointAnnotationSymbol = symbol; }
|
||||
std::pair<std::vector<Tile::ID>, std::vector<uint32_t>> addPointAnnotations(std::vector<LatLng>, std::vector<std::string>& symbols, const Map&);
|
||||
std::vector<Tile::ID> removeAnnotations(std::vector<uint32_t>);
|
||||
std::vector<uint32_t> getAnnotationsInBounds(LatLngBounds, const Map&) const;
|
||||
LatLngBounds getBoundsForAnnotations(std::vector<uint32_t>) const;
|
||||
void setDefaultPointAnnotationSymbol(const std::string& symbol);
|
||||
std::pair<std::vector<Tile::ID>, AnnotationIDs> addPointAnnotations(
|
||||
const std::vector<LatLng>&, const std::vector<std::string>& symbols, const Map&);
|
||||
std::vector<Tile::ID> removeAnnotations(const AnnotationIDs&, const Map&);
|
||||
AnnotationIDs getAnnotationsInBounds(const LatLngBounds&, const Map&) const;
|
||||
LatLngBounds getBoundsForAnnotations(const AnnotationIDs&) const;
|
||||
|
||||
const std::unique_ptr<LiveTile>& getTile(Tile::ID const& id);
|
||||
const LiveTile* getTile(Tile::ID const& id);
|
||||
|
||||
static const std::string layerID;
|
||||
|
||||
private:
|
||||
uint32_t nextID() { return nextID_++; }
|
||||
static vec2<double> projectPoint(LatLng& point);
|
||||
inline uint32_t nextID();
|
||||
static vec2<double> projectPoint(const LatLng& point);
|
||||
|
||||
private:
|
||||
std::mutex mtx;
|
||||
mutable std::mutex mtx;
|
||||
std::string defaultPointAnnotationSymbol;
|
||||
std::map<uint32_t, std::unique_ptr<Annotation>> annotations;
|
||||
std::map<Tile::ID, std::pair<std::vector<uint32_t>, std::unique_ptr<LiveTile>>> annotationTiles;
|
||||
std::unique_ptr<LiveTile> nullTile;
|
||||
std::unordered_map<uint32_t, std::unique_ptr<Annotation>> annotations;
|
||||
std::unordered_map<Tile::ID, std::pair<std::unordered_set<uint32_t>, std::unique_ptr<LiveTile>>, Tile::ID::Hash> tiles;
|
||||
uint32_t nextID_ = 0;
|
||||
};
|
||||
|
||||
class Annotation : private util::noncopyable {
|
||||
friend class AnnotationManager;
|
||||
public:
|
||||
Annotation(AnnotationType, std::vector<AnnotationSegment>);
|
||||
|
||||
private:
|
||||
LatLng getPoint() const;
|
||||
LatLngBounds getBounds() const { return bounds; }
|
||||
|
||||
private:
|
||||
const AnnotationType type = AnnotationType::Point;
|
||||
const std::vector<AnnotationSegment> geometry;
|
||||
std::map<Tile::ID, std::vector<std::weak_ptr<const LiveTileFeature>>> tileFeatures;
|
||||
LatLngBounds bounds;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -152,13 +152,14 @@ public:
|
||||
inline const LatLng latLngForPixel(const vec2<double> pixel) const { return state.latLngForPixel(pixel); }
|
||||
|
||||
// Annotations
|
||||
void setDefaultPointAnnotationSymbol(std::string&);
|
||||
uint32_t addPointAnnotation(LatLng, std::string& symbol);
|
||||
std::vector<uint32_t> addPointAnnotations(std::vector<LatLng>, std::vector<std::string>& symbols);
|
||||
void setDefaultPointAnnotationSymbol(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);
|
||||
void removeAnnotation(uint32_t);
|
||||
void removeAnnotations(std::vector<uint32_t>);
|
||||
std::vector<uint32_t> getAnnotationsInBounds(LatLngBounds) const;
|
||||
LatLngBounds getBoundsForAnnotations(std::vector<uint32_t>) const;
|
||||
void removeAnnotations(const std::vector<uint32_t>&);
|
||||
std::vector<uint32_t> getAnnotationsInBounds(const LatLngBounds&) const;
|
||||
LatLngBounds getBoundsForAnnotations(const std::vector<uint32_t>&) const;
|
||||
|
||||
// Debug
|
||||
void setDebug(bool value);
|
||||
@@ -198,7 +199,7 @@ private:
|
||||
// the stylesheet.
|
||||
void prepare();
|
||||
|
||||
void updateAnnotationTiles(std::vector<Tile::ID>&);
|
||||
void updateAnnotationTiles(const std::vector<Tile::ID>&);
|
||||
|
||||
enum class Mode : uint8_t {
|
||||
None, // we're not doing any processing
|
||||
@@ -248,7 +249,7 @@ private:
|
||||
std::unique_ptr<LineAtlas> lineAtlas;
|
||||
util::ptr<TexturePool> texturePool;
|
||||
std::unique_ptr<Painter> painter;
|
||||
util::ptr<AnnotationManager> annotationManager;
|
||||
std::unique_ptr<AnnotationManager> annotationManager;
|
||||
|
||||
const std::unique_ptr<MapData> data;
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <forward_list>
|
||||
#include <iosfwd>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
@@ -44,6 +45,12 @@ public:
|
||||
return ((std::pow(2, z) * y + x) * 32) + z;
|
||||
}
|
||||
|
||||
struct Hash {
|
||||
std::size_t operator()(ID const& i) const {
|
||||
return std::hash<uint64_t>()(i.to_uint64());
|
||||
}
|
||||
};
|
||||
|
||||
inline bool operator==(const ID& rhs) const {
|
||||
return w == rhs.w && z == rhs.z && x == rhs.x && y == rhs.y;
|
||||
}
|
||||
|
||||
@@ -16,8 +16,6 @@ extern const double M2PI;
|
||||
extern const double EARTH_RADIUS_M;
|
||||
extern const double LATITUDE_MAX;
|
||||
|
||||
extern const std::string ANNOTATIONS_POINTS_LAYER_ID;
|
||||
|
||||
}
|
||||
|
||||
namespace debug {
|
||||
|
||||
12
ios/app/MBXAnnotation.h
Normal file
12
ios/app/MBXAnnotation.h
Normal file
@@ -0,0 +1,12 @@
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <CoreLocation/CoreLocation.h>
|
||||
|
||||
#import <mbgl/ios/MGLAnnotation.h>
|
||||
|
||||
@interface MBXAnnotation : NSObject <MGLAnnotation>
|
||||
|
||||
+ (instancetype)annotationWithLocation:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle;
|
||||
|
||||
- (instancetype)initWithLocation:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle;
|
||||
|
||||
@end
|
||||
30
ios/app/MBXAnnotation.m
Normal file
30
ios/app/MBXAnnotation.m
Normal file
@@ -0,0 +1,30 @@
|
||||
#import "MBXAnnotation.h"
|
||||
|
||||
@interface MBXAnnotation ()
|
||||
|
||||
@property (nonatomic) CLLocationCoordinate2D coordinate;
|
||||
@property (nonatomic) NSString *title;
|
||||
@property (nonatomic) NSString *subtitle;
|
||||
|
||||
@end
|
||||
|
||||
@implementation MBXAnnotation
|
||||
|
||||
+ (instancetype)annotationWithLocation:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle
|
||||
{
|
||||
return [[self alloc] initWithLocation:coordinate title:title subtitle:subtitle];
|
||||
}
|
||||
|
||||
- (instancetype)initWithLocation:(CLLocationCoordinate2D)coordinate title:(NSString *)title subtitle:(NSString *)subtitle
|
||||
{
|
||||
if (self = [super init])
|
||||
{
|
||||
_coordinate = coordinate;
|
||||
_title = title;
|
||||
_subtitle = subtitle;
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
@end
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
#import <CoreLocation/CoreLocation.h>
|
||||
|
||||
#import "MBXAnnotation.h"
|
||||
|
||||
static UIColor *const kTintColor = [UIColor colorWithRed:0.120 green:0.550 blue:0.670 alpha:1.000];
|
||||
|
||||
static NSArray *const kStyleNames = @[
|
||||
@@ -134,7 +136,14 @@ mbgl::Settings_NSUserDefaults *settings = nullptr;
|
||||
delegate:self
|
||||
cancelButtonTitle:@"Cancel"
|
||||
destructiveButtonTitle:nil
|
||||
otherButtonTitles:@"Reset North", @"Reset Position", @"Toggle Debug", nil];
|
||||
otherButtonTitles:@"Reset North",
|
||||
@"Reset Position",
|
||||
@"Toggle Debug",
|
||||
@"Add 100 Points",
|
||||
@"Add 1,000 Points",
|
||||
@"Add 10,000 Points",
|
||||
@"Remove Points",
|
||||
nil];
|
||||
|
||||
[sheet showFromBarButtonItem:self.navigationItem.leftBarButtonItem animated:YES];
|
||||
}
|
||||
@@ -153,6 +162,67 @@ mbgl::Settings_NSUserDefaults *settings = nullptr;
|
||||
{
|
||||
[self.mapView toggleDebug];
|
||||
}
|
||||
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 3)
|
||||
{
|
||||
[self parseFeaturesAddingCount:100];
|
||||
}
|
||||
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 4)
|
||||
{
|
||||
[self parseFeaturesAddingCount:1000];
|
||||
}
|
||||
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 5)
|
||||
{
|
||||
[self parseFeaturesAddingCount:10000];
|
||||
}
|
||||
else if (buttonIndex == actionSheet.firstOtherButtonIndex + 6)
|
||||
{
|
||||
[self.mapView removeAnnotations:self.mapView.annotations];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)parseFeaturesAddingCount:(NSUInteger)featuresCount
|
||||
{
|
||||
[self.mapView removeAnnotations:self.mapView.annotations];
|
||||
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
|
||||
{
|
||||
NSData *featuresData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"features" ofType:@"geojson"]];
|
||||
|
||||
id features = [NSJSONSerialization JSONObjectWithData:featuresData
|
||||
options:0
|
||||
error:nil];
|
||||
|
||||
if ([features isKindOfClass:[NSDictionary class]])
|
||||
{
|
||||
NSMutableArray *annotations = [NSMutableArray array];
|
||||
|
||||
for (NSDictionary *feature in features[@"features"])
|
||||
{
|
||||
CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([feature[@"geometry"][@"coordinates"][1] doubleValue],
|
||||
[feature[@"geometry"][@"coordinates"][0] doubleValue]);
|
||||
NSString *title = feature[@"properties"][@"NAME"];
|
||||
|
||||
MBXAnnotation *annotation = [MBXAnnotation annotationWithLocation:coordinate
|
||||
title:title
|
||||
subtitle:nil];
|
||||
|
||||
[annotations addObject:annotation];
|
||||
|
||||
if (annotations.count == featuresCount) break;
|
||||
}
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^
|
||||
{
|
||||
[self.mapView addAnnotations:annotations];
|
||||
|
||||
[self.mapView setCenterCoordinate:CLLocationCoordinate2DMake(38.904722, -77.016389)
|
||||
zoomLevel:10
|
||||
animated:NO];
|
||||
|
||||
[self.mapView setDirection:0];
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
- (void)cycleStyles
|
||||
|
||||
136346
ios/app/features.geojson
Normal file
136346
ios/app/features.geojson
Normal file
File diff suppressed because it is too large
Load Diff
@@ -10,6 +10,7 @@
|
||||
'mac_bundle': 1,
|
||||
'mac_bundle_resources': [
|
||||
'<!@(find ./img -type f)',
|
||||
'./features.geojson'
|
||||
],
|
||||
|
||||
'dependencies': [
|
||||
@@ -27,6 +28,8 @@
|
||||
'./MBXAppDelegate.m',
|
||||
'./MBXViewController.h',
|
||||
'./MBXViewController.mm',
|
||||
'./MBXAnnotation.h',
|
||||
'./MBXAnnotation.m',
|
||||
'../../platform/darwin/settings_nsuserdefaults.mm',
|
||||
],
|
||||
|
||||
|
||||
@@ -195,17 +195,13 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ([@(mbglMap->getStyleJSON().c_str()) length]) mbglMap->stop();
|
||||
mbglMap->setStyleJSON((std::string)[styleJSON cStringUsingEncoding:[NSString defaultCStringEncoding]]);
|
||||
mbglMap->start();
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setStyleURL:(NSString *)filePathURL
|
||||
{
|
||||
if ([@(mbglMap->getStyleJSON().c_str()) length]) mbglMap->stop();
|
||||
mbglMap->setStyleURL(std::string("asset://") + [filePathURL UTF8String]);
|
||||
mbglMap->start();
|
||||
}
|
||||
|
||||
- (BOOL)commonInit
|
||||
@@ -387,6 +383,9 @@ mbgl::DefaultFileSource *mbglFileSource = nullptr;
|
||||
_regionChangeDelegateQueue = [NSOperationQueue new];
|
||||
_regionChangeDelegateQueue.maxConcurrentOperationCount = 1;
|
||||
|
||||
// start the main loop
|
||||
mbglMap->start();
|
||||
|
||||
|
||||
// Fire map.load on a background thread
|
||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
||||
@@ -1723,7 +1722,7 @@ CLLocationCoordinate2D latLngToCoordinate(mbgl::LatLng latLng)
|
||||
{
|
||||
assert([annotation conformsToProtocol:@protocol(MGLAnnotation)]);
|
||||
|
||||
annotationIDsToRemove.push_back([[self.annotationsStore objectForKey:annotation] unsignedIntValue]);
|
||||
annotationIDsToRemove.push_back([[[self.annotationsStore objectForKey:annotation] objectForKey:MGLAnnotationIDKey] unsignedIntValue]);
|
||||
[self.annotationsStore removeObjectForKey:annotation];
|
||||
}
|
||||
|
||||
|
||||
@@ -1,60 +1,120 @@
|
||||
#include <mbgl/map/annotation.hpp>
|
||||
#include <mbgl/map/map.hpp>
|
||||
#include <mbgl/map/live_tile.hpp>
|
||||
#include <mbgl/util/ptr.hpp>
|
||||
#include <mbgl/util/std.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <memory>
|
||||
|
||||
using namespace mbgl;
|
||||
namespace mbgl {
|
||||
|
||||
Annotation::Annotation(AnnotationType type_, std::vector<AnnotationSegment> geometry_)
|
||||
enum class AnnotationType : uint8_t {
|
||||
Point,
|
||||
Shape
|
||||
};
|
||||
|
||||
using AnnotationSegment = std::vector<LatLng>;
|
||||
using AnnotationSegments = std::vector<AnnotationSegment>;
|
||||
|
||||
class Annotation : private util::noncopyable {
|
||||
friend class AnnotationManager;
|
||||
public:
|
||||
Annotation(AnnotationType, const AnnotationSegments&);
|
||||
|
||||
private:
|
||||
LatLng getPoint() const;
|
||||
LatLngBounds getBounds() const { return bounds; }
|
||||
|
||||
private:
|
||||
const AnnotationType type = AnnotationType::Point;
|
||||
const AnnotationSegments geometry;
|
||||
std::unordered_map<Tile::ID, std::weak_ptr<const LiveTileFeature>, Tile::ID::Hash> tileFeatures;
|
||||
const LatLngBounds bounds;
|
||||
};
|
||||
|
||||
Annotation::Annotation(AnnotationType type_, const AnnotationSegments& geometry_)
|
||||
: type(type_),
|
||||
geometry(geometry_) {
|
||||
if (type == AnnotationType::Point) {
|
||||
bounds = LatLngBounds(getPoint(), getPoint());
|
||||
} else {
|
||||
for (auto segment : geometry) {
|
||||
for (auto point : segment) {
|
||||
bounds.extend(point);
|
||||
}
|
||||
}
|
||||
}
|
||||
geometry(geometry_),
|
||||
bounds([this] {
|
||||
LatLngBounds bounds_;
|
||||
if (type == AnnotationType::Point) {
|
||||
bounds_ = { getPoint(), getPoint() };
|
||||
} else {
|
||||
for (auto& segment : geometry) {
|
||||
for (auto& point : segment) {
|
||||
bounds_.extend(point);
|
||||
}
|
||||
}
|
||||
}
|
||||
return bounds_;
|
||||
}()) {
|
||||
}
|
||||
|
||||
LatLng Annotation::getPoint() const {
|
||||
// Return the first line's first point. Shortcut for point annotations.
|
||||
assert(!geometry.empty());
|
||||
assert(!geometry[0].empty());
|
||||
return geometry[0][0];
|
||||
}
|
||||
|
||||
AnnotationManager::AnnotationManager()
|
||||
: nullTile(util::make_unique<LiveTile>()) {}
|
||||
AnnotationManager::AnnotationManager() {}
|
||||
|
||||
vec2<double> AnnotationManager::projectPoint(LatLng& point) {
|
||||
double sine = std::sin(point.latitude * M_PI / 180);
|
||||
double x = point.longitude / 360 + 0.5;
|
||||
double y = 0.5 - 0.25 * std::log((1 + sine) / (1 - sine)) / M_PI;
|
||||
return vec2<double>(x, y);
|
||||
AnnotationManager::~AnnotationManager() {
|
||||
// leave this here because the header file doesn't have a definition of
|
||||
// Annotation so we can't destruct the object with just the header file.
|
||||
}
|
||||
|
||||
std::pair<std::vector<Tile::ID>, std::vector<uint32_t>> AnnotationManager::addPointAnnotations(std::vector<LatLng> points, std::vector<std::string>& symbols, const Map& map) {
|
||||
void AnnotationManager::setDefaultPointAnnotationSymbol(const std::string& symbol) {
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
defaultPointAnnotationSymbol = symbol;
|
||||
}
|
||||
|
||||
uint16_t extent = 4096;
|
||||
uint32_t AnnotationManager::nextID() {
|
||||
return nextID_++;
|
||||
}
|
||||
|
||||
vec2<double> AnnotationManager::projectPoint(const LatLng& point) {
|
||||
// Project a coordinate into unit space in a square map.
|
||||
const double sine = std::sin(point.latitude * M_PI / 180.0);
|
||||
const double x = point.longitude / 360.0 + 0.5;
|
||||
const double y = 0.5 - 0.25 * std::log((1.0 + sine) / (1.0 - sine)) / M_PI;
|
||||
return { x, y };
|
||||
}
|
||||
|
||||
std::pair<std::vector<Tile::ID>, AnnotationIDs> AnnotationManager::addPointAnnotations(
|
||||
const std::vector<LatLng>& points, const std::vector<std::string>& symbols, const Map& map) {
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
|
||||
// We pre-generate tiles to contain each annotation up to the map's max zoom.
|
||||
// We do this for fast rendering without projection conversions on the fly, as well as
|
||||
// to simplify bounding box queries of annotations later. Tiles get invalidated when
|
||||
// annotations are added or removed in order to refresh the map render without
|
||||
// touching the base map underneath.
|
||||
|
||||
const uint16_t extent = 4096;
|
||||
|
||||
std::vector<uint32_t> annotationIDs;
|
||||
annotationIDs.reserve(points.size());
|
||||
|
||||
std::vector<Tile::ID> affectedTiles;
|
||||
|
||||
for (uint32_t i = 0; i < points.size(); ++i) {
|
||||
uint32_t annotationID = nextID();
|
||||
for (size_t i = 0; i < points.size(); ++i) {
|
||||
const uint32_t annotationID = nextID();
|
||||
|
||||
auto anno_it = annotations.emplace(annotationID, util::make_unique<Annotation>(AnnotationType::Point, std::vector<AnnotationSegment>({{ points[i] }})));
|
||||
// track the annotation global ID and its geometry
|
||||
auto anno_it = annotations.emplace(
|
||||
annotationID,
|
||||
util::make_unique<Annotation>(AnnotationType::Point,
|
||||
AnnotationSegments({ { points[i] } })));
|
||||
|
||||
uint8_t maxZoom = map.getMaxZoom();
|
||||
const uint8_t maxZoom = map.getMaxZoom();
|
||||
|
||||
// side length of map at this zoom
|
||||
uint32_t z2 = 1 << maxZoom;
|
||||
|
||||
vec2<double> p = projectPoint(points[i]);
|
||||
// projection conversion into unit space
|
||||
const vec2<double> p = projectPoint(points[i]);
|
||||
|
||||
uint32_t x = p.x * z2;
|
||||
uint32_t y = p.y * z2;
|
||||
@@ -63,36 +123,51 @@ std::pair<std::vector<Tile::ID>, std::vector<uint32_t>> AnnotationManager::addPo
|
||||
affectedTiles.emplace_back(z, x, y);
|
||||
Tile::ID tileID = affectedTiles.back();
|
||||
|
||||
Coordinate coordinate(extent * (p.x * z2 - x), extent * (p.y * z2 - y));
|
||||
// calculate tile coordinate
|
||||
const Coordinate coordinate(extent * (p.x * z2 - x), extent * (p.y * z2 - y));
|
||||
|
||||
GeometryCollection geometries({{ {{ coordinate }} }});
|
||||
const GeometryCollection geometries({ { { { coordinate } } } });
|
||||
|
||||
std::map<std::string, std::string> properties = {{ "sprite", (symbols[i].length() ? symbols[i] : defaultPointAnnotationSymbol) }};
|
||||
// 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) }
|
||||
};
|
||||
|
||||
auto feature = std::make_shared<const LiveTileFeature>(FeatureType::Point,
|
||||
geometries,
|
||||
properties);
|
||||
auto feature =
|
||||
std::make_shared<const LiveTileFeature>(FeatureType::Point, geometries, properties);
|
||||
|
||||
auto tile_it = annotationTiles.find(tileID);
|
||||
if (tile_it != annotationTiles.end()) {
|
||||
auto tile_it = tiles.find(tileID);
|
||||
if (tile_it != tiles.end()) {
|
||||
//
|
||||
// We have this tile created already. Add this feature to it.
|
||||
//
|
||||
// get point layer & add feature
|
||||
auto layer = tile_it->second.second->getMutableLayer(util::ANNOTATIONS_POINTS_LAYER_ID);
|
||||
auto layer =
|
||||
tile_it->second.second->getMutableLayer(layerID);
|
||||
layer->addFeature(feature);
|
||||
// record annotation association with tile
|
||||
tile_it->second.first.push_back(annotationID);
|
||||
tile_it->second.first.insert(annotationID);
|
||||
} else {
|
||||
//
|
||||
// We need to create a new tile for this feature.
|
||||
//
|
||||
// create point layer & add feature
|
||||
util::ptr<LiveTileLayer> layer = std::make_shared<LiveTileLayer>();
|
||||
layer->addFeature(feature);
|
||||
// create tile & record annotation association
|
||||
auto tile_pos = annotationTiles.emplace(tileID, std::make_pair(std::vector<uint32_t>({ annotationID }), util::make_unique<LiveTile>()));
|
||||
auto tile_pos = tiles.emplace(
|
||||
tileID, std::make_pair(std::unordered_set<uint32_t>({ annotationID }),
|
||||
util::make_unique<LiveTile>()));
|
||||
// add point layer to tile
|
||||
tile_pos.first->second.second->addLayer(util::ANNOTATIONS_POINTS_LAYER_ID, layer);
|
||||
tile_pos.first->second.second->addLayer(layerID, layer);
|
||||
}
|
||||
|
||||
// record annotation association with tile feature
|
||||
anno_it.first->second->tileFeatures.emplace(tileID, std::vector<std::weak_ptr<const LiveTileFeature>>({ feature }));
|
||||
// Record annotation association with tile and tile feature. This is used to determine stale tiles,
|
||||
// as well as to remove the feature from the tile upon annotation deletion.
|
||||
anno_it.first->second->tileFeatures.emplace(
|
||||
tileID, std::weak_ptr<const LiveTileFeature>(feature));
|
||||
|
||||
// get ready for the next-lower zoom number
|
||||
z2 /= 2;
|
||||
x /= 2;
|
||||
y /= 2;
|
||||
@@ -101,65 +176,99 @@ std::pair<std::vector<Tile::ID>, std::vector<uint32_t>> AnnotationManager::addPo
|
||||
annotationIDs.push_back(annotationID);
|
||||
}
|
||||
|
||||
// Tile:IDs that need refreshed and the annotation identifiers held onto by the client.
|
||||
return std::make_pair(affectedTiles, annotationIDs);
|
||||
}
|
||||
|
||||
std::vector<Tile::ID> AnnotationManager::removeAnnotations(std::vector<uint32_t> ids) {
|
||||
std::vector<Tile::ID> AnnotationManager::removeAnnotations(const AnnotationIDs& ids, const Map& map) {
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
|
||||
std::vector<Tile::ID> affectedTiles;
|
||||
|
||||
for (auto& annotationID : ids) {
|
||||
auto annotation_it = annotations.find(annotationID);
|
||||
std::vector<uint32_t> z2s;
|
||||
uint8_t zoomCount = map.getMaxZoom() + 1;
|
||||
z2s.reserve(zoomCount);
|
||||
for (uint8_t z = 0; z < zoomCount; ++z) {
|
||||
z2s.emplace_back(1<< z);
|
||||
}
|
||||
|
||||
LatLng latLng;
|
||||
vec2<double> p;
|
||||
uint32_t x, y;
|
||||
|
||||
// iterate annotation id's passed
|
||||
for (const auto& annotationID : ids) {
|
||||
// grab annotation object
|
||||
const auto& annotation_it = annotations.find(annotationID);
|
||||
if (annotation_it != annotations.end()) {
|
||||
auto& annotation = annotation_it->second;
|
||||
for (auto& tile_it : annotationTiles) {
|
||||
auto& tileAnnotations = tile_it.second.first;
|
||||
util::erase_if(tileAnnotations, tileAnnotations.begin(),
|
||||
tileAnnotations.end(), [&](const uint32_t annotationID_) -> bool {
|
||||
return (annotationID_ == annotationID);
|
||||
});
|
||||
auto features_it = annotation->tileFeatures.find(tile_it.first);
|
||||
const auto& annotation = annotation_it->second;
|
||||
// calculate annotation's affected tile for each zoom
|
||||
for (uint8_t z = 0; z < zoomCount; ++z) {
|
||||
latLng = annotation->getPoint();
|
||||
p = projectPoint(latLng);
|
||||
x = z2s[z] * p.x;
|
||||
y = z2s[z] * p.y;
|
||||
Tile::ID tid(z, x, y);
|
||||
// erase annotation from tile's list
|
||||
auto& tileAnnotations = tiles[tid].first;
|
||||
tileAnnotations.erase(annotationID);
|
||||
// remove annotation's features from tile
|
||||
const auto& features_it = annotation->tileFeatures.find(tid);
|
||||
if (features_it != annotation->tileFeatures.end()) {
|
||||
auto layer = tile_it.second.second->getMutableLayer(util::ANNOTATIONS_POINTS_LAYER_ID);
|
||||
auto& features = features_it->second;
|
||||
layer->removeFeature(features[0]);
|
||||
affectedTiles.push_back(tile_it.first);
|
||||
const auto& layer =
|
||||
tiles[tid].second->getMutableLayer(layerID);
|
||||
layer->removeFeature(features_it->second);
|
||||
affectedTiles.push_back(tid);
|
||||
}
|
||||
}
|
||||
annotations.erase(annotationID);
|
||||
}
|
||||
}
|
||||
|
||||
// Tile::IDs for tiles that need refreshed.
|
||||
return affectedTiles;
|
||||
}
|
||||
|
||||
std::vector<uint32_t> AnnotationManager::getAnnotationsInBounds(LatLngBounds queryBounds, const Map& map) const {
|
||||
uint8_t z = map.getMaxZoom();
|
||||
uint32_t z2 = 1 << z;
|
||||
vec2<double> swPoint = projectPoint(queryBounds.sw);
|
||||
vec2<double> nePoint = projectPoint(queryBounds.ne);
|
||||
std::vector<uint32_t> AnnotationManager::getAnnotationsInBounds(const LatLngBounds& queryBounds,
|
||||
const Map& map) const {
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
|
||||
const uint8_t z = map.getMaxZoom();
|
||||
const uint32_t z2 = 1 << z;
|
||||
const vec2<double> swPoint = projectPoint(queryBounds.sw);
|
||||
const vec2<double> nePoint = projectPoint(queryBounds.ne);
|
||||
|
||||
// tiles number y from top down
|
||||
Tile::ID nwTile(z, swPoint.x * z2, nePoint.y * z2);
|
||||
Tile::ID seTile(z, nePoint.x * z2, swPoint.y * z2);
|
||||
const Tile::ID nwTile(z, swPoint.x * z2, nePoint.y * z2);
|
||||
const Tile::ID seTile(z, nePoint.x * z2, swPoint.y * z2);
|
||||
|
||||
std::vector<uint32_t> matchingAnnotations;
|
||||
|
||||
for (auto& tile : annotationTiles) {
|
||||
for (auto& tile : tiles) {
|
||||
Tile::ID id = tile.first;
|
||||
if (id.z == z) {
|
||||
if (id.x >= nwTile.x && id.x <= seTile.x && id.y >= nwTile.y && id.y <= seTile.y) {
|
||||
if (id.x > nwTile.x && id.x < seTile.x && id.y > nwTile.y && id.y < seTile.y) {
|
||||
// trivial accept; grab all of the tile's annotations
|
||||
std::copy(tile.second.first.begin(), tile.second.first.end(), std::back_inserter(matchingAnnotations));
|
||||
// Trivial accept; this tile is completely inside the query bounds, so
|
||||
// we'll return all of its annotations.
|
||||
std::copy(tile.second.first.begin(), tile.second.first.end(),
|
||||
std::back_inserter(matchingAnnotations));
|
||||
} else {
|
||||
// check tile's annotations' bounding boxes
|
||||
// This tile is intersected by the query bounds. We need to check the
|
||||
// tile's annotations' bounding boxes individually.
|
||||
std::copy_if(tile.second.first.begin(), tile.second.first.end(),
|
||||
std::back_inserter(matchingAnnotations), [&](const uint32_t annotationID) -> bool {
|
||||
LatLngBounds annoBounds = this->annotations.find(annotationID)->second->getBounds();
|
||||
return (annoBounds.sw.latitude >= queryBounds.sw.latitude &&
|
||||
annoBounds.ne.latitude <= queryBounds.ne.latitude &&
|
||||
annoBounds.sw.longitude >= queryBounds.sw.longitude &&
|
||||
annoBounds.ne.longitude <= queryBounds.ne.longitude);
|
||||
std::back_inserter(matchingAnnotations),
|
||||
[&](const uint32_t annotationID) -> bool {
|
||||
const auto it = annotations.find(annotationID);
|
||||
if (it != annotations.end()) {
|
||||
const LatLngBounds annoBounds = it->second->getBounds();
|
||||
return (annoBounds.sw.latitude >= queryBounds.sw.latitude &&
|
||||
annoBounds.ne.latitude <= queryBounds.ne.latitude &&
|
||||
annoBounds.sw.longitude >= queryBounds.sw.longitude &&
|
||||
annoBounds.ne.longitude <= queryBounds.ne.longitude);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -169,10 +278,12 @@ std::vector<uint32_t> AnnotationManager::getAnnotationsInBounds(LatLngBounds que
|
||||
return matchingAnnotations;
|
||||
}
|
||||
|
||||
LatLngBounds AnnotationManager::getBoundsForAnnotations(std::vector<uint32_t> ids) const {
|
||||
LatLngBounds AnnotationManager::getBoundsForAnnotations(const AnnotationIDs& ids) const {
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
|
||||
LatLngBounds bounds;
|
||||
for (auto id : ids) {
|
||||
auto annotation_it = annotations.find(id);
|
||||
const auto annotation_it = annotations.find(id);
|
||||
if (annotation_it != annotations.end()) {
|
||||
bounds.extend(annotation_it->second->getPoint());
|
||||
}
|
||||
@@ -181,12 +292,16 @@ LatLngBounds AnnotationManager::getBoundsForAnnotations(std::vector<uint32_t> id
|
||||
return bounds;
|
||||
}
|
||||
|
||||
const std::unique_ptr<LiveTile>& AnnotationManager::getTile(Tile::ID const& id) {
|
||||
const LiveTile* AnnotationManager::getTile(Tile::ID const& id) {
|
||||
std::lock_guard<std::mutex> lock(mtx);
|
||||
|
||||
auto tile_it = annotationTiles.find(id);
|
||||
if (tile_it != annotationTiles.end()) {
|
||||
return tile_it->second.second;
|
||||
const auto tile_it = tiles.find(id);
|
||||
if (tile_it != tiles.end()) {
|
||||
return tile_it->second.second.get();
|
||||
}
|
||||
return nullTile;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
const std::string AnnotationManager::layerID = "com.mapbox.annotations.points";
|
||||
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ private:
|
||||
GeometryCollection geometries;
|
||||
};
|
||||
|
||||
class LiveTileLayer : public GeometryTileLayer {
|
||||
class LiveTileLayer : public GeometryTileLayer {
|
||||
public:
|
||||
LiveTileLayer();
|
||||
|
||||
@@ -42,7 +42,6 @@ public:
|
||||
void addLayer(const std::string&, util::ptr<LiveTileLayer>);
|
||||
util::ptr<GeometryTileLayer> getLayer(const std::string&) const override;
|
||||
util::ptr<LiveTileLayer> getMutableLayer(const std::string&) const;
|
||||
bool operator()(const LiveTile&) const { return layers.size() > 0; }
|
||||
|
||||
private:
|
||||
std::map<std::string, util::ptr<LiveTileLayer>> layers;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <mbgl/map/annotation.hpp>
|
||||
#include <mbgl/map/live_tile_data.hpp>
|
||||
#include <mbgl/map/live_tile.hpp>
|
||||
#include <mbgl/map/tile_parser.hpp>
|
||||
#include <mbgl/style/style_source.hpp>
|
||||
#include <mbgl/map/vector_tile.hpp>
|
||||
@@ -36,7 +37,7 @@ void LiveTileData::parse() {
|
||||
}
|
||||
|
||||
if (source.type == SourceType::Annotations) {
|
||||
const std::unique_ptr<LiveTile>& tile = annotationManager.getTile(id);
|
||||
const LiveTile* tile = annotationManager.getTile(id);
|
||||
|
||||
if (tile) {
|
||||
// Parsing creates state that is encapsulated in TileParser. While parsing,
|
||||
|
||||
@@ -540,19 +540,19 @@ std::string Map::getAccessToken() const {
|
||||
|
||||
#pragma mark - Annotations
|
||||
|
||||
void Map::setDefaultPointAnnotationSymbol(std::string& symbol) {
|
||||
void Map::setDefaultPointAnnotationSymbol(const std::string& symbol) {
|
||||
assert(Environment::currentlyOn(ThreadType::Main));
|
||||
annotationManager->setDefaultPointAnnotationSymbol(symbol);
|
||||
}
|
||||
|
||||
uint32_t Map::addPointAnnotation(LatLng point, std::string& symbol) {
|
||||
uint32_t Map::addPointAnnotation(const LatLng& point, const std::string& symbol) {
|
||||
assert(Environment::currentlyOn(ThreadType::Main));
|
||||
std::vector<LatLng> points({ point });
|
||||
std::vector<std::string> symbols({ symbol });
|
||||
return addPointAnnotations(points, symbols)[0];
|
||||
}
|
||||
|
||||
std::vector<uint32_t> Map::addPointAnnotations(std::vector<LatLng> points, std::vector<std::string>& symbols) {
|
||||
std::vector<uint32_t> Map::addPointAnnotations(const std::vector<LatLng>& points, const std::vector<std::string>& symbols) {
|
||||
assert(Environment::currentlyOn(ThreadType::Main));
|
||||
auto result = annotationManager->addPointAnnotations(points, symbols, *this);
|
||||
updateAnnotationTiles(result.first);
|
||||
@@ -564,23 +564,24 @@ void Map::removeAnnotation(uint32_t annotation) {
|
||||
removeAnnotations({ annotation });
|
||||
}
|
||||
|
||||
void Map::removeAnnotations(std::vector<uint32_t> annotations) {
|
||||
void Map::removeAnnotations(const std::vector<uint32_t>& annotations) {
|
||||
assert(Environment::currentlyOn(ThreadType::Main));
|
||||
auto result = annotationManager->removeAnnotations(annotations);
|
||||
auto result = annotationManager->removeAnnotations(annotations, *this);
|
||||
updateAnnotationTiles(result);
|
||||
}
|
||||
|
||||
std::vector<uint32_t> Map::getAnnotationsInBounds(LatLngBounds bounds) const {
|
||||
std::vector<uint32_t> Map::getAnnotationsInBounds(const LatLngBounds& bounds) const {
|
||||
assert(Environment::currentlyOn(ThreadType::Main));
|
||||
return annotationManager->getAnnotationsInBounds(bounds, *this);
|
||||
}
|
||||
|
||||
LatLngBounds Map::getBoundsForAnnotations(std::vector<uint32_t> annotations) const {
|
||||
LatLngBounds Map::getBoundsForAnnotations(const std::vector<uint32_t>& annotations) const {
|
||||
assert(Environment::currentlyOn(ThreadType::Main));
|
||||
return annotationManager->getBoundsForAnnotations(annotations);
|
||||
}
|
||||
|
||||
void Map::updateAnnotationTiles(std::vector<Tile::ID>& ids) {
|
||||
void Map::updateAnnotationTiles(const std::vector<Tile::ID>& ids) {
|
||||
assert(Environment::currentlyOn(ThreadType::Main));
|
||||
for (const auto &source : activeSources) {
|
||||
if (source->info.type == SourceType::Annotations) {
|
||||
source->source->invalidateTiles(*this, ids);
|
||||
@@ -718,10 +719,11 @@ void Map::reloadStyle() {
|
||||
const auto styleInfo = data->getStyleInfo();
|
||||
|
||||
if (!styleInfo.url.empty()) {
|
||||
const auto base = styleInfo.base;
|
||||
// We have a style URL
|
||||
env->request({ Resource::Kind::JSON, styleInfo.url }, [&](const Response &res) {
|
||||
env->request({ Resource::Kind::JSON, styleInfo.url }, [this, base](const Response &res) {
|
||||
if (res.status == Response::Successful) {
|
||||
loadStyleJSON(res.data, styleInfo.base);
|
||||
loadStyleJSON(res.data, base);
|
||||
} else {
|
||||
Log::Error(Event::Setup, "loading style failed: %s", res.message.c_str());
|
||||
}
|
||||
|
||||
@@ -373,7 +373,7 @@ void Source::update(Map &map,
|
||||
updated = map.getTime();
|
||||
}
|
||||
|
||||
void Source::invalidateTiles(Map& map, std::vector<Tile::ID>& ids) {
|
||||
void Source::invalidateTiles(Map& map, const std::vector<Tile::ID>& ids) {
|
||||
for (auto& id : ids) {
|
||||
tiles.erase(id);
|
||||
tile_data.erase(id);
|
||||
|
||||
@@ -37,7 +37,7 @@ public:
|
||||
void load(Map &, Environment &);
|
||||
void update(Map &, uv::worker &, util::ptr<Style>, GlyphAtlas &, GlyphStore &,
|
||||
SpriteAtlas &, util::ptr<Sprite>, TexturePool &, std::function<void()> callback);
|
||||
void invalidateTiles(Map&, std::vector<Tile::ID>&);
|
||||
void invalidateTiles(Map&, const std::vector<Tile::ID>&);
|
||||
|
||||
void updateMatrices(const mat4 &projMatrix, const TransformState &transform);
|
||||
void drawClippingMasks(Painter &painter);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <mbgl/style/style_source.hpp>
|
||||
#include <mbgl/style/style_parser.hpp>
|
||||
#include <mbgl/style/style_layer_group.hpp>
|
||||
#include <mbgl/map/annotation.hpp>
|
||||
#include <mbgl/util/constants.hpp>
|
||||
#include <mbgl/util/std.hpp>
|
||||
#include <mbgl/util/vec.hpp>
|
||||
@@ -32,7 +33,7 @@ void StyleParser::parse(JSVal document) {
|
||||
|
||||
// create point annotations layer
|
||||
//
|
||||
std::string id = util::ANNOTATIONS_POINTS_LAYER_ID;
|
||||
const std::string& id = AnnotationManager::layerID;
|
||||
|
||||
std::map<ClassID, ClassProperties> paints;
|
||||
util::ptr<StyleLayer> annotations = std::make_shared<StyleLayer>(id, std::move(paints));
|
||||
|
||||
@@ -8,8 +8,6 @@ const double mbgl::util::M2PI = 2 * M_PI;
|
||||
const double mbgl::util::EARTH_RADIUS_M = 6378137;
|
||||
const double mbgl::util::LATITUDE_MAX = 85.05112878;
|
||||
|
||||
const std::string mbgl::util::ANNOTATIONS_POINTS_LAYER_ID = "com.mapbox.annotations.points";
|
||||
|
||||
#if defined(DEBUG)
|
||||
const bool mbgl::debug::tileParseWarnings = false;
|
||||
const bool mbgl::debug::styleParseWarnings = false;
|
||||
|
||||
Reference in New Issue
Block a user