I have added Google Maps to my app but I want to have it display the user's current location when it is first displayed.
我已经在我的应用程序中添加了谷歌地图,但我想让它在第一次显示时显示用户的当前位置。
I found code here on this site here: Current location on the google maps (Flutter)
我在这个网站上找到了代码:谷歌地图上的当前位置(Ffltter)
but I am getting an error when I run the code.
但是当我运行代码时,我收到一个错误。
Here is my code:
以下是我的代码:
import 'dart:async';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geolocator/geolocator.dart';
import 'package:flutter/material.dart';
class MapPropertySearchResults extends StatefulWidget {
const MapPropertySearchResults({super.key});
@override
State<MapPropertySearchResults> createState() =>
_MapPropertySearchResultsState();
}
class _MapPropertySearchResultsState extends State<MapPropertySearchResults> {
late GoogleMapController mapController;
late LatLng _currentPosition;
bool _isLoading = true;
@override
void initState() {
super.initState();
getLocation();
}
getLocation() async {
LocationPermission permission;
permission = await Geolocator.requestPermission();
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
double lat = position.latitude;
double long = position.longitude;
LatLng location = LatLng(lat, long);
setState(() {
_currentPosition = location;
_isLoading = false;
});
}
void _onMapCreated(GoogleMapController controller) {
mapController = controller;
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Map'),
),
body: _isLoading ?
const Center(child:CircularProgressIndicator()) :
GoogleMap(
onMapCreated: _onMapCreated,
initialCameraPosition: CameraPosition(
target: _currentPosition,
zoom: 16.0,
),
),
);
}
}
The execution stops in the platform_channel.dart file, which is not my file.
执行在Platform_Channel el.dart文件中停止,该文件不是我的文件。
This is the error message in the platform_channel.dart file:
以下是Platform_Channel.dart文件中的错误消息:
What am I doing wrong because this is the solution found in the other question I referenced above.
Thanks
我做错了什么,因为这是我在上面提到的另一个问题中找到的解决方案。谢谢
更多回答
优秀答案推荐
Stop the project and start again.
停止项目并重新开始。
This means Android native code can't catch flutter's method request.
这意味着Android原生代码无法捕获Ffltter的方法请求。
更多回答
我是一名优秀的程序员,十分优秀!