Home > Enterprise >  Flutter google maps and import polylines
Flutter google maps and import polylines

Time:09-04

I just start to learn more about dart and flutter and followed some tutorials. Now I have a start with a Google maps app and what i want to do is to import my own polylines.

Normally the polylines are generated automatically between the start and destination point but i want to change that:

This is the automatic standard:

List<LatLng> polylineCoordinates = [];

This is how I can manually change that.

List<LatLng> polylineCoordinates = [
   LatLng (4.6546609, 52.2997741),
   LatLng (4.6539206, 52.2998594),
   LatLng (4.6522898, 52.3002268),
   LatLng (4.651689, 52.3002793),
  ];

Now I dont want to manually add it in the code but I want to fill it from a file(json, csv)

What is best to do and how can I start.. Hope someone can help me.

CodePudding user response:

Your question is more like "how to read data from json/csv into dart code".

Anyway, here's a standard way to do it:

Assuming that you have the values in a json file like the following:

{
[
{ 'lat':33.22222,
  'long':20.25451
},
{ 'lat':30.89222,
  'long':20.55551
}
]
}
  1. Declare the json file in the pubspec.yaml
flutter:
assets:
- assets/sample.json
  1. Write a function that parses the json
   readJson() async {
final String assetFile = await rootBundle.loadString('assets/sample.json');
final data = await json.decode(assetFile);
return data;
}
  1. Use that data for your desired logic
List<LatLng> polylineCoordinates = readJson().map((e)=>LatLng(e['lat'], e['long']).toList;

CodePudding user response:

I now have:

List<LatLng> polylineCoordinates = [];

@override
  void initState() {
    polylineCoordinates = readJson().map((e)=> LatLng(e['lat'], e['long'])).tolist();
    super.initState();
  }

But now the error is:

Class 'Future<dynamic>' has no instance method 'map'.
Receiver: Instance of 'Future<dynamic>'
Tried calling: map(Closure: (dynamic) => LatLng)

Where do I have to look for? I dont understand what it means and when google found a lot what lookt like the error but not exactly this.

  • Related