【发布时间】:2017-11-09 02:12:22
【问题描述】:
我正在使用 Android Mapbox SDK 将 VectorSource 添加到 MapboxMap,然后我也尝试将 LineLayer 添加到地图。
目前使用的是 5.1.3 版本
此代码将在 TypeScript 中,因为它使用 NativeScript 框架,该框架允许使用本机库并直接访问本机 android/iOS API。
// these constants are just simple ways to reference a class in the mapbox package
const PropertyFactory = com.mapbox.mapboxsdk.style.layers.PropertyFactory;
const Property = com.mapbox.mapboxsdk.style.layers.Property;
const LineLayer = com.mapbox.mapboxsdk.style.layers.LineLayer;
const VectorSource = com.mapbox.mapboxsdk.style.sources.VectorSource;
const lineCap = com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineCap;
const lineColor = com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;
const lineJoin = com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin;
const lineWidth = com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;
const LatLngBounds = com.mapbox.mapboxsdk.geometry.LatLngBounds;
const FillLayer = com.mapbox.mapboxsdk.style.layers.FillLayer;
const SymbolLayer = com.mapbox.mapboxsdk.style.layers.SymbolLayer;
const CircleLayer = com.mapbox.mapboxsdk.style.layers.CircleLayer;
// set the map GLSource (vector) to the mapbox map
const vectorSource = new VectorSource(
layer.ID.toString(),
`http://themaptiles.cloudapp.net/data/${layer.GLSource}.json`
);
this._mapboxMap.addSource(vectorSource);
let newLayer;
if (layer.Type == "line") {
console.log(`*** creating new LineLayer ***`);
newLayer = new LineLayer("line-layer", layer.ID.toString());
// get the line color for this style
const lColor = style["line"]["line-color"];
const androidColor = new Color(lColor).android; // ends up valid and something like -1293839 for android to use
newLayer.setSourceLayer(layer.ID.toString());
// will throw here with `Failed resolving method setProperties on class com.mapbox.mapboxsdk.style.layers.Layer`
newLayer.setProperties(
PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
PropertyFactory.lineCap(Property.LINE_CAP_ROUND),
PropertyFactory.lineColor(androidColor),
PropertyFactory.lineWidth(new java.lang.Float(2))
);
}
this._mapboxMap.addLayer(newLayer);
如果我不尝试使用setProperties 方法,则代码可以正常执行,但添加图层后地图上没有可见线。
【问题讨论】:
-
嘿@Brad Martin,您能尝试将属性数组传递给
setProperties可变参数方法吗? -
感谢@pkanev - 将设置作为数组传递确实可以防止抛出一般异常
标签: android typescript mapbox nativescript