gpt4 book ai didi

android - 使用 'google_maps_flutter' 插件无法在 Flutter 中显示当前用户位置

转载 作者:行者123 更新时间:2023-12-01 18:34:19 28 4
gpt4 key购买 nike

我正在尝试使用 flutter 在 map View 中显示当前用户位置。在iOS中,很简单,只需添加self.mapView.showsUserLocation = YES;这将在 map View 中显示当前用户位置。但是在flutter中,看起来很复杂,需要写很多代码和插件。
顺便问一下,对于 Flutter 中的这个功能,有没有像原生 iOS 这样的简单解决方案?
我也试图以 flutter 的方式实现它,为此目的使用“geolocator”和“google_maps_flutter”插件,但在运行时在日志中出现异常。

Installing build\app\outputs\apk\app.apk...
Debug service listening on ws://127.0.0.1:51924/x_FSkxlnOcY=/ws
Syncing files to device sdk gphone x86...
W/one.cliffjumpe(14833): Accessing hidden method Landroid/content/Context;->getFeatureId()Ljava/lang/String; (greylist, reflection, allowed)
E/flutter (14833): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: PlatformException(ERROR_GEOCODING_COORDINATES, grpc failed, null)
E/flutter (14833): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:569:7)
E/flutter (14833): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
这是我的代码 fragment -
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 MapScreen extends StatefulWidget {
@override
_MapState createState() => _MapState();
}

class _MapState extends State<MapScreen> {
Completer<GoogleMapController> controller1;

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 : ${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),
],
)),
)
]),
),
);
}
}

最佳答案

请使用插件geolocator Plugin连同您的 Google_maps_flutter 插入
不要忘记添加使用权限 android:name="android.permission.ACCESS_FINE_LOCATION"在您的 androidmanifest.xml

关于android - 使用 'google_maps_flutter' 插件无法在 Flutter 中显示当前用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63095308/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com