在Flutter应用中向Mapbox添加可拖动标记。

huangapple go评论67阅读模式
英文:

Add draggable marker to flutter app mapbox

问题

我正在使用mapbox_maps_flutter: 0.4.4包构建一个Flutter应用程序。由于没有官方的Flutter文档,因此我只能使用示例和Android API来解决问题。但我想要实现的是在地图上有一个自定义标记图标,最初位于用户当前位置的相同位置,然后每当我在地图上拖动滚动时,标记会移动(通过获取相机的中心点来实现)。如果这太复杂,那么我只希望标记可以拖动(就像Uber应用程序在您请求行程时一样)。

我不介意指向一些Flutter文档或至少是如何使用标记的良好而详细的示例。

最后,请注意,我正在使用0.44版本,我认为它不再与mapbox_gl兼容。

以下是一些相关的代码:

MapboxMap? mapboxMap;

_onMapCreated(MapboxMap mapboxMap) async {
    this.mapboxMap = mapboxMap;
    mapboxMap.location
        .updateSettings(LocationComponentSettings(enabled: true)); // 显示当前位置
    mapboxMap.scaleBar.updateSettings(ScaleBarSettings(
      enabled: false,)); // 隐藏比例尺
    mapboxMap.compass.updateSettings(CompassSettings(
      enabled: false,)); // 隐藏指南针
  }

Widget _map() {
    return SizedBox(
      height: MediaQuery.of(context).size.height * 0.8, //mapbox需要有高度
      child: MapWidget(
          resourceOptions: ResourceOptions(accessToken: MAPBOX_ACCESS_TOKEN),
        onMapCreated: _onMapCreated,
        key: const ValueKey("mapWidget"),
        cameraOptions: CameraOptions(
            center: Point(
                coordinates: Position(
                  widget.location.longitude,
                  widget.location.latitude,
                )).toJson(),
            zoom: 17.5
        ),
      )
    );
  }

希望这对你有所帮助。如果你需要更多帮助,请告诉我。

英文:

