【发布时间】:2020-04-13 06:51:07
【问题描述】:
我在我的颤振应用程序中使用谷歌地图,我想在屏幕中间放置一个指针,这样用户只需移动地图并制作指针就可以轻松选择要查找的位置屏幕中间的 指向他们寻找的位置
那么我怎样才能获得位于屏幕中间的坐标,与指针所在的位置完全相同
【问题讨论】:
-
查看
GoogleMapController官方文档
标签: flutter maps coordinates
我在我的颤振应用程序中使用谷歌地图,我想在屏幕中间放置一个指针,这样用户只需移动地图并制作指针就可以轻松选择要查找的位置屏幕中间的 指向他们寻找的位置
那么我怎样才能获得位于屏幕中间的坐标,与指针所在的位置完全相同
【问题讨论】:
GoogleMapController官方文档
标签: flutter maps coordinates
您可以使用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);
【讨论】:
ScreenCoordinate 要求 (x,y) 参数为实际像素数,因此请记住将devicePixelRatio 考虑在内。即实际像素数为 width * devicePixelRatio
您只需使用Stack 即可放置指针。如果您使用的是google_maps_flutter,那么它有onCameraMove 参数,只要地图移动,它就会返回中心的CameraPosition。
onCameraMove: (CameraPosition cp) {
LatLng center = cp.target;
},
【讨论】: