gpt4 book ai didi

flutter - 如何从 Lat Long 值中获取地址并显示到文本字段中

转载 作者:行者123 更新时间:2023-12-04 07:56:32 28 4
gpt4 key购买 nike

在我的项目中,我使用谷歌地图来获取我当前的位置。现在我想从 map 标记中获取地址并将该地址显示到文本字段,但我不明白该怎么做。我对编程很陌生。
下面是我的代码。我接下来该怎么做?

class _MyHomePageState extends State<MyHomePage> {

LatLng _initialcameraposition = LatLng(20.5937, 78.9629);
GoogleMapController _controller;
Location _location = Location();

void _onMapCreated(GoogleMapController _cntlr)
{
_controller = _cntlr;
_location.onLocationChanged.listen((l) {
_controller.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(target: LatLng(l.latitude, l.longitude),zoom: 15),
),
);
});
}


@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Stack(
children: [
GoogleMap(
initialCameraPosition: CameraPosition(target: _initialcameraposition),
mapType: MapType.normal,
onMapCreated: _onMapCreated,
myLocationEnabled: true,
),
],
),
),
);
}
}

最佳答案

您必须使用地理编码器包 ( check description of geocoder package ) 从 LatLang 值中获取地址。并使用 Geolocator 包 ( Check Description of Geolocator pacakge here ) 从设备获取当前位置。
试试这个代码:

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:permission_handler/permission_handler.dart';
import 'package:geolocator/geolocator.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
import 'package:geocoder/geocoder.dart';

void main() {
runApp(MaterialApp(home: MyHomePage()));
}

class MyHomePage extends StatefulWidget{
@override
State createState() {
// TODO: implement createState
return MyHomePageState();
}

}

class MyHomePageState extends State{

@override
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(
body: MyMap(),
);
}
}
class MyMap extends StatefulWidget{

@override
State createState() {
// TODO: implement createState
return MyMapState();
}
}

class MyMapState extends State{
LatLng latlong=null;
CameraPosition _cameraPosition;
GoogleMapController _controller ;
Set<Marker> _markers={};
TextEditingController locationController = TextEditingController();
@override
void initState() {
// TODO: implement initState
super.initState();
_cameraPosition=CameraPosition(target: LatLng(0, 0),zoom: 10.0);
getCurrentLocation();
}

@override
Widget build(BuildContext context) {
// TODO: implement build
return SafeArea(child: Stack(
children: [
(latlong!=null) ?GoogleMap(
initialCameraPosition: _cameraPosition,
onMapCreated: (GoogleMapController controller){
_controller=(controller);
_controller.animateCamera(

CameraUpdate.newCameraPosition(_cameraPosition));
},

markers:_markers ,

):Container(),
Positioned(
top: 50.0,
right: 15.0,
left: 15.0,
child: Container(
height: 50.0,
width: double.infinity,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(3.0),
color: Colors.white,
boxShadow: [
BoxShadow(
color: Colors.grey,
offset: Offset(1.0, 5.0),
blurRadius: 10,
spreadRadius: 3)
],
),
child: TextField(
cursorColor: Colors.black,
controller: locationController,
decoration: InputDecoration(
icon: Container(
margin: EdgeInsets.only(left: 20, top: 0),
width: 10,
height: 10,
child: Icon(
Icons.location_on,
color: Colors.green,
),
),
hintText: "pick up",
border: InputBorder.none,
contentPadding: EdgeInsets.only(left: 15.0, top: 12.0),
),
),
),
),
],
));
}
Future getCurrentLocation() async {
LocationPermission permission = await Geolocator.checkPermission();
if (permission != PermissionStatus.granted) {
LocationPermission permission = await Geolocator.requestPermission();
if (permission != PermissionStatus.granted)
getLocation();
return;
}
getLocation();
}
List<Address> results = [];
getLocation() async
{
Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
print(position.latitude);
setState(() {
latlong=new LatLng(position.latitude, position.longitude);
_cameraPosition=CameraPosition(target:latlong,zoom: 10.0 );
if(_controller!=null)
_controller.animateCamera(

CameraUpdate.newCameraPosition(_cameraPosition));
_markers.add(Marker(markerId: MarkerId("a"),draggable:true,position: latlong,icon: BitmapDescriptor.defaultMarkerWithHue(

BitmapDescriptor.hueBlue),onDragEnd: (_currentlatLng){
latlong = _currentlatLng;

}));
});

getCurrentAddress();
}

getCurrentAddress() async
{

final coordinates = new Coordinates(latlong.latitude, latlong.longitude);
results = await Geocoder.local.findAddressesFromCoordinates(coordinates);
var first = results.first;
if(first!=null) {
var address;
address = first.featureName;
address = " $address, ${first.subLocality}" ;
address = " $address, ${first.subLocality}" ;
address = " $address, ${first.locality}" ;
address = " $address, ${first.countryName}" ;
address = " $address, ${first.postalCode}" ;

locationController.text = address;
}
}
}

关于flutter - 如何从 Lat Long 值中获取地址并显示到文本字段中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66678950/

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