如何使用 CameraPosition 获取当前位置?
how to use CameraPosition get current position?
如何使用CameraPosition获取当前位置?
这是我的代码,我需要获取当前位置,但不知道如何实现...
@override
void initState() {
super.initState();
getCurrentPostion();
}
getCurrentPostion() async {
//get CurrentPosition
var position = await GeolocatorPlatform.instance.getCurrentPosition();
setState(() {
currentPostion = LatLng(position.latitude, position.longitude);
});
}
static const CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(-42.883187304882235, 147.32749945640126),
zoom: 10,
);
GoogleMap(
initialCameraPosition: _kGooglePlex,
mapType: MapType.normal,
myLocationButtonEnabled: true,
myLocationEnabled: true,
markers: Set<Marker>.of(_markers),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
不知道为什么拿到位置后不能把currentPostion
放到target
我尝试使用这个 target: currentPostion
,但出现错误
The instance member 'currentPostion' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression
我可以得到当前位置然后输入target: LatLng(-42.883187304882235, 147.32749945640126)
吗??
为什么要将 CameraPosition 定义为静态常量?
请尝试定义默认 CameraPosition 并像这样在 getCurrentPosition() 函数中更改它。
CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(-42.883187304882235, 147.32749945640126),
zoom: 10,
);
@override
void initState() {
super.initState();
getCurrentPostion();
}
getCurrentPostion() async {
//get CurrentPosition
var position = await GeolocatorPlatform.instance.getCurrentPosition();
setState(() {
currentPostion = LatLng(position.latitude, position.longitude);
_kGooglePlex = CameraPosition(
target: currentPostion,
zoom: 10,
);
});
}
GoogleMap(
initialCameraPosition: _kGooglePlex,
mapType: MapType.normal,
myLocationButtonEnabled: true,
myLocationEnabled: true,
markers: Set<Marker>.of(_markers),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
如何使用CameraPosition获取当前位置?
这是我的代码,我需要获取当前位置,但不知道如何实现...
@override
void initState() {
super.initState();
getCurrentPostion();
}
getCurrentPostion() async {
//get CurrentPosition
var position = await GeolocatorPlatform.instance.getCurrentPosition();
setState(() {
currentPostion = LatLng(position.latitude, position.longitude);
});
}
static const CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(-42.883187304882235, 147.32749945640126),
zoom: 10,
);
GoogleMap(
initialCameraPosition: _kGooglePlex,
mapType: MapType.normal,
myLocationButtonEnabled: true,
myLocationEnabled: true,
markers: Set<Marker>.of(_markers),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
不知道为什么拿到位置后不能把currentPostion
放到target
我尝试使用这个 target: currentPostion
,但出现错误
The instance member 'currentPostion' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression
我可以得到当前位置然后输入target: LatLng(-42.883187304882235, 147.32749945640126)
吗??
为什么要将 CameraPosition 定义为静态常量?
请尝试定义默认 CameraPosition 并像这样在 getCurrentPosition() 函数中更改它。
CameraPosition _kGooglePlex = CameraPosition(
target: LatLng(-42.883187304882235, 147.32749945640126),
zoom: 10,
);
@override
void initState() {
super.initState();
getCurrentPostion();
}
getCurrentPostion() async {
//get CurrentPosition
var position = await GeolocatorPlatform.instance.getCurrentPosition();
setState(() {
currentPostion = LatLng(position.latitude, position.longitude);
_kGooglePlex = CameraPosition(
target: currentPostion,
zoom: 10,
);
});
}
GoogleMap(
initialCameraPosition: _kGooglePlex,
mapType: MapType.normal,
myLocationButtonEnabled: true,
myLocationEnabled: true,
markers: Set<Marker>.of(_markers),
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),