Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 1/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 2/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
F
apps, such as the next ride-hailing application, building it with Flutter
couldn’t be easier, thanks to the help of available Flutter packages that
facilitate its execution such as the Flutter Location Plugin that wraps
the complexities of location handling from your device and exposes its
capabilities in a simple to use API.
Let’s proceed!
Note: This post assumes you already have the maps set up in your project using
the Google Maps Flutter Package, as well as have your own Google Maps API
key. If not, follow this link on how to set up your Flutter project to work with
Google Maps. Other dependencies include the Flutter Polyline Points package
as well as the Flutter Location Plugin mentioned above. Github project for this
tutorial here.
Initial Setup
Make sure you prep your environment accordingly in order to enable
location tracking on both IOS and Android by following the steps in the
package’s README regarding the Android manifest file and the iOS
Info.plist.
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 3/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
Once setup, make sure your dependencies are correctly stated on your
pubspec.yaml.
...
dependencies:
flutter:
sdk: flutter
google_maps_flutter: ^0.5.11+1
location: ^2.3.5
flutter_polyline_points: ^0.1.0
...
Note: As of this writing, the versions of the packages above were the ones
available — please update accordingly.
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 4/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
import
‘package:flutter_polyline_points/flutter_polyline_points.dart’;
import ‘package:google_maps_flutter/google_maps_flutter.dart’;
import ‘package:location/location.dart’;
import ‘package:flutter/material.dart’;
import ‘dart:async’;
But the most important properties to pay attention to are the Location and
LocationData properties below. The Location’s location property will hold
a reference to the location information in a very encapsulated way, by
providing a set of APIs to retrieve the user’s current location as well as
hooks to obtain notifications to real-time changes in their location. The
LocationData’s currentLocation is how the Location reference exposes the
user’s location information, providing properties such as the lat, long,
speed, accuracy, altitude, etc. I’ll also hold on to the destination location in a
reference of type LocationData for consistency.
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 6/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
Let’s proceed to handle the initial state of this widget and set some listeners.
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 7/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
@override
void initState() {
super.initState();
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 9/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
Let’s override the build method, where the Google Map will be displayed
full screen on our widget. I wrap my Google Map inside a Stack as the body
of a Scaffold so as not to limit myself and to overlay things on top of my
map (more on that later).
@override
Widget build(BuildContext context) {
if (currentLocation != null) {
initialCameraPosition = CameraPosition(
target: LatLng(currentLocation.latitude,
currentLocation.longitude),
zoom: CAMERA_ZOOM,
tilt: CAMERA_TILT,
bearing: CAMERA_BEARING
);
}
return Scaffold(
body: Stack(
children: <Widget>[
GoogleMap(
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 10/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
myLocationEnabled: true,
compassEnabled: true,
tiltGesturesEnabled: false,
markers: _markers,
polylines: _polylines,
mapType: MapType.normal,
initialCameraPosition: initialCameraPosition,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
...
Notice how I set up the initial camera position based on the current user’s
initial location if it’s available at the time the widget builds the Map
(initialCameraPosition), otherwise I have a default start location to go
with just in case. The piece of code to pay attention here is the
onMapCreated event, which I handle by obtaining the reference to the
Google Map’s controller, and since the map is ready to be manipulated
when this gets called, I proceed to show my pins on the map
(showPinsOnMap).
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 11/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
void showPinsOnMap() {
// destination pin
_markers.add(Marker(
markerId: MarkerId('destPin'),
position: destPosition,
icon: destinationIcon
));
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 12/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
...
For drawing my polylines / routes on the map, I’m using both LocationData
references (currentLocation and destinationLocation) to get the complete
route between those two points (follow this tutorial for more reference).
if(result.isNotEmpty){
result.forEach((PointLatLng point){
polylineCoordinates.add(
LatLng(point.latitude,point.longitude)
);
});
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 13/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
setState(() {
_polylines.add(Polyline(
width: 5, // set the width of the polylines
polylineId: PolylineId(“poly”),
color: Color.fromARGB(255, 40, 122, 198),
points: polylineCoordinates
));
});
}
}
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 14/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
// updated position
var pinPosition = LatLng(currentLocation.latitude,
currentLocation.longitude);
_markers.add(Marker(
markerId: MarkerId(‘sourcePin’),
position: pinPosition, // updated position
icon: sourceIcon
));
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 15/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
});
}
Let’s dissect it for a bit: the first thing we do upon executing the
updatePinOnMap method is create a new CameraLocation object using
the updated user’s location (currentLocation)and animating the camera so
it moves along with the marker pin. If we don’t do this, we will see the pin
just move out of view and we end up staring at a blank map.
Now, the whole trick to simulate the movement of the ping upon the
location changing is removing the existing marker and adding it again
using the updated coordinates. We remove it by finding it within our list of
markers by id (sourcePin) so we don’t remove the destination pin or other
pins we may decide to place on our map, then spawning a new one using
the updated location coordinates and re-adding it to our markers’
collection. We do all this within the setState() method to notify Flutter that
a widget rebuild is due.
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 16/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
you want to compromise accuracy for performance (as long as you don’t drain
your user’s battery in the process!
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 17/19
28/12/2019 Implement Real-time Location Updates on Google Maps in your Flutter Apps
Here’s the link to the Github Project for this tutorial for your reference.
Hope you have found this tutorial useful for your own projects.
Happy Fluttering!
)
https://medium.com/@romanejaquez/implement-real-time-location-updates-on-google-maps-in-flutter-235c8a09173e 19/19