'how to pick an address from map in flutter
I want to create a widget that show google map with initial location, when user click on it it open google map with a marker and allow user to search or select a location from map, then show it in text under the map widget.
Column(
children: [
Container(
height: 200,
width: double.infinity,
child: Scaffold(
body: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: CameraPosition(target: _initialcameraposition),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
onTap: (LatLng location){ },
),
),
),
Text("$address")
],
)
Solution 1:[1]
The google maps plugin for flutter doesn't provide geocoding, with the long/lat you can then use a package like https://pub.dev/packages/geocoding which let you gen an address from lat and long coordinates.
Solution 2:[2]
A lot of time has passed but here is a way to do it with a complete StatefulWidget. It's up to you to draw the code that interests you. Also remember to correctly initialize the necessary plugins.
pubspec.yaml file :
geolocator: ^8.2.0 // for location
google_maps_flutter: ^2.1.3
google_maps_flutter_web: ^0.3.2+1 // for web plateform
dart/flutter code :
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
// import 'package:google_maps_flutter_web/google_maps_flutter_web.dart' as webGM;
class MapPicker extends StatefulWidget {
static const DEFAULT_ZOOM = 14.4746;
static const KINSHASA_LOCATION = LatLng(-4.325, 15.322222);
double initZoom;
LatLng initCoordinates;
LatLng? value;
MapPicker(
{Key? key,
this.initZoom = DEFAULT_ZOOM,
this.initCoordinates = KINSHASA_LOCATION})
: super(key: key);
@override
State<MapPicker> createState() => _MapPickerState();
}
class _MapPickerState extends State<MapPicker> {
final Completer<GoogleMapController> _controller = Completer();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: SizedBox(
// width: 400,
height: 300,
child: LayoutBuilder(
builder: (context, constraints) {
var maxWidth = constraints.biggest.width;
var maxHeight = constraints.biggest.height;
return Stack(
children: <Widget>[
SizedBox(
height: maxHeight,
width: maxWidth,
child: GoogleMap(
initialCameraPosition: CameraPosition(
target: widget.initCoordinates,
zoom: widget.initZoom,
),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
onCameraMove: (CameraPosition newPosition) {
// print(newPosition.target.toJson());
widget.value = newPosition.target;
},
mapType: MapType.normal,
myLocationButtonEnabled: true,
myLocationEnabled: false,
zoomGesturesEnabled: true,
padding: const EdgeInsets.all(0),
buildingsEnabled: true,
cameraTargetBounds: CameraTargetBounds.unbounded,
compassEnabled: true,
indoorViewEnabled: false,
mapToolbarEnabled: true,
minMaxZoomPreference: MinMaxZoomPreference.unbounded,
rotateGesturesEnabled: true,
scrollGesturesEnabled: true,
tiltGesturesEnabled: true,
trafficEnabled: false,
),
),
Positioned(
bottom: maxHeight / 2,
right: (maxWidth - 30) / 2,
child: const Icon(
Icons.person_pin_circle,
size: 30,
color: Colors.black,
),
),
Positioned(
bottom: 30,
left: 30,
child: Container(
color: Colors.white,
child: IconButton(
onPressed: () async {
var position = await _determinePosition();
final GoogleMapController controller =
await _controller.future;
controller.animateCamera(
CameraUpdate.newCameraPosition(CameraPosition(
target: LatLng(
position.latitude, position.longitude),
zoom: widget.initZoom)));
},
icon: const Icon(Icons.my_location),
),
),
),
],
);
},
),
),
),
);
}
Future<Position> _determinePosition() async {
bool serviceEnabled;
LocationPermission permission;
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return Future.error('Location services are disabled.');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
}
return await Geolocator.getCurrentPosition();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Abdelkrim Bournane |
Solution 2 | elgsylvain85 |