I am trying to display updated address whenever the google map camera is moved on my Flutter app.
Unfortunately, only the current location address is displayed when the map loads, and when the camera is moved, the address displayed in the TextEditingController _addressController.text
is not getting updated.
What is wrong in my code that prevents the address from being updated and how to solve it?
import 'package:flutter/material.dart';
import 'package:geocoding/geocoding.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class GMaps2 extends StatefulWidget {
const GMaps2({Key? key}) : super(key: key);
@override
State<GMaps2> createState() => _GMaps2State();
}
class _GMaps2State extends State<GMaps2> {
TextEditingController _addressController = TextEditingController();
GoogleMapController? _googleMapController;
CameraPosition _cameraPosition = CameraPosition(target: LatLng(0, 0));
LatLng _latlong = LatLng(0, 0);
Future<Position> getCurrentLocation() async {
bool serviceEnabled;
LocationPermission permission;
serviceEnabled =
await GeolocatorPlatform.instance.isLocationServiceEnabled();
if (!serviceEnabled) {
await GeolocatorPlatform.instance.openLocationSettings();
return Future.error('Location services are disabled.');
}
permission = await GeolocatorPlatform.instance.checkPermission();
if (permission == LocationPermission.denied) {
permission = await GeolocatorPlatform.instance.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.');
}
position = await GeolocatorPlatform.instance.getCurrentPosition(
locationSettings:
const LocationSettings(accuracy: LocationAccuracy.high));
setState(() {
_latlong = LatLng(position.latitude, position.longitude);
_googleMapController!.animateCamera(CameraUpdate.newCameraPosition(
CameraPosition(target: _latlong, zoom: 15)));
getAddress();
});
return position;
}
Position position = Position(
longitude: 0,
latitude: 0,
timestamp: DateTime.now(),
accuracy: 0,
altitude: 0,
heading: 0,
speed: 0,
speedAccuracy: 0,
);
List<Placemark>? placeMarks;
getAddress() async {
placeMarks =
await placemarkFromCoordinates(_latlong.latitude, _latlong.longitude);
Placemark placemark = placeMarks![0];
_addressController.text =
"${placemark.street}, ${placemark.subLocality}, ${placemark.locality}, ${placemark.subAdministrativeArea}, ${placemark.postalCode}, ${placemark.country}";
}
@override
void initState() {
super.initState();
getCurrentLocation();
}
//LatLng newLatlong = LatLng(0, 0);
@override
Widget build(BuildContext context) {
double screenWidth = MediaQuery.of(context).size.width;
double screenHeight = MediaQuery.of(context).size.height;
return SafeArea(
child: Scaffold(
body: Column(
children: [
SizedBox(
width: screenWidth,
height: screenHeight * 0.4,
child: GoogleMap(
initialCameraPosition: _cameraPosition,
onMapCreated: (controller) {
setState(() {
_googleMapController = controller;
getAddress();
});
},
onCameraMove: (CameraPosition cameraposition) {
position = Position(
longitude: cameraposition.target.latitude,
latitude: cameraposition.target.longitude,
timestamp: DateTime.now(),
accuracy: 0,
altitude: 0,
heading: 0,
speed: 0,
speedAccuracy: 0);
setState(() {
_latlong = LatLng(position.latitude, position.longitude);
print("${_latlong.latitude}, ${_latlong.longitude}");
print(_addressController.text);
getAddress();
//getMovedAddress(cameraposition);
});
},
),
),
const SizedBox(
height: 10,
),
Container(
child: TextField(
controller: _addressController,
style: const TextStyle(
fontSize: 12,
color: Colors.black,
),
),
),
],
),
),
);
}
}
Hi Ayan,
You can reach out to the Flutter community for Google Maps related queries using the following links.
Regards,
Sriram Kiran