I am building a flutter app using the mapbox_maps_flutter: 0.4.4 package. There is no official flutter documentation so I am just using the examples and the android api to figure things out.
But what i am trying to achieve is have a custom marker icon on my map that is initially in the same position as the user's current location, and then anytime I drag scroll on the map, the marker moves (by taking the center point of the camera).
If this is too complex, then I would just like the marker to be draggable (like the uber app when you're requesting a trip).

I don't mind being pointed to some flutter documentation or at least a good and detailed example on how to use markers.

Lastly, please e aware that I am using version 0.44 which is no longer compatible with mapbox_gl I believe.

Here's some relevant code:

MapboxMap? mapboxMap;

_onMapCreated(MapboxMap mapboxMap) async {
    this.mapboxMap = mapboxMap;
    mapboxMap.location
        .updateSettings(LocationComponentSettings(enabled: true)); // show current position
    mapboxMap.scaleBar.updateSettings(ScaleBarSettings(
      enabled: false,)); // hide the scale bar
    mapboxMap.compass.updateSettings(CompassSettings(
      enabled: false,)); // hide the compass
  }

Widget _map() {
    return SizedBox(
      height: MediaQuery.of(context).size.height * 0.8, //mapbox needs to have a height
      child: MapWidget(
          resourceOptions: ResourceOptions(accessToken: MAPBOX_ACCESS_TOKEN),
        onMapCreated: _onMapCreated,
        key: const ValueKey("mapWidget"),
        cameraOptions: CameraOptions(
            center: Point(
                coordinates: Position(
                  widget.location.longitude,
                  widget.location.latitude,
                )).toJson(),
            zoom: 17.5
        ),
      )
    );
  }

答案1

得分: 2

我明白了。正如我之前所说,mapbox_maps_flutter: 0.4.4目前与mapbox_gl不兼容。添加自定义标记的方法是使用点标记与标记管理器一起使用。

以下是设置的样例代码:

MapboxMap? mapboxMap;
PointAnnotationManager? pointAnnotationManager;
PointAnnotation? pointAnnotation;

_onMapCreated(MapboxMap mapboxMap) async {
    this.mapboxMap = mapboxMap;
    mapboxMap.annotations.createPointAnnotationManager().then((value) async {
        pointAnnotationManager = value;
        _createMarker();
    });
}

Future<void> _createMarker({double currentLat = 0, double currentLong = 0}) async {
    final ByteData bytes = await rootBundle.load('assets/images/marker.png');
    final Uint8List list = bytes.buffer.asUint8List();

    pointAnnotationManager
        ?.create(PointAnnotationOptions(
            geometry: Point(
                coordinates: Position(
                  currentLong == 0
                      ? widget.postLongitude != 0 ? widget.postLongitude : widget.location.longitude
                      : currentLong,
                  currentLat == 0
                      ? widget.postLatitude != 0 ? widget.postLatitude : widget.location.latitude
                      : currentLat,
                )).toJson(),
            iconSize: 2.5,
            iconOffset: [0.0, -10.0],
            symbolSortKey: 10,
            image: list))
        .then((value) => pointAnnotation = value);
}

Widget _map() {
    return SizedBox(
        height: MediaQuery.of(context).size.height * 0.8,
        child: MapWidget(
            resourceOptions: ResourceOptions(accessToken: MAPBOX_ACCESS_TOKEN),
            onMapCreated: _onMapCreated,
            key: const ValueKey("mapWidget"),
            onScrollListener: (screenCoordinate) {
                pointAnnotationManager?.deleteAll();
                _createMarker(currentLat: screenCoordinate.x, currentLong: screenCoordinate.y);
            },
            onTapListener: (screenCoordinate) async {
                pointAnnotationManager?.deleteAll();
                _createMarker(currentLat: screenCoordinate.x, currentLong: screenCoordinate.y);
                setState(() {});
            },
        )
    );
}

这是如何创建标记并将其放置在任意初始GPS位置的方法。以下是如何在地图上拖动滚动或单击地图中的任何位置时移动标记的方法。

英文:

I figured it out. As I said before, the mapbox_maps_flutter: 0.4.4 is AS OF THIS MOMENT not compatible with mapbox_gl. The way to add custom_markers is to use point annotations in conjunction with the annotation manager.

Here's what the setup looks like:

    MapboxMap? mapboxMap;
    PointAnnotationManager? pointAnnotationManager;
    PointAnnotation? pointAnnotation;
    
    _onMapCreated(MapboxMap mapboxMap) async {
        this.mapboxMap = mapboxMap;
        mapboxMap.annotations.createPointAnnotationManager().then((value) async {
          pointAnnotationManager = value;
          _createMarker();
        });
      }

Future&lt;void&gt; _createMarker({double currentLat = 0, double currentLong = 0, }) async {
    final ByteData bytes =
    await rootBundle.load(&#39;assets/images/marker.png&#39;);
    final Uint8List list = bytes.buffer.asUint8List();

    pointAnnotationManager
        ?.create(PointAnnotationOptions(
        geometry: Point(
            coordinates: Position(
              currentLong == 0
                  ? widget.postLongitude != 0 ? widget.postLongitude : widget.location.longitude
                  : currentLong,
              currentLat == 0
                  ? widget.postLatitude != 0 ? widget.postLatitude : widget.location.latitude
                  : currentLat,
            )).toJson(),
        iconSize: 2.5,
        iconOffset: [0.0, -10.0],
        symbolSortKey: 10,
        image: list))
        .then((value) =&gt; pointAnnotation = value);
  }

The above is how to create the marker and place it at an arbitrary initial gps location. The below shows how to move the marker when you drag scroll on the map or when you click anywhere within the map:

Widget _map() {
    return SizedBox(
      height: MediaQuery.of(context).size.height * 0.8, //mapbox needs to have a height
      child: MapWidget(
          resourceOptions: ResourceOptions(accessToken: MAPBOX_ACCESS_TOKEN),
        onMapCreated: _onMapCreated,
        key: const ValueKey(&quot;mapWidget&quot;),
        onScrollListener: (screenCoordinate) {
          pointAnnotationManager?.deleteAll();
          _createMarker(currentLat: screenCoordinate.x, currentLong: screenCoordinate.y);
        },
        onTapListener: (screenCoordinate) async {
          pointAnnotationManager?.deleteAll();
          _createMarker(currentLat: screenCoordinate.x, currentLong: screenCoordinate.y);
          setState(()  {});
        },
      )
    );
  }

huangapple
  • 本文由 发表于 2023年6月15日 09:33:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76478532.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定