如何从一个字符串创建一个LatLng列表?

huangapple go评论107阅读模式
英文:

Flutter how to create a List LatLng from a String?

问题

我需要你的帮助 如何从一个字符串创建一个LatLng列表?

在Flutter中,如果我创建一个像这样的LatLng列表(polygon1),我可以使用它来在Google地图上显示polygon1。这很好地工作

  1. List<LatLng> polygon1 = const [
  2. LatLng(48.885278, 2.349523),
  3. LatLng(48.885471, 2.349528),
  4. LatLng(48.888300, 2.349597)
  5. ];

我需要使用来自数据库的数据来创建相同的LatLng列表

我的数据是这个变量 'coord':

  1. var coord = "{[48.885278, 2.349523], [48.885471, 2.349528], [48.888300, 2.349597]}";

我如何将这个变量转换为一个LatLng列表,以便在GoogleMaps上显示一个来自这些LatLng的多边形

如果有人有时间帮助我,将非常非常友好 如何从一个字符串创建一个LatLng列表?

英文:

I need your help 如何从一个字符串创建一个LatLng列表?

in Flutter, if I create a List of LatLng like here (polygon1) I can use it to show polygon1 in a Google Maps. That work fine

  1. List&lt;LatLng&gt; polygon1 = const [
  2. LatLng(48.885278,2.349523),
  3. LatLng(48.885471,2.349528),
  4. LatLng(48.888300,2.349597)];

I need to create same List LatLng using data comming from a database

My data are like this variable 'coord' :

  1. var coord = &quot;{[48.885278, 2.349523], [48.885471,2.349528], [48.888300,2.349597]}&quot;;

how can I convert this variable to a List LatLng to be use to show
a polygon from this LatLng on a GoogleMaps

if somebody has time to help me it will be very very kind 如何从一个字符串创建一个LatLng列表?

答案1

得分: 1

以下是翻译好的部分:

这个字符串不是一个有效的JSON格式。

它需要像这样:

  1. var coord = "[[48.885278, 2.349523], [48.885471,2.349528], [48.888300,2.349597]]";

你可以使用这个方法:

  1. List<LatLng> parseLatLng(String jsonString) {
  2. var json = jsonDecode(jsonString);
  3. return (json as List).map((item) => LatLng(item[0], item[1])).toList();
  4. }

如果你需要处理它不是有效的JSON格式,你可以编辑上面的方法,让它变成这样:

  1. List<LatLng> parseLatLng(String jsonString) {
  2. var json = jsonDecode(jsonString);
  3. jsonString = jsonString.replaceAll(RegExp(r'[{}]'), ''); // 这样你就移除了大括号
  4. jsonString = '[$jsonString]'; // 然后你需要包装它成一个有效的JSON格式
  5. return (json as List).map((item) => LatLng(item[0], item[1])).toList();
  6. }

示例:

  1. void main() async {
  2. var coord = "[[48.885278, 2.349523], [48.885471,2.349528], [48.888300,2.349597]]";
  3. List<LatLng> polygon = parseLatLng(coord);
  4. }
  5. List<LatLng> parseLatLng(String jsonString) {
  6. var json = jsonDecode(jsonString);
  7. return (json as List).map((item) => LatLng(item[0], item[1])).toList();
  8. }

或者

  1. var coord = "[[48.885278, 2.349523], [48.885471,2.349528], [48.888300,2.349597]]";
  2. var json = jsonDecode(jsonString);
  3. List<LatLng> polygon = (json as List).map((item) => LatLng(item[0], item[1])).toList();
英文:

The string that you are using is not a valid JSON Format.

it needs to be like this:

  1. var coord = &quot;[[48.885278, 2.349523], [48.885471,2.349528], [48.888300,2.349597]]&quot;;

You can use this method:

  1. List&lt;LatLng&gt; parseLatLng(String jsonString) {
  2. var json = jsonDecode(jsonString);
  3. return (json as List).map((item) =&gt; LatLng(item[0], item[1])).toList();
  4. }

if you have to deal with it as not being valid JSON format you can edit the method above to be like this:

  1. List&lt;LatLng&gt; parseLatLng(String jsonString) {
  2. var json = jsonDecode(jsonString);
  3. jsonString = jsonString.replaceAll(RegExp(r&#39;[{}]&#39;), &#39;&#39;); // this way you remove curly braces
  4. jsonString = &#39;[$jsonString]&#39;; // and then you need to wrap it to be a valid JSON format
  5. return (json as List).map((item) =&gt; LatLng(item[0], item[1])).toList();
  6. }

Example:

  1. void main() async {
  2. var coord = &quot;[[48.885278, 2.349523], [48.885471,2.349528], [48.888300,2.349597]]&quot;;
  3. List&lt;LatLng&gt; polygon=parseLatLng(coord);
  4. }
  5. List&lt;LatLng&gt; parseLatLng(String jsonString) {
  6. var json = jsonDecode(jsonString);
  7. return (json as List).map((item) =&gt; LatLng(item[0], item[1])).toList();
  8. }

or

  1. var coord = &quot;[[48.885278, 2.349523], [48.885471,2.349528], [48.888300,2.349597]]&quot;;
  2. var json = jsonDecode(jsonString);
  3. List&lt;LatLng&gt; polygon=(json as List).map((item) =&gt; LatLng(item[0], item[1])).toList()

huangapple
  • 本文由 发表于 2023年6月16日 15:18:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76487809.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定