I'm a beginner in Flutter and I'm currently building a map app that display a map with current user's continuous location using Flutter_map plugin and I used marker to display it ..
It really working fine with me and the camera move well according to user's location but, the thing is that the marker actually jump from current position to new position, and I'd like to make the marker move in smoother way from a given latlon location to another one like we see in google maps user location marker. Is it possible to update it on the map by animation ? if yes could anyone show me how ?
(I don't want google maps marker solution cuz I prefer working with Flutter_map and displaying a leaflet map)
Here is my piece of code:
import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:flutter_map/plugin_api.dart';
import 'package:geolocator/geolocator.dart';
import 'package:latlong2/latlong.dart' as latLng;
import 'package:car_speed_limit/Components/constants.dart';
import 'dart:async';
//ignore: must_be_immutable
class RoadMap extends StatefulWidget {
var userLat;
var userLong;
var userSpeed;
RoadMap({this.userLat, this.userLong, this.userSpeed});
@override
_RoadMapState createState() => _RoadMapState();
}
class _RoadMapState extends State<RoadMap> with SingleTickerProviderStateMixin{
MapController? _mapController;
StreamSubscription<Position>? positionStreamSub2;
@override
void initState() {
super.initState();
_mapController = MapController();
positionStreamSub2= Geolocator.getPositionStream(
intervalDuration: Duration(milliseconds: 1000),
forceAndroidLocationManager: false,
desiredAccuracy: LocationAccuracy.best).listen((_updatedPosition) {
if (!mounted) {
return;
}
setState(() {
_mapController?.move(latLng.LatLng(widget.userLat, widget.userLong),16.8);
widget.userLat= _updatedPosition.latitude;
widget.userLong= _updatedPosition.longitude;
widget.userSpeed= (_updatedPosition.speed*(2.23693)).toInt();
});
print('THE ROAD STREAM $_updatedPosition');
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('${widget.userSpeed}'),
backgroundColor: Color(0xFF01051C),
leading: IconButton(
icon: Icon(
Icons.arrow_back,
color: kIconColor,
size: 35,
),
onPressed: () {
Navigator.of(context).pop();
positionStreamSub2?.cancel();
}
),
actions: [
Column(
children: [
Text('Lat: ${widget.userLat}',
style: TextStyle(
color: kIconColor,
)),
Text('Long: ${widget.userLong}',
style: TextStyle(
color: kIconColor,
)),
],
),
],
),
body: FlutterMap(
mapController: _mapController,
options: MapOptions(
minZoom: 2.6,
maxZoom: 18.0,
zoom: 17.0,
center: latLng.LatLng(widget.userLat,widget.userLong),
interactiveFlags: InteractiveFlag.pinchZoom | InteractiveFlag.drag | InteractiveFlag.pinchMove,
),
layers: [
TileLayerOptions(
urlTemplate: 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
subdomains: ['a','b','c'],
),
MarkerLayerOptions(
markers: [
Marker(
point: latLng.LatLng(widget.userLat,widget.userLong),
builder: (context) {
return Stack(
children:[
Center(
child: Container(
height: 30,
width: 30,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.tealAccent.withOpacity(0.5),
),
),
),
Center(
child: Container(
height: 17,
width: 17,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.tealAccent[700],
),
),
),
],
);
}
),
],
),
]
),
);
}
}