giroprotagonist
giroprotagonist

Reputation: 156

Flutter polyline distance with google_maps_flutter plugin

Hi I am using the google_maps_flutter plug in and have gotten a polyline to show up on my map. What I would like to do is calculate the distance of the polyline length. My code is linked here and there is a snippet below. I do not want to use the Directions API. How do I calculate polyline distance and print to console? Thanks for reading.

class ShowMap extends StatefulWidget {
  final double lat;
  final double lng;
  ShowMap({Key key, this.lat, this.lng}) : super(key: key);

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

class _ShowMapState extends State<ShowMap> {
  // Field
  double lat, lng;
  BitmapDescriptor policeIcon;
  List<Marker> list = List();
  List<String> listDocuments = List();
  final Set<Polyline> _polyline = {};
  GoogleMapController controller;
  List<LatLng> latlngSegment1 = List();
  List<LatLng> latlngSegment2 = List();
  static LatLng _lat1 = LatLng(45.19, -121.59);
  static LatLng _lat2 = LatLng(45.30, -122.20);
  static LatLng _lat3 = LatLng(45.11, -122.61);
  static LatLng _lat4 = LatLng(45.42, -122.62);
  static LatLng _lat5 = LatLng(45.34, -122.32);
  static LatLng _lat6 = LatLng(45.21, -122.2);
  bool _myLocationButtonEnabled = true;
  bool _myLocationEnabled = true;
  
// Method
  @override
  void initState() {
    super.initState();
    // findLatLng();

    readDataFromFirebase();

    setState(() {
      lat = widget.lat;
      lng = widget.lng;
      latlngSegment1.add(_lat1);
      latlngSegment1.add(_lat2);
      latlngSegment1.add(_lat3);
      latlngSegment1.add(_lat4);

      //line segment 2
      latlngSegment2.add(_lat4);
      latlngSegment2.add(_lat5);
      latlngSegment2.add(_lat6);
      latlngSegment2.add(_lat1);
    });
  }

Upvotes: 0

Views: 2593

Answers (1)

Nitesh
Nitesh

Reputation: 508

Let us say that you want to calculate the distance of polyline created by the points in latlngSegment1.

For that you need to calculate the distance between each consecutive LatLng points in latlngSegment1.

I would do it with something like this.

            double calculateDistane(List<LatLng> polyline) {
                double totalDistance = 0;
                for (int i = 0; i < polyline.length; i++) {
                  if (i < polyline.length - 1) { // skip the last index
                    totalDistance += getStraightLineDistance(
                        polyline[i + 1].latitude,
                        polyline[i + 1].longitude,
                        polyline[i].latitude,
                        polyline[i].longitude);
                  }
                }
               return totalDistance;
              }

              double getStraightLineDistance(lat1, lon1, lat2, lon2) {
                var R = 6371; // Radius of the earth in km
                var dLat = deg2rad(lat2 - lat1);
                var dLon = deg2rad(lon2 - lon1);
                var a = math.sin(dLat / 2) * math.sin(dLat / 2) +
                    math.cos(deg2rad(lat1)) *
                        math.cos(deg2rad(lat2)) *
                        math.sin(dLon / 2) *
                        math.sin(dLon / 2);
                var c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a));
                var d = R * c; // Distance in km
                return d * 1000; //in m
              }

              dynamic deg2rad(deg) {
                return deg * (math.pi / 180);
              }

Note: The getStraightLineDistance() function gives the straight line distance between two latlng points, which might not be the way someone reaches from point A to B.

Upvotes: 1

Related Questions