I'm using google maps in my flutter app, I want to put a pointer in the middle of the screen so it will be easy for the users to choose the location the look for by just moving the map and making the pointer in the middle of the screen points to the location they look for
so how then can I get the coordinates that sits in the middle of the screen ,exactly the same where the pointer is
You put pointer just by using Stack . If you are using google_maps_flutter then it has onCameraMove argument which returns CameraPosition of center whenever the map moves.
You can use the GlobalKey to obtain the RenderBox from which you can get the size and offset position information. First, you need to assign a GlobalKey to the widget. If you've assigned the GlobalKey to the widget, you can get the currentContext property of the key and call the findRenderObject() method.
Get user's latitude and longitude To query the current location of the device, simply make a call to the getCurrentPosition() method. For example: import 'package:geolocator/geolocator. dart';Position position = await Geolocator.
You can do it by using ScreenCoordinate
:
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
double screenWidth = MediaQuery.of(context).size.width *
MediaQuery.of(context).devicePixelRatio;
double screenHeight = MediaQuery.of(context).size.height *
MediaQuery.of(context).devicePixelRatio;
double middleX = screenWidth / 2;
double middleY = screenHeight / 2;
ScreenCoordinate screenCoordinate = ScreenCoordinate(x: middleX.round(), y: middleY.round());
LatLng middlePoint = await googleMapController.getLatLng(screenCoordinate);
You put pointer just by using Stack
. If you are using google_maps_flutter
then it has onCameraMove
argument which returns CameraPosition
of center whenever the map moves.
onCameraMove: (CameraPosition cp) {
LatLng center = cp.target;
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With