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

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

Flutter how to create a List LatLng from a String?

问题

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

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

List<LatLng> polygon1 = const [
  LatLng(48.885278, 2.349523),
  LatLng(48.885471, 2.349528),
  LatLng(48.888300, 2.349597)
];

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

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

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

List&lt;LatLng&gt; polygon1 = const [
LatLng(48.885278,2.349523),
LatLng(48.885471,2.349528), 
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' :

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格式。

它需要像这样:

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

你可以使用这个方法:

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

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

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

示例:

void main() async {
    var coord = "[[48.885278, 2.349523], [48.885471,2.349528], [48.888300,2.349597]]";
    List<LatLng> polygon = parseLatLng(coord);
}

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

或者

var coord = "[[48.885278, 2.349523], [48.885471,2.349528], [48.888300,2.349597]]";
var json = jsonDecode(jsonString);
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:

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

You can use this method:

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

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

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


}

Example:

void main() async {


var coord = &quot;[[48.885278, 2.349523], [48.885471,2.349528], [48.888300,2.349597]]&quot;;
List&lt;LatLng&gt; polygon=parseLatLng(coord);



}

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

or

var coord = &quot;[[48.885278, 2.349523], [48.885471,2.349528], [48.888300,2.349597]]&quot;;
var json = jsonDecode(jsonString);
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:

确定