【问题标题】:MapBox setProperties on LineLayer fails to resolveLineLayer 上的 MapBox setProperties 无法解析
【发布时间】: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


【解决方案1】:

众多 Java JavaScript 不兼容以及 NativeScript 中的挑战之一 - 可变参数函数

查看 mapbox 的官方 API 参考,Layer.setProperties 方法采用参数列表:https://www.mapbox.com/android-docs/api/map-sdk/5.2.0-beta.3/com/mapbox/mapboxsdk/style/layers/Layer.html#setProperties-com.mapbox.mapboxsdk.style.layers.PropertyValue...-

但是,用于 JNI -> Java 调用 Layer.setProperties 的参数无法从 JavaScript 中的函数调用中轻松提取,因此您应该将参数包装在一个数组中。

  newLayer.setProperties([
    PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
    PropertyFactory.lineCap(Property.LINE_CAP_ROUND),
    PropertyFactory.lineColor(androidColor),
    PropertyFactory.lineWidth(new java.lang.Float(2))
  ]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多