gpt4 book ai didi

loops - 无法在 Flutter 中过滤位置

转载 作者:IT王子 更新时间:2023-10-29 07:15:58 26 4
gpt4 key购买 nike

我正在尝试过滤掉我的 flutter 应用程序中的位置。我可以过滤我的标记,但我不确定按名称详细信息提取位置的最佳方法是什么。以下是我创建标记的方式:

  Map<MarkerId, Marker> markers = <MarkerId, Marker>{};


Future filterMarker(_userlat, _userlong) async {
int filterCounter = 0;

for (int i = 0; i < venues.length; ++i) {
Geolocator()
.distanceBetween(
_userLat, _userlong, venues[i].latitude, venues[i].longitude)
.then((calcDist) {
if (calcDist / 1000 < 1) {
filterCounter++;
placeFilteredMarker(filterCounter, calcDist / 1000, venues[i].name,
venues[i].latitude, venues[i].longitude);

filtervenues(filterCounter, venues[i].name, venues[i].address,
venues[i].rating, calcDist / 1000);
}
});
}
}


placeFilteredMarker(counter, distance, name, lat, lng) {
final int markerCount = counter;

for (int i = 0; i < markerCount; i++) {
final String markerIdVal = 'marker_id_$markerCount';

final MarkerId markerId = MarkerId(markerIdVal);

// creating a new MARKER
final Marker marker = Marker(
markerId: markerId,
position: LatLng(lat, lng),
infoWindow: InfoWindow(title: name, snippet: '$distance'),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueBlue),
);

setState(() {
markers[markerId] = marker;
// adding a new marker to map
});

return;
}
}

我正在尝试使用详细信息列表进行类似操作。按位置过滤并显示详细信息,但我不明白我做错了什么。这是我正在尝试的

问题是:我不确定我应该怎么做才能提取姓名、地址和评级。

List<Venue>venue;  

Future filterMarker(_userlat, _userlong) async {
int filterCounter = 0;

for (int i = 0; i < venues.length; ++i) {
Geolocator()
.distanceBetween(
_userLat, _userlong, venues[i].latitude, venues[i].longitude)
.then((calcDist) {
if (calcDist / 1000 < 1) {
filterCounter++;
placeFilteredMarker(filterCounter, calcDist / 1000, venues[i].name,
venues[i].latitude, venues[i].longitude);

filtervenues(filterCounter, venues[i].name, venues[i].address,
venues[i].rating, calcDist / 1000);
}
});
}
}


filtervenues(counter, name, addres, rating, distance) {
final int venueCounter = counter;

for (int i = 0; i < venueCounter; i++) {
setState(() {


});
return;
}
}

那应该迭代然后添加到下面的容器中:

  Widget _buildList(BuildContext context, _name, _address, _rating) {

return Container(
height: 425,
child: Column(
children: <Widget>[
SizedBox(
height: 2,
),
SizedBox(
height: 2,
),
_locations(venues[index].name, venues[index].address,
venues[index].rating),
Divider(
height: 2,
)
],
);
),
);
}

照片更新。我正在尝试将标记的数据放入此底页。

enter image description here

最佳答案

首先:这整个逻辑似乎表现不佳。您在 for 循环的每次迭代中设置 State,这会导致 MapsWidget 重建每次迭代。你为什么不创建一个标记项列表,然后最后像下面的代码那样做呢?另外我会做类似于 field 的事情:

class Location {
final Venue venue;
final double dist;
final int index;
Location({this.venue, this.dist, this.index});
}

  Set<Marker> markers = Set(); //Google Maps Markers are normally a Set
List<Location> location = List();


Future filterMarker(_userlat, _userlong) async {
Set<Marker> _tmpMarkers = Set();
int filterCounter = 0;
List<Location> _tmpLocation = List();
for (int i = 0; i < venues.length; ++i) {
Geolocator()
.distanceBetween(
_userLat, _userlong, venues[i].latitude, venues[i].longitude)
.then((calcDist) {
if (calcDist / 1000 < 1) {
filterCounter++;
final String markerIdVal = 'marker_id_$filterCounter';

final MarkerId markerId = MarkerId(markerIdVal);

// creating a new MARKER
tmpMarkers.add(Marker(
markerId: markerId,
position: LatLng(venues[i].latitude, venues[i].longitude),
infoWindow: InfoWindow(title: venues[i].name, snippet: '$calcDist / 1000'),
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueBlue),
));
_tmpLocation.add(Location(index: i, Venue: venue[i], dist: calcDist / 1000));
}
});
}
setState((){
markers = _tmpMarkers;
location = _tmpLocation;
});
}

然后您在底部工作表中使用您的位置对象。此外,我无法测试此代码,因为我没有您的完整代码,可能缺少一些括号。

然后您可以在 bottomSheet 的 ListView.builder 中使用位置列表。在那里,您可以自己设置每个列表磁贴的样式。

// As you didn't provide any code of your bottom sheet i just call it DraggableBottomSheet
bottomSheet: DraggableBottomSheet(
child: Column(
children : [
Row(
children: [
Text('bla'),
Text(location.length.toString(),
]
),
ListView.builder(
itemCount: location.length,
itemBuilder: (context, index) {
return Container(
child Column(
children : [
Text(location[index].venue.name),
Text(location[index].dist.toString()),
]
)
);
}
)
]
)
)

关于loops - 无法在 Flutter 中过滤位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57381700/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com