Home > Net >  How to get LatLng from a specific postion on screen in google map flutter
How to get LatLng from a specific postion on screen in google map flutter

Time:12-28

I need to get the coordinates under my custom marker here is an image of my custom marker

custom marker image I am using stack to place my custom maker on specfic postion from top and left now I need the coordinates under my custom marker to get the location.

final GoogleMapController gmController =
  await controller.future;
LatLngBounds visibleRegion =
  await gmController.getVisibleRegion();
LatLng centerLatLng = LatLng(
(visibleRegion.northeast.latitude  
        visibleRegion.southwest.latitude) /
    2,
(visibleRegion.northeast.longitude  
        visibleRegion.southwest.longitude) /
    2,
);

print(centerLatLng);

this above code is used to get center coordinates of map any help would be appriciated.

CodePudding user response:

To get the latitude and longitude (LatLng) of a specific position on the screen, you can use the GoogleMapController.getLatLng method.

 LatLng _getLatLng(Offset offset) async {
    return await _mapController.getLatLng(offset);
 }

CodePudding user response:

        GoogleMap(
          initialCameraPosition: CameraPosition(target: _initialCameraPosition),
          mapType: MapType.normal,
          onMapCreated: (GoogleMapController _cntlr) {
            _onMapCreated(_cntlr);
          },
          myLocationEnabled: true,
          onTap: (LatLng latLng) {
            if (_controller != null) {
              _controller!.animateCamera(
                CameraUpdate.newCameraPosition(CameraPosition(target: LatLng(latLng.latitude, latLng.longitude), zoom: 15)),
              );
            }
            _handleTap(latLng);
          },
          markers: _markers,
          gestureRecognizers: Set()
            ..add(
              Factory<PanGestureRecognizer>(
                () => PanGestureRecognizer(),
              ),
            )
            ..add(Factory<ScaleGestureRecognizer>(
                () => ScaleGestureRecognizer()))
            ..add(
              Factory<TapGestureRecognizer>(
                () => TapGestureRecognizer(),
              ),
            )
            ..add(
              Factory<VerticalDragGestureRecognizer>(
                () => VerticalDragGestureRecognizer(),
              ),
            ),
        ),


   HandleTap method will give you lat long.

_handleTap(LatLng point) {
   latLng = "${num.parse(point.latitude.toStringAsFixed(6))},${num.parse(point.longitude.toStringAsFixed(6))}";
   print("LAT ::: LNG ${latLng}");
 } 
  • Related