当我在谷歌地图上标记时如何获取地址的所有详细信息。然后它将在颤动中填充文本框
Posted
技术标签:
【中文标题】当我在谷歌地图上标记时如何获取地址的所有详细信息。然后它将在颤动中填充文本框【英文标题】:How to get all detail of address when i am put mark on google map. and then it will fill up in text box in flutter 【发布时间】:2020-01-15 06:32:33 【问题描述】:我想在地图上放置一个标记,当我放置标记时,它会获取该地址并填充到文本框中。
就像我在这张图片中提到的那样,当我放置一个标记然后单击显示地址时,它将采用该地址并填充到文本框中。希望你明白这个问题。如果有任何疑问,请告诉我。我需要帮助请帮助我。因为我非常陷入这个问题。
这是谷歌地图的一些代码。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:tudo/src/styles/colors.dart';
import 'package:tudo/src/utils/navigation_helper.dart';
import 'package:tudo/src/widgets/google_map.dart';
const kGoogleApiKey = "API_KEY";
class BspAddressmapscreen extends StatefulWidget
BspAddressmapscreen(Key key) : super(key: key);
@override
_BspAddressmapscreenState createState() => _BspAddressmapscreenState();
class _BspAddressmapscreenState extends State<BspAddressmapscreen>
final homeScaffoldKey = GlobalKey<ScaffoldState>();
Completer<GoogleMapController> _controller = Completer();
@override
void initState()
super.initState();
double zoomVal = 5.0;
@override
Widget build(BuildContext context)
return new Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: ()
NavigationHelper.navigatetoBack(context);
),
centerTitle: true,
title: Text("Business Address Detail"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () ,
),
],
),
bottomNavigationBar: Container(
color: Colors.transparent,
height: 56,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new FlatButton.icon(
icon: Icon(Icons.arrow_back_ios),
label: Text('Show Address'),
textColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7),
),
onPressed: () async
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BspAddressmapscreen()));
,
),
],
),
),
body: Container(
height: double.infinity,
width: double.infinity,
child: Stack(
children: <Widget>[
_searchbar(),
_buildGoogleMap(context),
_zoomminusfunction(),
_zoomplusfunction(),
],
),
),
);
Widget _zoomminusfunction()
return Align(
alignment: Alignment.topLeft,
child: IconButton(
icon: Icon(FontAwesomeIcons.searchMinus, color: Color(0xff008080)),
onPressed: ()
zoomVal--;
_minus(zoomVal);
),
);
Widget _zoomplusfunction()
return Align(
alignment: Alignment.topRight,
child: IconButton(
icon: Icon(FontAwesomeIcons.searchPlus, color: Color(0xff008080)),
onPressed: ()
zoomVal++;
_plus(zoomVal);
),
);
Future<void> _minus(double zoomVal) async
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(target: LatLng(40.712776, -74.005974), zoom: zoomVal)));
Future<void> _plus(double zoomVal) async
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(target: LatLng(40.712776, -74.005974), zoom: zoomVal)));
Widget _buildGoogleMap(BuildContext context)
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: GoogleMap(
mapType: MapType.normal,
initialCameraPosition:
CameraPosition(target: LatLng(40.712776, -74.005974), zoom: 12),
onMapCreated: (GoogleMapController controller)
_controller.complete(controller);
,
),
);
Widget _searchbar()
return Positioned(
top: 50.0,
right: 15.0,
left: 15.0,
child: Container(
height: 50.0,
width: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0), color: Colors.white),
child: TextField(
decoration: InputDecoration(
hintText: 'Enter Address',
border: InputBorder.none,
contentPadding: EdgeInsets.only(left: 15.0, top: 15.0),
suffixIcon: IconButton(
icon: Icon(Icons.search),
//onPressed: searchandNavigate,
onPressed: () ,
iconSize: 30.0)),
onChanged: (val)
setState(()
// searchAddr = val;
);
,
),
),
);
【问题讨论】:
【参考方案1】:使用下面的代码绘制标记并在按钮单击时获取地址,也可以使用geocoder 从 latlng 获取地址
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:geocoder/geocoder.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
const kGoogleApiKey = "API_KEY";
class BspAddressmapscreen extends StatefulWidget
BspAddressmapscreen(Key key) : super(key: key);
@override
_BspAddressmapscreenState createState() => _BspAddressmapscreenState();
class _BspAddressmapscreenState extends State<BspAddressmapscreen>
final homeScaffoldKey = GlobalKey<ScaffoldState>();
Completer<GoogleMapController> _controller = Completer();
@override
void initState()
super.initState();
double zoomVal = 5.0;
@override
Widget build(BuildContext context)
return new Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(Icons.arrow_back_ios),
onPressed: ()
/*NavigationHelper.navigatetoBack(context);*/
),
centerTitle: true,
title: Text("Business Address Detail"),
actions: <Widget>[
IconButton(
icon: Icon(Icons.search),
onPressed: () ,
),
],
),
bottomNavigationBar: Container(
color: Colors.transparent,
height: 56,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
new FlatButton.icon(
icon: Icon(Icons.arrow_back_ios),
label: Text('Show Address'),
textColor: Colors.blue,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(7),
),
onPressed: ()
getUserLocation();
,
),
],
),
),
body: Container(
height: double.infinity,
width: double.infinity,
child: Stack(
children: <Widget>[
_searchbar(),
_buildGoogleMap(context),
_zoomminusfunction(),
_zoomplusfunction(),
],
),
),
);
getUserLocation() async
markers.values.forEach((value) async
print(value.position);
// From coordinates
final coordinates =
new Coordinates(value.position.latitude, value.position.longitude);
var addresses = await Geocoder.google(kGoogleApiKey)
.findAddressesFromCoordinates(coordinates);
print("Address: $addresses.first.featureName");
);
Widget _zoomminusfunction()
return Align(
alignment: Alignment.topLeft,
child: IconButton(
icon: Icon(Icons.remove, color: Color(0xff008080)),
onPressed: ()
zoomVal--;
_minus(zoomVal);
),
);
Widget _zoomplusfunction()
return Align(
alignment: Alignment.topRight,
child: IconButton(
icon: Icon(Icons.add, color: Color(0xff008080)),
onPressed: ()
zoomVal++;
_plus(zoomVal);
),
);
Future<void> _minus(double zoomVal) async
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(target: LatLng(40.712776, -74.005974), zoom: zoomVal)));
Future<void> _plus(double zoomVal) async
final GoogleMapController controller = await _controller.future;
controller.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(target: LatLng(40.712776, -74.005974), zoom: zoomVal)));
Map<MarkerId, Marker> markers = <MarkerId, Marker>;
Widget _buildGoogleMap(BuildContext context)
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: GoogleMap(
mapType: MapType.normal,
initialCameraPosition:
CameraPosition(target: LatLng(40.712776, -74.005974), zoom: 12),
onMapCreated: (GoogleMapController controller)
_controller.complete(controller);
,
markers: Set<Marker>.of(markers.values),
onLongPress: (LatLng latLng)
// creating a new MARKER
final MarkerId markerId = MarkerId('4544');
final Marker marker = Marker(
markerId: markerId,
position: latLng,
);
setState(()
markers.clear();
// adding a new marker to map
markers[markerId] = marker;
);
,
),
);
Widget _searchbar()
return Positioned(
top: 50.0,
right: 15.0,
left: 15.0,
child: Container(
height: 50.0,
width: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10.0), color: Colors.white),
child: TextField(
decoration: InputDecoration(
hintText: 'Enter Address',
border: InputBorder.none,
contentPadding: EdgeInsets.only(left: 15.0, top: 15.0),
suffixIcon: IconButton(
icon: Icon(Icons.search),
//onPressed: searchandNavigate,
onPressed: () ,
iconSize: 30.0,
),
),
onChanged: (val)
setState(()
// searchAddr = val;
);
,
),
),
);
【讨论】:
感谢您的回答,但我收到有关依赖性的错误。它说手动解决您的依赖关系。以上是关于当我在谷歌地图上标记时如何获取地址的所有详细信息。然后它将在颤动中填充文本框的主要内容,如果未能解决你的问题,请参考以下文章