我想使用PlacesAutocomplete获取地点并将其存储在我的TextFormField中。
我正在从displayPrediction()
致电TextFormFeild()
TextFormField(
decoration: textInputDecoration.copyWith(hintText: 'From'),
onTap: () async {
Prediction p = await PlacesAutocomplete.show(
context: context,
apiKey: kGoogleApiKey,
mode: Mode.overlay,
//language: "en",
//components: [Component (Component.country, "en")],
);
displayPrediction(p);
},
),
这里是displayPrediction()
:
Future<Null> displayPrediction(Prediction p) async {
if (p != null) {
PlacesDetailsResponse detail =
await _places.getDetailsByPlaceId(p.placeId);
var placeId = p.placeId;
double lat = detail.result.geometry.location.lat;
double lng = detail.result.geometry.location.lng;
var address = await Geocoder.local.findAddressesFromQuery(p.description);
print(lat);
print(lng);
print(address);
}
}
变量kGoogleApiKey和_places
const kGoogleApiKey = "My_API_Key";
GoogleMapsPlaces _places = GoogleMapsPlaces(apiKey: kGoogleApiKey);
我想在我的TextFormField上点击后获取位置,并在同一TextFormField中显示相同的位置
选择一个位置后,我得到了这个错误-_TypeError (type 'List<dynamic>' is not a subtype of type 'PlaceDetails')
答案 0 :(得分:1)
这是演示
final TextEditingController _controller = TextEditingController();
TextFormField( controller: _controller ,
decoration: textInputDecoration.copyWith(hintText: 'From'),
onTap: () async {
Prediction p = await PlacesAutocomplete.show(
context: context,
apiKey: kGoogleApiKey,
mode: Mode.overlay,
//language: "en",
//components: [Component (Component.country, "en")],
);
displayPrediction(p);
},
),
您似乎正在使用地理编码器库
Future<Null> displayPrediction(Prediction p) async {
if (p != null) {
PlacesDetailsResponse detail =
await _places.getDetailsByPlaceId(p.placeId);
var placeId = p.placeId;
double lat = detail.result.geometry.location.lat;
double lng = detail.result.geometry.location.lng;
var address = await Geocoder.local.findAddressesFromQuery(p.description);
setState(() {
_controller.text = address.first.featureName;
});
print(lat);
print(lng);
print(address);
}
}
希望有帮助