【问题标题】:I need to convert latitude and longitude to address from rest api in to a listview我需要将纬度和经度从rest api转换为listview
【发布时间】:2020-10-31 00:07:04
【问题描述】:

我正在从 rest api 获取纬度和经度,并且每次加载列表时都必须转换为地址,但我得到 'Future' 不是'String' Flutter 类型的子类型 .我也尝试添加 FutureBuilder。如何解决这个问题我是新手。

 body: Container(
        child: Center(
          child: FutureBuilder(
            builder: (context, snapshot) {
              return ListView.builder(
                  physics: const AlwaysScrollableScrollPhysics(),
                  itemCount:
                  _getAttendanceLog == null ? 0 : _getAttendanceLog.length,
                  itemBuilder: (BuildContext context, int index) {
                    final item = _getAttendanceLog[index];
                     
                   return card(
                   child: Column(
                   children: <Widget>[
                       new Container(
                              child: Column(
                              children: <Widget>[
                              if(item.startlattitude == 'null'|| item.startlattitude == null||item.startlongitude == 'null'|| item.startlongitude == null) ...[
                              new Text("In Location : -", style: TextStyle(fontSize: SizeConfig.safeBlockHorizontal*3),)
                               ]else ...[
                               Marquee(
                               child: Text("In Location : " + getUserLocation(item.startlattitude, item.startlongitude),style: TextStyle(fontSize: SizeConfig.safeBlockHorizontal*3),),
                               directionMarguee: DirectionMarguee.oneDirection,
                               ),SizeConfig.safeBlockHorizontal*3),),
                               ]
                              ],
                            ),
                         ),
                    ])
                   )

我用过 Geocoder 也见下文 ======> getUserLocation

getUserLocation(startlat, startlong) async {

    final coordinates = new Coordinates(
        startlat, startlong);
    var addresses = await Geocoder.local.findAddressesFromCoordinates(
        coordinates);
    finalInLocation = addresses.first;
    print(' ${finalInLocation.locality}, ${finalInLocation.adminArea},${finalInLocation.subLocality}, ${finalInLocation.subAdminArea},${finalInLocation.addressLine}, ${finalInLocation.featureName},${finalInLocation.thoroughfare}, ${finalInLocation.subThoroughfare}');
    return finalInLocation;
  }

我需要这样:

【问题讨论】:

    标签: flutter


    【解决方案1】:

    您可以使用Geocoder包将latlng转换为地址列表

    final coordinates = new Coordinates(1.10, 45.50);
    addresses = await Geocoder.local.findAddressesFromCoordinates(coordinates);
    first = addresses.first;
    print("${first.featureName} : ${first.addressLine}");
    

    【讨论】:

    • 我已经使用过了。我需要在列表视图中并将转换后的函数(纬度,经度)分配给文本。
    • 这类数据通常来自API端。但是,如果您真的想在 Android 中执行此操作,则必须遍历每个列表项,并使用地理编码器将坐标转换为地址。这是一个非常耗时且资源繁重的过程。我不鼓励你这样做。
    • 问题已解决。顺便说一句,我每个列表只显示 8 个项目。感谢您提供信息。
    【解决方案2】:

    您收到错误是因为您将 Future 传递给需要 String 的参数。

    因为您需要等待api将lat-long转换为地址文本,所以您需要在小部件树中使用FutureBuilderitemBuilder中需要显示地址文本的位置像这样..

    FutureBuilder(
                future: getUserLocation(lat, lon),
                builder: (context, snapshot) {
                  if (snapshot.hasData == false) {
                    return Text('loading...');
                  } else {
                    return Text(snapshot.data);
                  }
                })
    

    你需要让你的getUserLocation 方法返回一个Future&lt;String&gt; 像这样..

    Future<String> getUserLocation(startlat, startlong) async {
        //other code
        await //more code;
        return yourAddressString;
      }
    

    【讨论】:

    猜你喜欢
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 2013-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多