扑。谷歌地图。我需要从屏幕上的 LatLng(纬度、经度)列表中找到 LatLngBounds?

Flutter. GoogleMap. I need to find LatLngBounds from a list of LatLng(latitude, longitude) on screen?

我正在使用这个包:[google_maps_flutter: ^2.0.1][1]

我有这个 list of LatLng:

List<LatLng> _list = [
  LatLng(PointA.lat, PointA.lng), // Point A
  LatLng(PointB.lat, PointB.lng), // Point B
  LatLng(PointC.lat, PointC.lng), // Point C
  LatLng(PointD.lat, PointD.lng), // Point D
];

我需要一个 CameraPosition,其中包含所有这些要点:

  final _cameraPosition = CameraUpdate.newLatLngBounds(
    LatLngBounds(
      southwest: LatLng(southwestPoint.lat,southwestPoint.lng), // southwest Point
      northeast: LatLng(northeastPoint.lat,northeastPoint.lng)  // northeast Point
      ), padding);

图形演示

您可以使用 LatLngBounds.extend:

var bounds = LatLngBounds();
for (var latlng in _list) bounds.extend(latlng);

编辑:此代码适用于 google_maps 包,但 google_maps_flutter 中的 LatLng 未公开此方法。

一个简单的解决方案是计算 lat/lng 的 max/min:

LatLngBounds computeBounds(List<LatLng> list) {
  assert(list.isNotEmpty);
  var firstLatLng = list.first;
  var s = firstLatLng.latitude,
      n = firstLatLng.latitude,
      w = firstLatLng.longitude,
      e = firstLatLng.longitude;
  for (var i = 1; i < list.length; i++) {
    var latlng = list[i];
    s = min(s, latlng.latitude);
    n = max(n, latlng.latitude);
    w = min(w, latlng.longitude);
    e = max(e, latlng.longitude);
  }
  return LatLngBounds(southwest: LatLng(s, w), northeast: LatLng(n, e));
}

如果您将使用此点进行路由。几乎所有地图 API 提供者都准确地提供了您想要查找的内容。 例如google方向API

