我们可以在Google Map中初始摄像头位置 - Flutter

问题描述 投票:0回答:1

我们如何在谷歌地图中设置初始相机位置,而不是硬编码拉/拉(代码下面)

GoogleMap(
          initialCameraPosition: CameraPosition(
            target: LatLng(30.666, 76.8127),
            zoom: 15
          ),
          onMapCreated: _onMapCreated,
          myLocationEnabled: true,
          mapType: MapType.hybrid, 
          compassEnabled: true,
          trackCameraPosition: true,
      )

我只想要当前位置lat / lang而不是硬编码值

google-maps dart flutter
1个回答
0
投票

使用location包。

这是一个例子。

var currentLocation = LocationData;

var location = new Location();

// Platform messages may fail, so we use a try/catch PlatformException.
try {
  currentLocation = await location.getLocation();
} on PlatformException catch (e) {
if (e.code == 'PERMISSION_DENIED') {
error = 'Permission denied';
}
currentLocation = null;
}

然后

var location = new Location();   location.onLocationChanged().listen((LocationData currentLocation) {
print(currentLocation.latitude);
print(currentLocation.longitude);
})
© www.soinside.com 2019 - 2024. All rights reserved.