gpt4 book ai didi

Flutter - 从坐标获取地理编码地址

转载 作者:行者123 更新时间:2023-11-29 11:26:51 25 4
gpt4 key购买 nike

我已经在我正在创建的应用程序中实现了一种获取用户经纬度坐标的可靠方法,但似乎无法将这些坐标转换为地标。

我还想弄清楚我需要用什么来打印这些数据。

这就是我试图获取此数据的原因:

List<Placemark> place = [];

void _getPlace() async {
List<Placemark> newPlace = await _geolocator.placemarkFromCoordinates(_position.latitude, _position.longitude);
print(newPlace);
setState(() {
place = newPlace;
});
}

谢谢

编辑:

在 CopsOnRoad 的响应之后,我收到了这个错误:

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: 

NoSuchMethodError: The method 'placemarkFromCoordinates' was called on null.
Receiver: null
Tried calling: placemarkFromCoordinates(51.4998, -0.129)
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:50:5)
#1 _NearbyPageState._getPlace
package:tr/pages/nearby.dart:128
<asynchronous suspension>
#2 _NearbyPageState._controlCard.<anonymous closure>
package:tr/pages/nearby.dart:629
#3 GestureRecognizer.invokeCallback
package:flutter/…/gestures/recognizer.dart:182
#4 TapGestureRecognizer._checkUp
package:flutter/…/gestures/tap.dart:365
#5 TapGestureRecognizer.acceptGesture
package:flutter/…/gestures/tap.dart:312
#6 GestureArenaManager.sweep
package:flutter/…/gestures/arena.dart:156
#7 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent
package:flutter/…/gestures/binding.dart:222
#8 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (p<…>

最佳答案

更新:使用 geocoding

import 'package:geocoding/geocoding.dart';

List<Placemark> placemarks = await placemarkFromCoordinates(52.2165157, 6.9437819);

旧的解决方案:

你已经在那里了,你需要的额外东西是:

String _address = ""; // create this variable

void _getPlace() async {
List<Placemark> newPlace = await _geolocator.placemarkFromCoordinates(_position.latitude, _position.longitude);

// this is all you need
Placemark placeMark = newPlace[0];
String name = placeMark.name;
String subLocality = placeMark.subLocality;
String locality = placeMark.locality;
String administrativeArea = placeMark.administrativeArea;
String postalCode = placeMark.postalCode;
String country = placeMark.country;
String address = "${name}, ${subLocality}, ${locality}, ${administrativeArea} ${postalCode}, ${country}";

print(address);

setState(() {
_address = address; // update _address
});
}

关于Flutter - 从坐标获取地理编码地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57949432/

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