0

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);
    });
  }
giroprotagonist
  • 138
  • 1
  • 3
  • 15

1 Answers1

1

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.

Nitesh
  • 438
  • 2
  • 10
  • Thanks this gets me going in the right direction. The end goal is to show the users distance traveled along a static route. So I want to know the length of a static high resolution polyline route, and then show at what distance the user is along the route. Not meant for navigation, mostly for reference. Again thank you for your response, very helpful. – giroprotagonist Feb 13 '21 at 21:35
  • Hi Nitesh, I've now tried a few different variations of this code and I'm unable to make it work. Could you provide any more information of how I would get it to print the resulting distance to console? – giroprotagonist Jul 09 '21 at 21:27