K.c*_*him 15 google-maps flutter
我正在为我的应用程序使用 google maps flutter 插件。我希望初始相机位置目标(LatLng)与当前设备的纬度和经度完全相等,以便相机在启动时显示用户当前所在的位置。但是,我在尝试执行此操作时遇到了问题。我曾尝试使用 Location 和 Geolocator 包,但没有关于如何做我想要实现的目标的明确示例。
当我尝试使用currentLocation.latitude和currentLocation.longitude位置包作为相机目标中 Latlng 的值时,我遇到了以下错误。
“初始化器只能访问静态成员”
坦率地说,我不知道在这种情况下这意味着什么。我已经阅读了一些示例,但没有一个清楚地实现我真正想要实现的目标
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:location/location.dart';
class Map extends StatefulWidget {
@override
State<Map> createState() => MapState();
}
class MapState extends State<Map> {
Completer<GoogleMapController> _controller = Completer();
var currentLocation = LocationData;
var location = new Location();
Future _getLocation() async {
try {
location.onLocationChanged().listen((LocationData currentLocation) {
print('Latitude:${currentLocation.latitude}');
print('Longitude:${currentLocation.longitude}');
return LatLng(currentLocation.latitude, currentLocation.longitude);
});
} catch (e) {
print('ERROR:$e');
currentLocation = null;
}
}
static final CameraPosition _currentPosition = CameraPosition(
target: LatLng(currentLocation.latitude , currentLocation.longitude),
zoom: 14.4746,
);
@override
void initState() {
_getLocation();
super.initState();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: GoogleMap(
mapType: MapType.normal,
initialCameraPosition: _currentPosition,
zoomGesturesEnabled: true,
myLocationButtonEnabled: true,
rotateGesturesEnabled: true,
tiltGesturesEnabled: true,
onMapCreated: (GoogleMapController controller) {
_controller.complete(controller);
},
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
那是我的代码片段在 _currentPosition 最终产生错误,它表示相机位置使用定位器包将其等同于当前设备的纬度和经度,但 Flutter 正在抱怨。我不知道如何正确地做到这一点。我希望 latlng 目标是设备的 latlng
K.c*_*him 20
在获取用户当前位置的同时显示地图小部件时出现错误的问题是,获取用户的实际当前位置是一个异步操作,因此在整个过程中初始位置将为空,因此我们创建了一个运行的条件等待异步操作完成然后显示地图,然后该值将为非空。我重构了代码并改用了 geolocator 包
import 'package:flutter/cupertino.dart';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class Map extends StatefulWidget {
@override
_MapState createState() => _MapState();
}
class _MapState extends State<Map> {
Completer<GoogleMapController> controller1;
//static LatLng _center = LatLng(-15.4630239974464, 28.363397732282127);
static LatLng _initialPosition;
final Set<Marker> _markers = {};
static LatLng _lastMapPosition = _initialPosition;
@override
void initState() {
super.initState();
_getUserLocation();
}
void _getUserLocation() async {
Position position = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
List<Placemark> placemark = await Geolocator().placemarkFromCoordinates(position.latitude, position.longitude);
setState(() {
_initialPosition = LatLng(position.latitude, position.longitude);
print('${placemark[0].name}');
});
}
_onMapCreated(GoogleMapController controller) {
setState(() {
controller1.complete(controller);
});
}
MapType _currentMapType = MapType.normal;
void _onMapTypeButtonPressed() {
setState(() {
_currentMapType = _currentMapType == MapType.normal
? MapType.satellite
: MapType.normal;
});
}
_onCameraMove(CameraPosition position) {
_lastMapPosition = position.target;
}
_onAddMarkerButtonPressed() {
setState(() {
_markers.add(
Marker(
markerId: MarkerId(_lastMapPosition.toString()),
position: _lastMapPosition,
infoWindow: InfoWindow(
title: "Pizza Parlour",
snippet: "This is a snippet",
onTap: (){
}
),
onTap: (){
},
icon: BitmapDescriptor.defaultMarker));
});
}
Widget mapButton(Function function, Icon icon, Color color) {
return RawMaterialButton(
onPressed: function,
child: icon,
shape: new CircleBorder(),
elevation: 2.0,
fillColor: color,
padding: const EdgeInsets.all(7.0),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: _initialPosition == null ? Container(child: Center(child:Text('loading map..', style: TextStyle(fontFamily: 'Avenir-Medium', color: Colors.grey[400]),),),) : Container(
child: Stack(children: <Widget>[
GoogleMap(
markers: _markers,
mapType: _currentMapType,
initialCameraPosition: CameraPosition(
target: _initialPosition,
zoom: 14.4746,
),
onMapCreated: _onMapCreated,
zoomGesturesEnabled: true,
onCameraMove: _onCameraMove,
myLocationEnabled: true,
compassEnabled: true,
myLocationButtonEnabled: false,
),
Align(
alignment: Alignment.topRight,
child: Container(
margin: EdgeInsets.fromLTRB(0.0, 50.0, 0.0, 0.0),
child: Column(
children: <Widget>[
mapButton(_onAddMarkerButtonPressed,
Icon(
Icons.add_location
), Colors.blue),
mapButton(
_onMapTypeButtonPressed,
Icon(
IconData(0xf473,
fontFamily: CupertinoIcons.iconFont,
fontPackage: CupertinoIcons.iconFontPackage),
),
Colors.green),
],
)),
)
]),
),
);
}
}
Run Code Online (Sandbox Code Playgroud)
您提到的错误意味着您正在尝试访问初始化程序内的非静态变量,这就是您的情况currentLocation。
由于类的每个实例的值currentLocation都不同MapState,因此您会收到上述错误。
删除static和final关键字并尝试在函数_currentPosition内设置变量,initState如下所示 -
class MapState extends State<Map> {
Completer<GoogleMapController> _controller = Completer();
var currentLocation = LocationData;
var location = new Location();
CameraPosition _currentPosition;
Future _getLocation() async {
try {
location.onLocationChanged().listen((LocationData currentLocation) {
print('Latitude:${currentLocation.latitude}');
print('Longitude:${currentLocation.longitude}');
return LatLng(currentLocation.latitude, currentLocation.longitude);
});
} catch (e) {
print('ERROR:$e');
currentLocation = null;
}
}
@override
void initState() {
_getLocation();
currentPosition = CameraPosition(
target: LatLng(currentLocation.latitude , currentLocation.longitude),
zoom: 14.4746,
);
super.initState();
}
//Rest of the code remains same
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15217 次 |
| 最近记录: |