Flutter: how to get location coordinates of point in the center of screen
Asked Answered
L

2

9

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

Lesbianism answered 20/12, 2019 at 19:50 Comment(1)
check GoogleMapController official documentationMetagnathous
S
9

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);
Selhorst answered 17/1, 2020 at 12:59 Comment(2)
ScreenCoordinate requires the (x,y) parameters to be the number of actual pixels, so remember to take the devicePixelRatio into account. i.e.actual number of pixels is width * devicePixelRatioPrevocalic
screenWidth *= MediaQuery.of(context).devicePixelRatio; screenHeight *= MediaQuery.of(context).devicePixelRatio;Prototrophic
B
4

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;
      },
Burette answered 24/9, 2021 at 11:12 Comment(1)
This solution is more accurate, because calculating manually the position varies from device to another according to my test attempts.Fairtrade

© 2022 - 2024 — McMap. All rights reserved.