Flutter:如何获取屏幕中心点的位置坐标

Kar*_*rar 5 maps coordinates flutter

我在我的 flutter 应用程序中使用谷歌地图,我想在屏幕中间放置一个指针,这样用户只需移动地图并将指针放在中间就可以轻松选择要查找的位置屏幕指向他们寻找的位置

那么我怎样才能获得位于屏幕中间的坐标,与指针所在的位置完全相同

小智 5

您可以通过使用ScreenCoordinate

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
Run Code Online (Sandbox Code Playgroud)
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);
Run Code Online (Sandbox Code Playgroud)

  • “ScreenCoordinate”要求 (x,y) 参数是实际像素数,因此请记住考虑“devicePixelRatio”。即实际像素数为 width * devicePixelRatio (2认同)
  • screenWidth *= MediaQuery.of(context).devicePixelRatio; screenHeight *= MediaQuery.of(context).devicePixelRatio; (2认同)