我们可以在GoogleMap中设置initialCameraPosition-Flutter

时间:2019-04-10 06:43:50

标签: google-maps dart flutter

我们如何在googleMap中设置摄像机的初始位置,即代替硬编码的经纬度(以下代码)

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

我只希望当前位置为经纬度,而不是硬编码值

1 个答案:

答案 0 :(得分: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);
})