Flutter Mapbox Point 扩展到 MyPoint 不返回扩展变量

问题描述 投票:0回答:1

希望您能轻松帮助我找到我的错误。我的代码基于这个示例

https://pub.dev/packages/mapbox_maps_flutter/example

我已将扩展变量修改为 MyPoint

/// Point, as specified here https://tools.ietf.org/html/rfc7946#section-3.1.2
@JsonSerializable(explicitToJson: true)
class MyPoint extends Point {
  int? pinnumber;
  MyPoint({
    super.bbox,
    required super.coordinates,
    this.pinnumber,
  });

  factory MyPoint.fromJson(Map<String, dynamic> json) => _$MyPointFromJson(json);

  @override
  Map<String, dynamic> toJson() => super.serialize(_$MyPointToJson(this));

  @override
  MyPoint clone() => MyPoint(coordinates: coordinates.clone(), bbox: bbox?.clone(), pinnumber: pinnumber);

  @override
  int get hashCode => Object.hashAll([
    type,
    ...coordinates,
    if (bbox != null) ...bbox!,
    if (pinnumber != null) pinnumber!,
  ]);

  @override
  bool operator == (Object other) => other is MyPoint
      ? coordinates == other.coordinates
      : false;
}

当我使用 addAnnotation 初始化 de Points 时

  for (MyPoint pinpoint in pinlist) {
    pointAnnotationManager?.addAnnotation(imageData, pinpoint,);
  }

我可以看到新的 Int“pin 号码” 1

我还在这里看到了新的 Int“pinnumber” 2

当我用

读出 AnnotationClickListener 上的 Pin 时,却没有

最终结束 = MyPoint.fromJson((annotation.geometry)!.cast());

我确实得到了 Var,但它们是空的!

3

class AnnotationClickListener extends OnPointAnnotationClickListener {
  _MapBoxMapScreenState mapState;

  Animation<double>? animation;
  AnimationController? controller;

  AnnotationClickListener(this.mapState);

  @override
  void onPointAnnotationClick(PointAnnotation annotation) async {
    if (await mapState.mapboxMap.style.styleSourceExists("source")) {
      await mapState.mapboxMap.style.removeStyleLayer("layer");
      await mapState.mapboxMap.style.removeStyleSource("source");
    }

    // build route from puck position to the clicked annotation
    final start = await mapState.mapboxMap.style.getPuckPosition();
    final end = MyPoint.fromJson((annotation.geometry)!.cast());

    final coordinates = await fetchRouteCoordinates(start, end.coordinates, MapsDemo.ACCESS_TOKEN);

    drawRouteLowLevel(coordinates);
  }

  drawRouteLowLevel(List<Position> polyline) async {
    final line = LineString(coordinates: polyline);
    mapState.mapboxMap.style.styleSourceExists("source").then((exists) async {
      if (exists) {
        // if source exists - just update it
        final source = await mapState.mapboxMap.style.getSource("source");
        (source as GeoJsonSource).updateGeoJSON(json.encode(line));
      } else {
        await mapState.mapboxMap.style.addSource(GeoJsonSource(
            id: "source", data: json.encode(line), lineMetrics: true));

        await mapState.mapboxMap.style.addLayer(LineLayer(
          id: 'layer',
          sourceId: 'source',
          lineCap: LineCap.ROUND,
          lineJoin: LineJoin.ROUND,
          lineBlur: 1.0,
          lineColor: Colors.deepOrangeAccent.value,
          lineDasharray: [1.0, 2.0],
          lineTrimOffset: [0.0, 0.0],
          lineWidth: 5.0,
        ));
      }

      // query line layer
      final lineLayer =
          await mapState.mapboxMap.style.getLayer('layer') as LineLayer;

      // draw layer with gradient
      mapState.mapboxMap.style.setStyleLayerProperty("layer", "line-gradient",
          '["interpolate",["linear"],["line-progress"],0.0,["rgb",255,0,0],0.4,["rgb",0,255,0],1.0,["rgb",0,0,255]]');

      // animate layer to reveal it from start to end
      controller?.stop();
      controller = AnimationController(
          duration: const Duration(seconds: 2), vsync: mapState);
      animation = Tween<double>(begin: 0, end: 1.0).animate(controller!)
        ..addListener(() async {
          // set the animated value of lineTrim and update the layer
          lineLayer.lineTrimOffset = [animation?.value, 1.0];
          mapState.mapboxMap.style.updateLayer(lineLayer);
        });
      controller?.forward();
    });
  }
}

对我的问题有什么想法吗?

flutter mapbox rnmapbox-maps
1个回答
0
投票

您的问题为我找不到解决方案的问题提供了解决方案。

我的问题是,在“AnnotationClickListener”内部,我无法在主页上调用方法,它会给我卸载错误。

我的原始代码:

class PointAnnotationClickListener extends OnPointAnnotationClickListener {
  @override
  void onPointAnnotationClick(PointAnnotation annotation) {
    print("onAnnotationClick, id: ${annotation.id}");

    _MyHomePageState().showAnnotationDialog(annotation.id);  // <== this will give unmount error.
    
  }
}

看到你的问题后,这是我的新工作代码:

    class PointAnnotationClickListener extends OnPointAnnotationClickListener {
  late _MyHomePageState myPageState;

  PointAnnotationClickListener(this.myPageState); // passing the current object of my active homePage.

  @override
  void onPointAnnotationClick(PointAnnotation annotation) {
    print("onAnnotationClick, id: ${annotation.id}");

    myPageState.showAnnotationDialog(annotation.id);
    
  }
}

我希望有人也觉得它有用。 谢谢你。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.