Issue
I tried to determine my address’s camera position by using ‘geolocator’ library but I don’t know how to set it into the google map’s initial camera position
searchAndNavigate() {
searchAddress = widget.author.address;
Geolocator().placemarkFromAddress(searchAddress).then((result) {
mapController.animateCamera(CameraUpdate.newCameraPosition(CameraPosition(
target:
LatLng(result[0].position.latitude, result[0].position.longitude),
zoom: 10.0)));
});
}
void onMapCreated(controller) {
setState(() {
mapController = controller;
});
}
Widget mapSection = Container(
height: 400,
width: 400,
child: Card(
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12.0)),
child: GoogleMap(
onMapCreated: onMapCreated,
initialCameraPosition: CameraPosition(
target: LatLng(40.7128, -74.0060), zoom: 10.0)),
),
);
@override
void initState() {
super.initState();
searchAndNavigate();
}
Solution
This is how I set initial camera position
//initialize _center
Position _center;
final Set<Marker> _markers = {};
@override
void initState() {
super.initState();
//Then define _center in initState
_center = Position(locationData.latitude, locationData.longitude);
_markers.add(
Marker(
// This marker id can be anything that uniquely identifies each marker.
markerId: MarkerId(_center.toString()),
position: _center,
infoWindow: InfoWindow(
title: widget.hearingItem.location.locationName,
snippet: widget.hearingItem.location.address,
),
icon: BitmapDescriptor.defaultMarker,
),
);
}
GoogleMap(
myLocationEnabled: true,
onMapCreated: _onMapCreated,
markers: _markers,
gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>[
Factory<OneSequenceGestureRecognizer>(
() => EagerGestureRecognizer(),
),
].toSet(),
//Finally assign _center to target in CameraPosition
initialCameraPosition: CameraPosition(
target: _center,
zoom: 11.0,
),
);
Answered By – wcyankees424
Answer Checked By – Cary Denson (FlutterFixes Admin)