英文:
How to show a widget when the marker is clicked on in gmap flutter?
问题
以下是您要翻译的代码部分:
I want to show a widget with the latlong info and a button when the marker is placed on the map.or clicked on marker.
**this is my code to handle the markers**
Future<void> addMarker(
LatLng mLatLng, String mTitle, String mDescription) async {
final Uint8List markerIcon =
await getBytesFromAsset('assets/images/icons/pin.png', 100);
setState(() {
_markers.clear();
_markers.add(Marker(
markerId:
MarkerId((mTitle + "_" + _markers.length.toString()).toString()),
position: mLatLng,
infoWindow: InfoWindow(
title: mTitle,
snippet: mDescription,
),
icon: BitmapDescriptor.fromBytes(
markerIcon), //BitmapDescriptor.defaultMarker,
));
});
}
Future<void> _handleTap(LatLng tappedPoint) async {
print(tappedPoint);
final words = await api
.convertTo3wa(Coordinates(tappedPoint.latitude, tappedPoint.longitude))
.language('en')
.execute();
print(words.data()!.words);
LatLng mLatLng = tappedPoint;
String mTitle = '///${words.data()!.words}'';
String mDescription =
'Coordinates: ${words.data()!.coordinates.lat}, ${words.data()!.coordinates.lng},\nNearest Place: ${words.data()!.nearestPlace}'
setState(() {
_markers.clear();
if (words.isSuccessful()) {
addMarker(mLatLng, mTitle, mDescription);
getPillInfowindow(mLatLng, mTitle, mDescription);
//_customInfoWindowController.hideInfoWindow!();
} else {
print(words.error());
}
});
}
**and the widget i want shown when marker is clicked on is this**
import 'package:flutter/material.dart';
import 'package:mymapp/screens/home_page.dart';
Widget getPillInfowindow(mLatLng, mTitle, mDescription) {
return Positioned(
bottom: 100, right: 0, left: 0,
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
margin: EdgeInsets.all(20),
height: 70,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(50)),
boxShadow: <BoxShadow>[
BoxShadow(
blurRadius: 20,
offset: Offset.zero,
color: Colors.grey.withOpacity(0.5))
]),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.only(left: 10),
width: 50,
height: 50,
child: ClipOval(
child: Image.asset('assets/images/icons/pin2.png',
fit: BoxFit.cover))),
// first widget
Expanded(
child: Container(
margin: EdgeInsets.only left: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('title:${mTitle}', //${mTitle}',
style: TextStyle(
color: Colors
.green)), //currentlySelectedPin.labelColor)),
Text(
'Latitude, Longitude: ${mLatLng}',
style: TextStyle(fontSize: 12, color: Colors.grey)),
Text(
'Desc: ${mDescription}', //
style: TextStyle(fontSize: 12, color: Colors.grey))
], // end of Column Widgets
), // end of Column
),
), // second widget
],
),
),
),
); // end of Container
}
but somehow this doesn't work the way I want it to. What's wrong here?
Sorry if there's mistakes here, I'm a beginner in flutter
希望这有所帮助。如果您需要更多的信息或有其他问题,请随时提问。
英文:
I want to show a widget with the latlong info and a button when the marker is placed on the map.or clicked on marker.
this is my code to handle the markers
Future<void> addMarker(
LatLng mLatLng, String mTitle, String mDescription) async {
final Uint8List markerIcon =
await getBytesFromAsset('assets/images/icons/pin.png', 100);
setState(() {
_markers.clear();
_markers.add(Marker(
markerId:
MarkerId((mTitle + "_" + _markers.length.toString()).toString()),
position: mLatLng,
infoWindow: InfoWindow(
title: mTitle,
snippet: mDescription,
),
icon: BitmapDescriptor.fromBytes(
markerIcon), //BitmapDescriptor.defaultMarker,
));
});
}
Future<void> _handleTap(LatLng tappedPoint) async {
print(tappedPoint);
final words = await api
.convertTo3wa(Coordinates(tappedPoint.latitude, tappedPoint.longitude))
.language('en')
.execute();
print(words.data()!.words);
LatLng mLatLng = tappedPoint;
String mTitle = '///${words.data()!.words}';
String mDescription =
'Coordinates: ${words.data()!.coordinates.lat}, ${words.data()!.coordinates.lng},\nNearest Place: ${words.data()!.nearestPlace}';
setState(() {
_markers.clear();
if (words.isSuccessful()) {
addMarker(mLatLng, mTitle, mDescription);
getPillInfowindow(mLatLng, mTitle, mDescription);
//_customInfoWindowController.hideInfoWindow!();
} else {
print(words.error());
}
});
}
and the widget i want shown when marker is clicked on is this
import 'package:flutter/material.dart';
import 'package:mymapp/screens/home_page.dart';
Widget getPillInfowindow(mLatLng, mTitle, mDescription) {
return Positioned(
bottom: 100, right: 0, left: 0,
child: Align(
alignment: Alignment.bottomCenter,
child: Container(
margin: EdgeInsets.all(20),
height: 70,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(50)),
boxShadow: <BoxShadow>[
BoxShadow(
blurRadius: 20,
offset: Offset.zero,
color: Colors.grey.withOpacity(0.5))
]),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
margin: EdgeInsets.only(left: 10),
width: 50,
height: 50,
child: ClipOval(
child: Image.asset('assets/images/icons/pin2.png',
fit: BoxFit.cover))),
// first widget
Expanded(
child: Container(
margin: EdgeInsets.only(left: 20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('title:${mTitle}', //${mTitle}',
style: TextStyle(
color: Colors
.green)), //currentlySelectedPin.labelColor)),
Text(
'Latitude, Longitude: ${mLatLng}',
style: TextStyle(fontSize: 12, color: Colors.grey)),
Text(
'Desc: ${mDescription}', //
style: TextStyle(fontSize: 12, color: Colors.grey))
], // end of Column Widgets
), // end of Column
),
), // second widget
],
),
),
),
); // end of Container
}
but somehow this doesn't work the way I want it to. What's wrong here?
Sorry if there's mistakes here, I'm a beginner in flutter
答案1
得分: 0
未来<void> _handleTap(LatLng tappedPoint) async {
// 获取标记信息的现有代码
// ...
setState(() {
_markers.clear();
if (words.isSuccessful()) {
addMarker(mLatLng, mTitle, mDescription);
_pillInfoWindow = getPillInfowindow(mLatLng, mTitle, mDescription); // 使用新的标记信息更新 _pillInfoWindow 变量
} else {
print(words.error());
}
});
}
然后
Stack(
children: [
GoogleMap(
// ... 你现有的 GoogleMap widget 代码 ...
onMapCreated: (GoogleMapController controller) {
_mapController = controller;
},
onTap: _handleTap,
markers: _markers,
),
if (_pillInfoWindow != null) _pillInfoWindow!, // 将 _pillInfoWindow widget 添加到 Stack 中
],
)
英文:
Future<void> _handleTap(LatLng tappedPoint) async {
// Your existing code to get the marker information
// ...
setState(() {
_markers.clear();
if (words.isSuccessful()) {
addMarker(mLatLng, mTitle, mDescription);
_pillInfoWindow = getPillInfowindow(mLatLng, mTitle, mDescription); // Update the _pillInfoWindow variable with the new marker information
} else {
print(words.error());
}
});
}
then
Stack(
children: [
GoogleMap(
// ... your existing GoogleMap widget code ...
onMapCreated: (GoogleMapController controller) {
_mapController = controller;
},
onTap: _handleTap,
markers: _markers,
),
if (_pillInfoWindow != null) _pillInfoWindow!, // Add the _pillInfoWindow widget to the Stack
],
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论