You are not logged in.

#1 2021-01-22 10:16:00

annahernandez99
Member
Registered: 2020-12-31
Posts: 3

How to track vehicle traffic in real time on the OSMmap using the loca

I'm trying to create an application that tracking the movement of vehicles, so I first plotted stop icons on the map, and now I want the vehicle movement to be displayed on the map. I found a location_package with which I can do this. Also, since I use OSM maps , it is flutter_map package(I saw a solution for google maps, but I need to use free maps), then I need to display the traffic on it and I don't understand where to give the code that I wrote below.

Also I wanted not just animate this markers but take the real live movement.

Maybe I need to use a different package to solve my issues?

Also I take all longitude and latitude from my own API and the code ise down bellow.

Now i am using this code below to display the all icons of moving transport but it dosen't work and I am confused.

Can you guys give me a hint what I am missingin this project?

the code

class TransportMove extends StatefulWidget {
  @override
  _TransportMoveState createState() => _TransportMoveState();
}

class _TransportMoveState extends State<TransportMove> {
  List<LocVeh> listDir;
  Future <List<LocVeh>> futureMove;
  List<Marker> allMarkers = [];

  @override

  void initState() {
    super.initState();
    futureMove = fetchLoc();
  }

 
  Widget build(BuildContext context) {
    return FutureBuilder<List<LocVeh>>(
      future: futureMove,
        builder: (context, snapshot){
        if(snapshot.hasData){
        }else if(snapshot.hasError){
          listDir = snapshot.data;
          listDir.forEach((element) {
            allMarkers = listDir.map((e) => Marker(
              point: latLng.LatLng(e.uLong, e.uLat),
                  width: 10,
              height: 10,
                builder: (ctx) => Icon(Icons.directions_car, color: Colors.green,)
            )).toList(growable: true);
          });
          return Text("${snapshot.error}");
        }return CircularProgressIndicator();
        }
        );
  }
}

class LocationTraking {
  LocVeh _currentLocation;

  StreamController<LocVeh> _locationController = StreamController<LocVeh>();

  Stream<LocVeh> get locationStream => _locationController.stream;


  LocationService() {
    // Request permission to use location
    location.requestPermission().then((granted) {
      if (granted != null) {
        // If granted listen to the onLocationChanged stream and emit over our controller
        location.onLocationChanged.listen((locationData) {
          if (locationData != null) {
            _locationController.add(LocVeh(
              uLat: locationData.latitude,
              uLong: locationData.longitude,
            ));
          }
        });
      }
    });
  }



  var location = Location();

  Future<LocVeh> getLocation() async {
    try {
      var userLocation = await location.getLocation();
      _currentLocation = LocVeh(
        uLat: userLocation.latitude,
        uLong: userLocation.longitude,
      );
    } on Exception catch (e) {
      print('not work');
    }
    return _currentLocation;
  }
}
the API with long and lat which I give to transport on flutter map

class LocVeh {
  LocVeh({
    this.srvId,
    this.ttId,
    this.uTimenav,
    this.uLat,
    this.uLong,
    this.uSpeed,
    this.uCourse,
  });

  int srvId;
  int ttId;
  String uModel;
  DateTime uTimenav;
  double uLat;
  double uLong;
  int uSpeed;
  int uCourse;

  factory LocVeh.fromJson(Map<String, dynamic> json) => LocVeh(
    srvId: json["srv_id"],

    ttId: json["tt_id"],
    uTimenav: DateTime.parse(json["u_timenav"]),
    uLat: json["u_lat"].toDouble(),
    uLong: json["u_long"].toDouble(),
    uSpeed: json["u_speed"],
    uCourse: json["u_course"],
  );

  Map<String, dynamic> toJson() => {
    "srv_id": srvId,
    "tt_id": ttId,
    "u_model": uModel,
    "u_timenav": uTimenav.toIso8601String(),
    "u_lat": uLat,
    "u_long": uLong,
    "u_speed": uSpeed,
    "u_course": uCourse,
  };
}

Last edited by annahernandez99 (2021-01-23 09:33:02)

Offline

#2 2021-10-10 09:32:59

Abdulrahman Alhatti
New Member
Registered: 2021-10-10
Posts: 1

Re: How to track vehicle traffic in real time on the OSMmap using the loca

please if you solved the problem tell me, i faced the same problem

Offline

#3 2021-10-11 20:21:02

Düzeltme
Member
Registered: 2021-09-12
Posts: 8

Re: How to track vehicle traffic in real time on the OSMmap using the loca

Please remove the link to yesornogenerator.com since it does not contain anything related to the question.

Offline

Board footer

Powered by FluxBB