如何在最新的Flutter Mapbox_gl版本中使用.addCircleLayer()方法?

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

How do you use the .addCircleLayer() in the latest Flutter Mapbox_gl version?

问题

I am making a flutter website that fetches realtime location data from my firebase database and project them in my mapbox map. I was able to deal with using .addCircle() to individually add them to the map but it seems like the better approach is to use .addCircleLayer() since I am consistently updating multiple circles at the same time. I have searched over the internet for ways to use .addCircleLayer() but most of them looks outdated with methods that are no longer recognized to the current version.

Here is my code in projecting the locations one by one:

void _projectVehiclePositions(List<LatLng> VehiclePositions) {
    String sourceId = 'PositionStream';
    String layerId = 'PositionStream';
    
    _mapController.addSource(sourceId, GeojsonSourceProperties(
      data: VehiclePositions,
    ));
    
    _mapController.addCircleLayer(sourceId, layerId, const CircleLayerProperties(
      circleColor: '#E91E63',
      circleRadius: 5.0,
      circleOpacity: 0.8,
      circleStrokeColor: '#E91E63'
    ));
}

I preprocess the data first into one List of vehicle positions before I pass it to the function responsible for adding the circle layer. So far, it doesn't work at the moment.

英文:

I am making a flutter website that fetches realtime location data from my firebase database and project them in my mapbox map. I was able to deal with using .addCircle() to individually add them to the map but it seems like the better approach is to use .addCircleLayer() since I am consistently updating multiple circles at the same time. I have searched over the internet for ways to use .addCircleLayer() but most of them looks outdated with methods that are no longer recognized to the current version.

Here is my code in projecting the locations one by one:

   void _projectVehiclePositions(List&lt;VehicleData&gt; Vehicles) {

    Vehicles.forEach((Vehicle) {
      final vehicleCircle = CircleOptions(
        geometry: LatLng(Vehicle.position.latitude, Vehicle.position.longitude),
      );
      _mapController.addCircle(vehicleCircle);
    });

   }

And here is my attempt to .addCircleLayer():

 void _projectVehiclePositions(List&lt;LatLng&gt; VehiclePositions) {
    String sourceId = &#39;PositionStream&#39;;
    String layerId = &#39;PositionStream&#39;;
    
    _mapController.addSource(sourceId, GeojsonSourceProperties(
      data: VehiclePositions,
    ));
    
    _mapController.addCircleLayer(sourceId, layerId, const CircleLayerProperties(
      circleColor: &#39;#E91E63&#39;,
      circleRadius: 5.0,
      circleOpacity: 0.8,
      circleStrokeColor: &#39;#E91E63&#39;
    ));
    
  }

I preprocess the data first into one List<LatLng> of vehicle positions before I pass it to the function responsible of adding the circle layer. So far, it doesn't work at the moment.

答案1

得分: 1

Here's the provided code translated into Chinese:

// 以下是提供的代码。请相应实现。

import 'package:flutter/material.dart';
import 'package:flutter_mapbox_gl/flutter_mapbox_gl.dart';

class MapPage extends StatefulWidget {
  const MapPage({Key? key}) : super(key: key);

  @override
  _MapPageState createState() => _MapPageState();
}

class _MapPageState extends State<MapPage> {
  MapboxMapController? _mapController;
  final String accessToken = 'MAPBOX_ACCESS_TOKEN'; // 添加你的令牌密钥
  final LatLng initialCameraPosition = LatLng(37.7749, -122.4194);
  final List<LatLng> vehiclePositions = [
    LatLng(37.7749, -122.4194),
    LatLng(37.7849, -122.4194),
    LatLng(37.7949, -122.4194),
    LatLng(37.8049, -122.4194),
    LatLng(37.8149, -122.4194),
  ];

  void _onMapCreated(MapboxMapController controller) {
    _mapController = controller;
  }

  void _addVehiclePositions(List<LatLng> positions) {
    String sourceId = 'vehiclePositions';
    String layerId = 'vehiclePositions';
    GeoJsonSource source = GeoJsonSource(sourceId, data: {
      'type': 'FeatureCollection',
      'features': positions.map((position) {
        return {
          'type': 'Feature',
          'geometry': {
            'type': 'Point',
            'coordinates': [position.longitude, position.latitude]
          }
        };
      }).toList()
    });
    CircleLayer circleLayer = CircleLayer(layerId, sourceId);
    circleLayer.circleColor = "#007cbf";
    circleLayer.circleRadius = 5;
    _mapController?.addSource(sourceId, source);
    _mapController?.addLayer(circleLayer);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('地图页面'),
      ),
      body: MapboxMap(
        accessToken: accessToken,
        onMapCreated: _onMapCreated,
        initialCameraPosition: CameraPosition(
          target: initialCameraPosition,
          zoom: 12,
        ),
        styleString: MapboxStyles.MAPBOX_STREETS,
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _addVehiclePositions(vehiclePositions);
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}

如果遇到任何问题,请告诉我。
英文:

i've provided below code. implement accordingly.

import &#39;package:flutter/material.dart&#39;;
import &#39;package:flutter_mapbox_gl/flutter_mapbox_gl.dart&#39;;
class MapPage extends StatefulWidget {
const MapPage({Key? key}) : super(key: key);
@override
_MapPageState createState() =&gt; _MapPageState();
}
class _MapPageState extends State&lt;MapPage&gt; {
MapboxMapController? _mapController;
final String accessToken =
&#39;MAPBOX_ACCESS_TOKEN&#39;; // add your token key
final LatLng initialCameraPosition = LatLng(37.7749, -122.4194); 
final List&lt;LatLng&gt; vehiclePositions = [
LatLng(37.7749, -122.4194),
LatLng(37.7849, -122.4194),
LatLng(37.7949, -122.4194),
LatLng(37.8049, -122.4194),
LatLng(37.8149, -122.4194),
];
void _onMapCreated(MapboxMapController controller) {
_mapController = controller;
}
void _addVehiclePositions(List&lt;LatLng&gt; positions) {
String sourceId = &#39;vehiclePositions&#39;;
String layerId = &#39;vehiclePositions&#39;;
GeoJsonSource source = GeoJsonSource(sourceId, data: {
&#39;type&#39;: &#39;FeatureCollection&#39;,
&#39;features&#39;: positions.map((position) {
return {
&#39;type&#39;: &#39;Feature&#39;,
&#39;geometry&#39;: {
&#39;type&#39;: &#39;Point&#39;,
&#39;coordinates&#39;: [position.longitude, position.latitude]
}
};
}).toList()
});
CircleLayer circleLayer = CircleLayer(layerId, sourceId);
circleLayer.circleColor = &quot;#007cbf&quot;;
circleLayer.circleRadius = 5;
_mapController?.addSource(sourceId, source);
_mapController?.addLayer(circleLayer);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(&#39;Map Page&#39;),
),
body: MapboxMap(
accessToken: accessToken,
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: initialCameraPosition,
zoom: 12,
),
styleString: MapboxStyles.MAPBOX_STREETS,
),
floatingActionButton: FloatingActionButton(
onPressed: () {
_addVehiclePositions(vehiclePositions);
},
child: const Icon(Icons.add),
),
);
}
}

let me know if you face any issue.

huangapple
  • 本文由 发表于 2023年5月11日 17:38:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226204.html
匿名

发表评论

匿名网友

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

确定