将Http中的坐标提取到数组中

问题描述 投票:0回答:1

我需要从http请求中获取一组坐标,并将其输入我定义的数组中,并在Google Maps中绘制标记。以下是我的代码:

final List<LatLng> _markerLocations = [
    LatLng(3.082519, 101.592201),
    LatLng(3.081758, 101.583433),
    LatLng(3.079726, 101.581876),
    LatLng(3.082197, 101.589534),
    LatLng(3.073675, 101.593028)
  ];
_initMarkers() async {
    final List<MapMarker> _markers = [];

    for (LatLng markerLocation in _markerLocations) {
      // final BitmapDescriptor markerImage =
      //     await MapHelper.getMarkerImageFromUrl(_markerImageUrl);

      _markers.add(
        MapMarker(
          id: _markerLocations.indexOf(markerLocation).toString(),
          position: markerLocation,
          icon: BitmapDescriptor.defaultMarker,
        ),
      );
    }

    _clusterManager = await MapHelper.initClusterManager(
      _markers,
      _minClusterZoom,
      _maxClusterZoom,
    );

    await _updateMarkers();
  }

这是http请求的正文:

{
    "status": true,
    "statusMsg": null,
    "errorCode": null,
    "body": {
        "rondaLoopCheckpointsList": [
            {
                "pkRondaLoopCheckpointsId": "11f1c165-5647-44d4-a2fd-1d4c8071f5f7",
                "rondaLoop": {
                    "pkRondaLoopId": "42ef5505-e739-45f1-bff3-194d9e1329d5",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "name": "SHA-USJ(LOOP1)",
                    "description": "Shah Alam - USJ"
                },
                "checkpoints": {
                    "pkCheckpointId": "8b915f65-b4d1-4cf5-81e7-796d943fe7ab",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "cardinalDirection": {
                        "key": 293,
                        "value": "N/A",
                        "description": "Tidak Berkenaan"
                    },
                    "locationType": {
                        "key": 49,
                        "value": "O/H Bridge Restaurant",
                        "description": null
                    },
                    "kilometerMarker": 8.50,
                    "latitude": "3.03000000",
                    "longtitude": "101.58000000",
                    "name": "USJ OBR"
                },
                "sequence": 6
            },
            {
                "pkRondaLoopCheckpointsId": "4000600c-e376-4412-94ad-bd13b454b126",
                "rondaLoop": {
                    "pkRondaLoopId": "42ef5505-e739-45f1-bff3-194d9e1329d5",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "name": "SHA-USJ(LOOP1)",
                    "description": "Shah Alam - USJ"
                },
                "checkpoints": {
                    "pkCheckpointId": "8b915f65-b4d1-4cf5-81e7-796d943fe7ab",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "cardinalDirection": {
                        "key": 293,
                        "value": "N/A",
                        "description": "Tidak Berkenaan"
                    },
                    "locationType": {
                        "key": 49,
                        "value": "O/H Bridge Restaurant",
                        "description": null
                    },
                    "kilometerMarker": 8.50,
                    "latitude": "3.03000000",
                    "longtitude": "101.58000000",
                    "name": "USJ OBR"
                },
                "sequence": 4
            },
            {
                "pkRondaLoopCheckpointsId": "522c066f-3c59-46e7-9bec-9f2de955874d",
                "rondaLoop": {
                    "pkRondaLoopId": "42ef5505-e739-45f1-bff3-194d9e1329d5",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "name": "SHA-USJ(LOOP1)",
                    "description": "Shah Alam - USJ"
                },
                "checkpoints": {
                    "pkCheckpointId": "3566c7ba-6ac4-4e07-b035-f50a5ca53702",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "cardinalDirection": {
                        "key": 293,
                        "value": "N/A",
                        "description": "Tidak Berkenaan"
                    },
                    "locationType": {
                        "key": 51,
                        "value": "Toll Plaza",
                        "description": null
                    },
                    "kilometerMarker": 9.00,
                    "latitude": "3.03000000",
                    "longtitude": "101.58000000",
                    "name": "USJ T/P"
                },
                "sequence": 5
            },
            {
                "pkRondaLoopCheckpointsId": "8a0f7e2b-5f8b-4d52-bfb5-846910c8e8c3",
                "rondaLoop": {
                    "pkRondaLoopId": "42ef5505-e739-45f1-bff3-194d9e1329d5",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "name": "SHA-USJ(LOOP1)",
                    "description": "Shah Alam - USJ"
                },
                "checkpoints": {
                    "pkCheckpointId": "58c0f7b2-550e-4cff-a9d5-a817c81ad55a",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "cardinalDirection": {
                        "key": 293,
                        "value": "N/A",
                        "description": "Tidak Berkenaan"
                    },
                    "locationType": {
                        "key": 51,
                        "value": "Toll Plaza",
                        "description": null
                    },
                    "kilometerMarker": 4.50,
                    "latitude": "3.06000000",
                    "longtitude": "101.56000000",
                    "name": "Ebor (S) T/P"
                },
                "sequence": 8
            },
            {
                "pkRondaLoopCheckpointsId": "95aaae00-ba04-4d75-a1ec-366c58cd21a3",
                "rondaLoop": {
                    "pkRondaLoopId": "42ef5505-e739-45f1-bff3-194d9e1329d5",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "name": "SHA-USJ(LOOP1)",
                    "description": "Shah Alam - USJ"
                },
                "checkpoints": {
                    "pkCheckpointId": "8404710d-05ab-497b-8777-443007c6b277",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "cardinalDirection": {
                        "key": 293,
                        "value": "N/A",
                        "description": "Tidak Berkenaan"
                    },
                    "locationType": {
                        "key": 51,
                        "value": "Toll Plaza",
                        "description": null
                    },
                    "kilometerMarker": 5.40,
                    "latitude": "3.05000000",
                    "longtitude": "101.57000000",
                    "name": "Seafield T/P"
                },
                "sequence": 7
            },
            {
                "pkRondaLoopCheckpointsId": "98bb4343-a187-411d-a363-ec27a35766f5",
                "rondaLoop": {
                    "pkRondaLoopId": "42ef5505-e739-45f1-bff3-194d9e1329d5",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "name": "SHA-USJ(LOOP1)",
                    "description": "Shah Alam - USJ"
                },
                "checkpoints": {
                    "pkCheckpointId": "499254b5-ea65-4b32-8fa2-d36ee77f12b7",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "c179476a-7fa5-4189-9ed0-1abd8ee4f8c2",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C4",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "cardinalDirection": {
                        "key": 293,
                        "value": "N/A",
                        "description": "Tidak Berkenaan"
                    },
                    "locationType": {
                        "key": 51,
                        "value": "Toll Plaza",
                        "description": null
                    },
                    "kilometerMarker": 0.00,
                    "latitude": "3.10000000",
                    "longtitude": "101.55000000",
                    "name": "Shah Alam T/B"
                },
                "sequence": 1
            },
            {
                "pkRondaLoopCheckpointsId": "e63b7424-d3c9-4266-bf30-91db1751b2eb",
                "rondaLoop": {
                    "pkRondaLoopId": "42ef5505-e739-45f1-bff3-194d9e1329d5",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "name": "SHA-USJ(LOOP1)",
                    "description": "Shah Alam - USJ"
                },
                "checkpoints": {
                    "pkCheckpointId": "8404710d-05ab-497b-8777-443007c6b277",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "cardinalDirection": {
                        "key": 293,
                        "value": "N/A",
                        "description": "Tidak Berkenaan"
                    },
                    "locationType": {
                        "key": 51,
                        "value": "Toll Plaza",
                        "description": null
                    },
                    "kilometerMarker": 5.40,
                    "latitude": "3.05000000",
                    "longtitude": "101.57000000",
                    "name": "Seafield T/P"
                },
                "sequence": 3
            },
            {
                "pkRondaLoopCheckpointsId": "f0afa11b-a2ac-4f27-8b2c-3279f87ddd69",
                "rondaLoop": {
                    "pkRondaLoopId": "42ef5505-e739-45f1-bff3-194d9e1329d5",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "name": "SHA-USJ(LOOP1)",
                    "description": "Shah Alam - USJ"
                },
                "checkpoints": {
                    "pkCheckpointId": "df923a70-dd5a-49fa-ad0d-5224106fcbab",
                    "company": {
                        "pkCompanyId": "f77819c5-97b8-41be-a745-33f907353e9e",
                        "name": null,
                        "phone": null
                    },
                    "section": {
                        "pkSectionId": "2cd43029-71aa-43a4-8aa2-7e2fbf1ecb1f",
                        "company": null,
                        "sectionNumber": null,
                        "sectionName": "C6",
                        "description": null,
                        "region": null,
                        "mapcenter": null,
                        "mapzoom": null
                    },
                    "cardinalDirection": {
                        "key": 293,
                        "value": "N/A",
                        "description": "Tidak Berkenaan"
                    },
                    "locationType": {
                        "key": 51,
                        "value": "Toll Plaza",
                        "description": null
                    },
                    "kilometerMarker": 4.30,
                    "latitude": "3.07000000",
                    "longtitude": "101.56000000",
                    "name": "Ebor (U) T/P"
                },
                "sequence": 2
            }
        ]
    }
}

现在我需要从latitude数组中获取longitudecheckpoints。从这些坐标中,我可以使用我定义的for循环函数将其绘制到地图中。

应该将#_markerLocations#数组中的硬编码坐标替换为http正文中的lat和lng#

flutter
1个回答
0
投票

将其保留在执行HTTP请求的位置。

final Response response = await http.get("Your URL");

final String body = response.body;
// your list.
List<LatLng> _latLngs = _parseLatLngsFromJSON(jsonDecode(body));

以下是将解析您的JSON的方法。

List<LatLng> _parseLatLngsFromJSON(Map<String, dynamic> data) {
  List<Map<String, dynamic>> rondaLoopCheckpoints =
      data["body"]["rondaLoopCheckpointsList"];
      List<LatLng> latLngData = <LatLng>[];
  rondaLoopCheckpoints.forEach((Map<String, dynamic> item) {
    Map<String, dynamic> checkPoint = item["checkpoints"];
    latLngData.add(LatLng(
      num.tryParse(checkPoint["latitude"]),
      num.tryParse(checkPoint["longtitude"]),
    ));
  });
  return latLngData;
}

现在您有了所需的LatLng列表。

[如果您要演示,see this demo希望对您有所帮助。如果您有任何疑问,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.