Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 759
Karrar Avatar asked Dec 20 '19 19:12

Karrar


People also ask

How do you find the center of the screen Flutter?

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.

How do you get the coordinates of a widget in Flutter?

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.

How do you get a place name from latitude and longitude in Flutter?

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.


2 Answers

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);
like image 198
Marek Loose Avatar answered Sep 25 '22 16:09

Marek Loose


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;
      },
like image 30
temirbek Avatar answered Sep 23 '22 16:09

temirbek