summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbsudekum <bobby@mapbox.com>2017-01-03 13:12:57 -0800
committerbsudekum <bobby@mapbox.com>2017-01-03 13:12:57 -0800
commitc011189f712565e4813eedb532e7f81b73c62f39 (patch)
treea425ef25e96773d248572d541d1d58abe934b683
parent561694a878c0499c08bd036d0a902e296be8a1d7 (diff)
downloadqtlocation-mapboxgl-upstream/bs-annotation-node.tar.gz
Add annotation apiupstream/bs-annotation-node
-rw-r--r--platform/node/src/node_map.cpp47
-rw-r--r--platform/node/src/node_map.hpp2
-rw-r--r--platform/node/test.js44
3 files changed, 93 insertions, 0 deletions
diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp
index acf83eef66..fad372a307 100644
--- a/platform/node/src/node_map.cpp
+++ b/platform/node/src/node_map.cpp
@@ -9,6 +9,8 @@
#include <mbgl/style/conversion/source.hpp>
#include <mbgl/style/conversion/layer.hpp>
#include <mbgl/style/conversion/filter.hpp>
+#include <mbgl/annotation/annotation.hpp>
+#include <mbgl/sprite/sprite_image.hpp>
#include <unistd.h>
@@ -20,6 +22,12 @@
namespace node_mbgl {
+struct AnnotationOptions {
+ double latitude = 0;
+ double longitude = 0;
+ std::string image;
+};
+
struct NodeMap::RenderOptions {
double zoom = 0;
double bearing = 0;
@@ -29,6 +37,7 @@ struct NodeMap::RenderOptions {
unsigned int width = 512;
unsigned int height = 512;
std::vector<std::string> classes;
+ std::vector<AnnotationOptions> annotations;
mbgl::MapDebugOptions debugOptions = mbgl::MapDebugOptions::NoDebug;
};
@@ -248,6 +257,23 @@ NodeMap::RenderOptions NodeMap::ParseOptions(v8::Local<v8::Object> obj) {
options.pitch = Nan::Get(obj, Nan::New("pitch").ToLocalChecked()).ToLocalChecked()->NumberValue();
}
+ if (Nan::Has(obj, Nan::New("annotations").ToLocalChecked()).FromJust()) {
+ auto annotationsArray = Nan::Get(obj, Nan::New("annotations").ToLocalChecked()).ToLocalChecked();
+ if (annotationsArray->IsArray()) {
+ auto annotations = annotationsArray.As<v8::Array>();
+ const int length = annotations->Length();
+ for (int i = 0; i < length; i++) {
+ auto annotationObj = annotations->Get(i).As<v8::Object>();
+ double latitude = Nan::Get(annotationObj, Nan::New("latitude").ToLocalChecked()).ToLocalChecked()->NumberValue();
+ double longitude = Nan::Get(annotationObj, Nan::New("longitude").ToLocalChecked()).ToLocalChecked()->NumberValue();
+ std::string image = "foo";
+ options.annotations.push_back({
+ latitude, longitude, image
+ });
+ }
+ }
+ }
+
if (Nan::Has(obj, Nan::New("center").ToLocalChecked()).FromJust()) {
auto centerObj = Nan::Get(obj, Nan::New("center").ToLocalChecked()).ToLocalChecked();
if (centerObj->IsArray()) {
@@ -392,6 +418,27 @@ void NodeMap::startRender(NodeMap::RenderOptions options) {
map->setDebug(options.debugOptions);
}
+ for(auto id: annotationIDs) {
+ map->removeAnnotation(id);
+ }
+ annotationIDs.clear();
+
+ for(auto annotationIconString: annotationIconNames) {
+ map->removeAnnotationIcon(annotationIconString);
+ }
+ annotationIconNames.clear();
+
+ for(auto annotation: options.annotations) {
+ std::string iconName = "airfield-15";
+ annotationIconNames.push_back(iconName);
+
+ mbgl::PremultipliedImage cPremultipliedImage({ static_cast<uint32_t>(40), static_cast<uint32_t>(40) });
+ auto cSpriteImage = std::make_shared<mbgl::SpriteImage>(std::move(cPremultipliedImage), 1);
+ map->addAnnotationIcon(iconName, cSpriteImage);
+ map->addAnnotation(mbgl::SymbolAnnotation {{ annotation.longitude, annotation.latitude }, iconName });
+ annotationIDs.push_back(map->addAnnotation(mbgl::SymbolAnnotation {{ annotation.longitude, annotation.latitude }, iconName }));
+ }
+
map->renderStill(*view, [this](const std::exception_ptr eptr) {
if (eptr) {
error = std::move(eptr);
diff --git a/platform/node/src/node_map.hpp b/platform/node/src/node_map.hpp
index c68f543b02..4600bd5986 100644
--- a/platform/node/src/node_map.hpp
+++ b/platform/node/src/node_map.hpp
@@ -61,6 +61,8 @@ public:
std::unique_ptr<mbgl::OffscreenView> view;
NodeThreadPool threadpool;
std::unique_ptr<mbgl::Map> map;
+ std::vector<mbgl::AnnotationID> annotationIDs;
+ std::vector<std::string> annotationIconNames;
std::exception_ptr error;
mbgl::PremultipliedImage image;
diff --git a/platform/node/test.js b/platform/node/test.js
new file mode 100644
index 0000000000..6a3cc66666
--- /dev/null
+++ b/platform/node/test.js
@@ -0,0 +1,44 @@
+var fs = require('fs');
+var path = require('path');
+var mbgl = require('./index.js');
+var sharp = require('sharp');
+
+var options = {
+ request: function(req, callback) {
+ fs.readFile(path.join(__dirname, 'test', req.url), function(err, data) {
+ callback(err, { data: data });
+ });
+ },
+ ratio: 1
+};
+
+var map = new mbgl.Map(options);
+
+map.load(require('./test/fixtures/style.json'));
+
+map.render({
+ zoom: 0,
+ bearing: 20,
+ annotations: [{
+ latitude: 0,
+ longitude: 0,
+ imageBuffer: new Buffer()
+ }]
+}, function(err, buffer) {
+ if (err) throw err;
+
+ map.release();
+
+ var image = sharp(buffer, {
+ raw: {
+ width: 512,
+ height: 512,
+ channels: 4
+ }
+ });
+
+ // Convert raw image buffer to PNG
+ image.toFile('image.png', function(err) {
+ if (err) throw err;
+ });
+});