{
    "geocoded_waypoints": [
        {
            "geocoder_status": "OK",
            "place_id": "ChIJm9bNDWCKrjgRhKxkH0DyMrk",
            "types": [
                "route"
            ]
        },
        {
            "geocoder_status": "OK",
            "place_id": "ChIJhSjM65n1rjgRfAbHPs9vR_E",
            "types": [
                "route"
            ]
        }
    ],
    "routes": [
        {
            "bounds": {
                "northeast": {
                    "lat": 41.307515,
                    "lng": 69.3458742
                },
                "southwest": {
                    "lat": 41.2780856,
                    "lng": 69.2386449
                }
            },
            "copyrights": "Map data ©2020",
            "legs": [
                {
                    "distance": {
                        "text": "11.4 km",
                        "value": 11355
                    },
                    "duration": {
                        "text": "20 mins",
                        "value": 1189
                    },
                    "end_address": "Павел Садименко кўчаси, Тошкент, Uzbekistan",
                    "end_location": {
                        "lat": 41.2965501,
                        "lng": 69.3458742
                    },
                    "start_address": "2-й проезд Эхтиром, Тошкент, Uzbekistan",
                    "start_location": {
                        "lat": 41.2795665,
                        "lng": 69.2389022
                    },
                    "steps": [
                        {
                            "distance": {
                                "text": "27 m",
                                "value": 27
                            },
                            "duration": {
                                "text": "1 min",
                                "value": 10
                            },
                            "end_location": {
                                "lat": 41.2797191,
                                "lng": 69.2386449
                            },
                            "html_instructions": "Head <b>northwest</b> on <b>2-й проезд Эхтиром</b> toward <b>Ekhtirom Str.</b>",
                            "polyline": {
                                "points": "il}zFcfreL]r@"
                            },
                            "start_location": {
                                "lat": 41.2795665,
                                "lng": 69.2389022
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "0.2 km",
                                "value": 211
                            },
                            "duration": {
                                "text": "1 min",
                                "value": 64
                            },
                            "end_location": {
                                "lat": 41.2811787,
                                "lng": 69.2402548
                            },
                            "html_instructions": "Turn <b>right</b> onto <b>Ekhtirom Str.</b>",
                            "maneuver": "turn-right",
                            "polyline": {
                                "points": "gm}zFodreLyAaBqAwAwBgC"
                            },
                            "start_location": {
                                "lat": 41.2797191,
                                "lng": 69.2386449
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "47 m",
                                "value": 47
                            },
                            "duration": {
                                "text": "1 min",
                                "value": 28
                            },
                            "end_location": {
                                "lat": 41.2809113,
                                "lng": 69.2406852
                            },
                            "html_instructions": "Turn <b>right</b> onto <b>Muqimiy ko'chasi</b>",
                            "maneuver": "turn-right",
                            "polyline": {
                                "points": "kv}zFqnreLt@wA"
                            },
                            "start_location": {
                                "lat": 41.2811787,
                                "lng": 69.2402548
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "0.5 km",
                                "value": 535
                            },
                            "duration": {
                                "text": "1 min",
                                "value": 32
                            },
                            "end_location": {
                                "lat": 41.2780856,
                                "lng": 69.2457843
                            },
                            "html_instructions": "Slight <b>left</b>",
                            "maneuver": "turn-slight-left",
                            "polyline": {
                                "points": "ut}zFiqreL@SBIJWVg@l@kArAeCbB_DtBwDBE@CTc@Ta@Vg@Ta@d@_AFQHYBOHc@Fw@"
                            },
                            "start_location": {
                                "lat": 41.2809113,
                                "lng": 69.2406852
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "1.1 km",
                                "value": 1074
                            },
                            "duration": {
                                "text": "2 mins",
                                "value": 109
                            },
                            "end_location": {
                                "lat": 41.2812759,
                                "lng": 69.257887
                            },
                            "html_instructions": "Continue onto <b>Kostrov Str.</b>",
                            "polyline": {
                                "points": "ac}zFcqseL?oAEe@AMa@wBScAuAyH{@_F_@qBEUMq@e@oCuA{HyBiMKg@k@uCYyACMm@eD"
                            },
                            "start_location": {
                                "lat": 41.2780856,
                                "lng": 69.2457843
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "3.5 km",
                                "value": 3478
                            },
                            "duration": {
                                "text": "7 mins",
                                "value": 403
                            },
                            "end_location": {
                                "lat": 41.2999713,
                                "lng": 69.290857
                            },
                            "html_instructions": "Continue onto <b>Nukus ko'chasi</b>",
                            "polyline": {
                                "points": "_w}zFy|ueLO{@G[AAIIOs@AIG[e@aCIc@_AcDOc@iBiGOc@g@eBuAqEUu@Og@M_@Qi@u@{B{@iCa@mASk@AAiA_Do@gB{@}BAYSk@a@iAACiA_Du@wBa@kAKQSc@gBkCyBaDw@mAs@cAWa@wB}CSGGKSYGMIKe@u@yAuB[i@qCmEYg@o@kAq@mAkAwBmBkD_@q@MYk@kAm@kAe@cAs@qA}BkEa@u@aBaDeBsCWe@gAiBcAoBsAeCqBwDc@y@qA}BeAkBeAmBc@u@}@aBgAsBU[U[IGg@q@"
                            },
                            "start_location": {
                                "lat": 41.2812759,
                                "lng": 69.257887
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "0.6 km",
                                "value": 623
                            },
                            "duration": {
                                "text": "1 min",
                                "value": 50
                            },
                            "end_location": {
                                "lat": 41.3039146,
                                "lng": 69.29613519999999
                            },
                            "html_instructions": "Continue onto <b>Abdulla Qodiriy ko'chasi</b>",
                            "polyline": {
                                "points": "yka{F{j|eLSSu@u@QOMMsA_BmBgCmBuCk@cAeAeB}AmCkAqBGKuAcC"
                            },
                            "start_location": {
                                "lat": 41.2999713,
                                "lng": 69.290857
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "0.1 km",
                                "value": 123
                            },
                            "duration": {
                                "text": "1 min",
                                "value": 8
                            },
                            "end_location": {
                                "lat": 41.3042791,
                                "lng": 69.29752569999999
                            },
                            "html_instructions": "Slight <b>right</b>",
                            "maneuver": "turn-slight-right",
                            "polyline": {
                                "points": "mdb{F{k}eLIo@Ow@Y{AIg@Ki@"
                            },
                            "start_location": {
                                "lat": 41.3039146,
                                "lng": 69.29613519999999
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "0.4 km",
                                "value": 358
                            },
                            "duration": {
                                "text": "1 min",
                                "value": 30
                            },
                            "end_location": {
                                "lat": 41.3073594,
                                "lng": 69.2984756
                            },
                            "html_instructions": "Turn <b>left</b>",
                            "maneuver": "turn-left",
                            "polyline": {
                                "points": "wfb{Fqt}eL[Qs@]kAo@EAcAc@sAe@o@Gi@EWA_AAy@BMAs@A"
                            },
                            "start_location": {
                                "lat": 41.3042791,
                                "lng": 69.29752569999999
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "0.8 km",
                                "value": 760
                            },
                            "duration": {
                                "text": "2 mins",
                                "value": 102
                            },
                            "end_location": {
                                "lat": 41.3052384,
                                "lng": 69.30680590000001
                            },
                            "html_instructions": "Turn <b>right</b> onto <b>Taraqqiyot ko'chasi</b>",
                            "maneuver": "turn-right",
                            "polyline": {
                                "points": "_zb{Foz}eLE[SgCCe@Bw@Bq@D{ABg@?I@MBe@D]BUFm@@GFe@BSD[J{@D[NeA?CHg@DWDUBONg@Rs@Vu@h@wA^_AbAgCXo@JWf@oANe@Ni@Ri@"
                            },
                            "start_location": {
                                "lat": 41.3073594,
                                "lng": 69.2984756
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "2.4 km",
                                "value": 2426
                            },
                            "duration": {
                                "text": "3 mins",
                                "value": 168
                            },
                            "end_location": {
                                "lat": 41.30553159999999,
                                "lng": 69.3352651
                            },
                            "html_instructions": "Continue straight onto <b>улица Махтумкули</b>",
                            "maneuver": "straight",
                            "polyline": {
                                "points": "wlb{Fqn_fL^{AFa@Jk@ZwBRmBLaAHu@Z{CVuBPcBFm@RcBJcAJ}@TcBZsCDa@VsBBUNqAL}A?ID_A?eAAwAAWCUCQCWa@aDGm@WoBYuBWkBOwAYiDG_AC_@C_@I_AG_A[uFKaC?GA[EoCA[IqBOsDAm@CUGsAMaCAWIwBE_AYgIGuCWmFMeCAQMuDE_C"
                            },
                            "start_location": {
                                "lat": 41.3052384,
                                "lng": 69.30680590000001
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "0.3 km",
                                "value": 333
                            },
                            "duration": {
                                "text": "1 min",
                                "value": 26
                            },
                            "end_location": {
                                "lat": 41.3058117,
                                "lng": 69.33922
                            },
                            "html_instructions": "Keep <b>right</b>",
                            "maneuver": "keep-right",
                            "polyline": {
                                "points": "qnb{Fm`efLKgEc@wHCy@Cg@Cs@@{A@c@"
                            },
                            "start_location": {
                                "lat": 41.30553159999999,
                                "lng": 69.3352651
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "0.3 km",
                                "value": 298
                            },
                            "duration": {
                                "text": "1 min",
                                "value": 24
                            },
                            "end_location": {
                                "lat": 41.30402780000001,
                                "lng": 69.3418039
                            },
                            "html_instructions": "At the roundabout, take the <b>1st</b> exit",
                            "maneuver": "roundabout-right",
                            "polyline": {
                                "points": "ipb{FcyefL?AFKBMBI^{@Xq@Ze@xBaCRUlA{ALSFQBSF["
                            },
                            "start_location": {
                                "lat": 41.3058117,
                                "lng": 69.33922
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "0.1 km",
                                "value": 124
                            },
                            "duration": {
                                "text": "1 min",
                                "value": 8
                            },
                            "end_location": {
                                "lat": 41.3031531,
                                "lng": 69.3427282
                            },
                            "html_instructions": "Turn <b>right</b> onto <b>Parkent ko'chasi</b>",
                            "maneuver": "turn-right",
                            "polyline": {
                                "points": "eeb{FgiffLhCeCd@s@"
                            },
                            "start_location": {
                                "lat": 41.30402780000001,
                                "lng": 69.3418039
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "0.2 km",
                                "value": 182
                            },
                            "duration": {
                                "text": "1 min",
                                "value": 20
                            },
                            "end_location": {
                                "lat": 41.3017832,
                                "lng": 69.3438792
                            },
                            "html_instructions": "Turn <b>right</b>",
                            "maneuver": "turn-right",
                            "polyline": {
                                "points": "u_b{FaoffL\CpAeA`D{C"
                            },
                            "start_location": {
                                "lat": 41.3031531,
                                "lng": 69.3427282
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "0.4 km",
                                "value": 408
                            },
                            "duration": {
                                "text": "1 min",
                                "value": 46
                            },
                            "end_location": {
                                "lat": 41.29828510000001,
                                "lng": 69.3424684
                            },
                            "html_instructions": "Turn <b>right</b> onto <b>Қорасув кўчаси</b>",
                            "maneuver": "turn-right",
                            "polyline": {
                                "points": "cwa{FgvffL~@DrC~@`AZz@XvE|AxAb@t@Z"
                            },
                            "start_location": {
                                "lat": 41.3017832,
                                "lng": 69.3438792
                            },
                            "travel_mode": "DRIVING"
                        },
                        {
                            "distance": {
                                "text": "0.3 km",
                                "value": 348
                            },
                            "duration": {
                                "text": "1 min",
                                "value": 61
                            },
                            "end_location": {
                                "lat": 41.2965501,
                                "lng": 69.3458742
                            },
                            "html_instructions": "Turn <b>left</b> onto <b>Павел Садименко кўчаси</b>",
                            "maneuver": "turn-left",
                            "polyline": {
                                "points": "iaa{FmmffLv@oDLk@Lk@Xu@Zy@Xe@r@gAr@mANWn@eANU"
                            },
                            "start_location": {
                                "lat": 41.29828510000001,
                                "lng": 69.3424684
                            },
                            "travel_mode": "DRIVING"
                        }
                    ],
                    "traffic_speed_entry": [],
                    "via_waypoint": []
                }
            ],
            "overview_polyline": {
                "points": "il}zFcfreL]r@yAaBiE_Ft@wA@SNa@|FyKpCeFhBkD^_BFw@?oAGs@gEuUyAiIoEeWw@}D{AiII]IIOs@Ie@o@eDiEuNcDuKqCoIaC{GkBeFAYSk@c@mA_CwGm@}ASc@gBkCqDoFkAeBwB}CSG[e@qCeEmDwFiAsBkGcLmCuFuHuN}ByDkCyE{HuNmFqJgAsBU[_@c@{@eAgAeAaBmBmBgCmBuCqBiDiD_G}AoCYgBc@cCKi@[Q_CmAiAe@sAe@o@GaAG_AAy@BaACYcDCe@Bw@HmCBq@NgB|@iHTyARw@j@iBfDoIr@gB^oARi@^{ARmAZwBRmBVwBlAcL`AiI|@_I\oDDiAA}CEm@q@yFyAiLi@iHQ_Cg@wJIoE_@iJa@eJ_@gKGuCWmFOwCSuHKgEc@wHGaBCs@@{A@e@n@_BXq@Ze@lCwClA{ALSJe@F[hCeCd@s@\CpAeA`D{C~@DrC~@|Bt@pH`Ct@Zv@oDZwAt@oBlAmBbCaE"
            },
            "summary": "Nukus ko'chasi",
            "warnings": [],
            "waypoint_order": []
        }
    ],
    "status": "OK"
}
  bounds: LatLngBounds(
      northeast: LatLng(json['routes'][0]['bounds']['northeast']['lat'], json['routes'][0]['bounds']['northeast']['lng']),
      southwest: LatLng(json['routes'][0]['bounds']['southwest']['lat'], json['routes'][0]['bounds']['southwest']['lng']),
  )