【发布时间】:2021-01-27 13:24:21
【问题描述】:
我有数据,其中我有起点和终点的 lat 和 long 值。
我正在尝试显示这样的标记
class _TripRouteScreenState extends State<TripRouteScreen> {
BitmapDescriptor pinLocationIcon;
var start_currentPostion;
var end_currentPostion;
Map<MarkerId, Marker> markers =
<MarkerId, Marker>{}; // CLASS MEMBER, MAP OF MARKS
void setCustomMapPin() async {
pinLocationIcon = await BitmapDescriptor.fromAssetImage(
ImageConfiguration(devicePixelRatio: 2.5), 'images/pin.png');
}
@override
void initState() {
super.initState();
setCustomMapPin();
working();
}
working(){
print(widget.data);
double start_latitude = widget.data['start']['lat'].toDouble();
double start_longitude = widget.data['start']['lon'].toDouble();
double end_latitude = widget.data['end']['lat'].toDouble();
double end_longitude = widget.data['end']['lon'].toDouble();
start_currentPostion = LatLng(start_latitude, start_longitude); //this is the marker 1 location which is already set and showing in map
end_currentPostion = LatLng(end_latitude, end_longitude); // this is the marker 2 location which needs to be set and show marker on this value
print(start_currentPostion);
print(end_currentPostion);
var snip = widget.data["start"]["address"];
final Map<String, Marker> _markers = {};
setState(() {
final MarkerId markerId = MarkerId('1');
// creating a new MARKER
final Marker marker = Marker(
markerId: markerId,
icon: pinLocationIcon,
position: start_currentPostion,
infoWindow: InfoWindow(title: 'Address',snippet: snip),
);
setState(() {
// adding a new marker to map
markers[markerId] = marker;
});
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
leading: GestureDetector(
onTap: () {
Navigator.pop(context);
},
child: Icon(Icons.arrow_back)),
centerTitle: true,
flexibleSpace: Container(
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('images/nav.jpg'),
fit: BoxFit.cover,
),
),
),
backgroundColor: Colors.transparent,
title: Text(
'Route Location',
style: TextStyle(fontFamily: 'UbuntuBold'),
),
actions: [
Padding(
padding: const EdgeInsets.only(right: 15),
child: Icon(
Icons.notifications_none,
size: 33,
),
)
]),
body: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: CameraPosition(
target: start_currentPostion,
zoom: 15,
),
markers: Set<Marker>.of(markers.values), // YOUR MARKS IN MAP
),
);
}
}
问题是我可以在地图上添加一个标记,但只能在起点上:D 意思是 start_currentPostion 我需要在终点上添加一个标记,也就是 end_currentPostion 但我不知道该怎么做。最后,从教程和其他答案中,我可以添加 1 个标记,但现在不知道如何添加第二个标记。
【问题讨论】:
-
标记将由您或用户设置?意味着它将动态设置?
-
@ShubhamNarkhede 由我你可以在代码中看到我自己设置 Lan 和 long 值我也在代码中评论所以它会清除
-
请查看我的回答
-
你只需要在我的代码中动态传递你的数据我希望这对你有用