mapbox-maps-flutter icon indicating copy to clipboard operation
mapbox-maps-flutter copied to clipboard

How to get the coordinates of center of Map screen?

Open arslankaleem7229 opened this issue 1 year ago • 3 comments

I want to get the location of the center of Map screen which I am unable to see an option to get. Is there any option by which we can track the coordinates after scroll ??

arslankaleem7229 avatar Feb 06 '24 13:02 arslankaleem7229

@arslankaleem7229 You can use MapboxMap.coordinateForPixel() to get the coordinates of a location in the center of the map.

evil159 avatar Feb 07 '24 07:02 evil159

Thanks @evil159 . Its working fine. But there as issue with this I am using screen center using MediaQuery for giving x and y but the coordinates I am getting in return are not accurate


double screenWidth = MediaQuery.of(context).size.width;
double screenHeight = MediaQuery.of(context).size.height;

double middleX = screenWidth / 2;
double middleY = screenHeight / 2;

mapboxMap.coordinateForPixel(mapbox.ScreenCoordinate(
                          x: middleX.roundToDouble(),
                          y: middleY.roundToDouble())
                          ).then((value) async {
                      print(value)
                  });

arslankaleem7229 avatar Feb 07 '24 13:02 arslankaleem7229

@arslankaleem7229 void _onCameraChanged() async { final center = await _controller!.getCameraState(); final centerLatLng = LatLng( (center.center['coordinates'] as List)[1], (center.center['coordinates'] as List)[0], ); }

GregoryPardini avatar Mar 11 '24 15:03 GregoryPardini