placemarkFromCoordinates geocoding error on flutter

huangapple go评论100阅读模式
英文:

placemarkFromCoordinates geocoding error on flutter

问题

"flutter.baseflow.com/geocoding" 上的渠道上找不到 placemarkFromCoordinates 方法的实现。

这是我的位置提供程序代码:

  1. List<Placemark> placemarks =
  2. await placemarkFromCoordinates(latitude, longitude);
  3. selectedAddress = placemarks.first;

我尝试更改方法,但我不理解错误的根本原因。placemarkFromCoordinates 方法是否已弃用?修复此问题的适当方法是什么?

英文:

No implementation found for method placemarkFromCoordinates on channel
flutter.baseflow.com/geocoding

Here is my Location Provider code:

  1. List&lt;Placemark&gt; placemarks =
  2. await placemarkFromCoordinates(latitude, longitude);
  3. selectedAddress = placemarks.first;

I have tried changing the method but i dont understand the root of the erro. Is placemarkfromCoordinates depreceated. What is the appropriate way to fix this?

答案1

得分: 1

以下是已翻译的内容:

  1. placemarkFromAddress - 此方法接受表示地址的字符串,并返回与地址匹配的Placemark对象列表。

  2. placemarkFromQuery - 此方法接受表示查询的字符串,并返回与查询匹配的Placemark对象列表。查询可以是描述位置的任何文本,例如地标、地址或城市名称。

  3. locationFromAddress - 此方法接受表示地址的字符串,并返回包含地址的纬度和经度的Location对象。

  4. locationFromQuery - 此方法接受表示查询的字符串,并返回包含与查询匹配的位置的纬度和经度的Location对象。

  5. placesNearby - 此方法接受一个Location对象和以米为单位的半径,返回在指定位置半径范围内的Place对象列表。

  6. placesAutocomplete - 此方法接受表示查询的字符串,并返回与查询匹配的AutocompletePrediction对象列表。这些预测可以用于提供位置搜索的自动完成建议。

以下是代码示例以帮助理解:

// 从地址获取地标
final addresses = await placemarkFromAddress('1600 Amphitheatre Parkway, Mountain View, CA');
final selectedAddress = addresses.first;
print(selectedAddress);

// 从查询获取位置
final location = await locationFromQuery('Central Park, New York, NY');
print(location.latitude);
print(location.longitude);

// 获取附近的地点
final places = await placesNearby(Location(40.748817, -73.985428), 1000);
places.forEach((place) {
print(place.name);
});

// 获取自动完成预测
final predictions = await placesAutocomplete('Times Square');
predictions.forEach((prediction) {
print(prediction.description);
});

英文:

The geocoding library in Flutter provides several methods other than placemarkFromCoordinates for converting a location's coordinates into a human-readable address or location information:

  1. placemarkFromAddress - This method takes a string representing an address and returns a list of Placemark objects that match the address.

  2. placemarkFromQuery - This method takes a string representing a query and returns a list of Placemark objects that match the query. The query can be any text that describes a location, such as a landmark, an address, or a city name.

  3. locationFromAddress - This method takes a string representing an address and returns a Location object containing the latitude and longitude of the address.

  4. locationFromQuery - This method takes a string representing a query and returns a Location object containing the latitude and longitude of the location that matches the query.

  5. placesNearby - This method takes a Location object and a radius in meters and returns a list of Place objects that are within the specified radius of the location.

  6. placesAutocomplete - This method takes a string representing a query and returns a list of AutocompletePrediction objects that match the query. The predictions can be used to provide autocomplete suggestions for a location search.

Here are code examples to help:

  1. // Get placemark from address
  2. final addresses = await placemarkFromAddress(&#39;1600 Amphitheatre Parkway, Mountain View, CA&#39;);
  3. final selectedAddress = addresses.first;
  4. print(selectedAddress);
  5. // Get location from query
  6. final location = await locationFromQuery(&#39;Central Park, New York, NY&#39;);
  7. print(location.latitude);
  8. print(location.longitude);
  9. // Get places nearby
  10. final places = await placesNearby(Location(40.748817, -73.985428), 1000);
  11. places.forEach((place) {
  12. print(place.name);
  13. });
  14. // Get autocomplete predictions
  15. final predictions = await placesAutocomplete(&#39;Times Square&#39;);
  16. predictions.forEach((prediction) {
  17. print(prediction.description);
  18. });

答案2

得分: 1

以下是代码部分的翻译:

  1. 相同的问题!这里有一堆链接和我尝试过的代码,但都不起作用。上面的答案不适用,因为它要求用户在文本框中键入他们的位置信息(placemarkFromAddresslocationFromQuery),但在大多数情况下,当前位置应该是自动的!
  2. import 'package:flutter/material.dart';
  3. import 'package:geocoding/geocoding.dart' 隐藏 Location;
  4. import 'package:geolocator/geolocator.dart;
  5. import 'package:provider/provider.dart';
  6. //import 'package:geocode/geocode.dart';
  7. import 'package:location/location.dart';
  8. import 'package:flutter/services.dart';
  9. //==============
  10. // 尝试
  11. //==============
  12. Future<void> _getCurrentLocation() async {
  13. Position position = await Geolocator.getCurrentPosition();
  14. //final GeoCode geoCode = GeoCode();
  15. //final longitude = position.longitude;
  16. //final latitude = position.latitude;
  17. // 从当前位置获取地址
  18. try {
  19. //final addresses = await geoCode.reverseGeocoding(longitude, latitude);
  20. //final first = addresses.first;
  21. List<Placemark> placemarks =
  22. await placemarkFromCoordinates(position.longitude, position.latitude);
  23. setState(() {
  24. _currentLocation = '${placemarks[0].subAdministrativeArea.toString()},'
  25. '${placemarks[0].administrativeArea.toString()},'
  26. '${placemarks[0].country.toString()}';
  27. _isLoading = false;
  28. });
  29. } catch (e) {
  30. // 忽略打印
  31. print(e);
  32. // 忽略打印
  33. print('当前位置不起作用');
  34. _isLoading = false;
  35. }
  36. //==============
  37. // 尝试
  38. //==============
  39. Future<void> _getCurrentLocation() async {
  40. Position position = await Geolocator.getCurrentPosition();
  41. GeoCode geoCode = GeoCode();
  42. // 从当前位置获取地址
  43. try {
  44. // 反向地理编码纬度和经度以获取地址
  45. List<Address> addresses = await geoCode.reverseGeocoding(
  46. latitude: position.latitude, longitude: position.longitude) as List<Address>;
  47. Address address = addresses.first;
  48. setState(() {
  49. _currentLocation =
  50. '${address.street}, ${address.locality}, ${address.countryName}';
  51. _isLoading = false;
  52. });
  53. } catch (e) {
  54. print(e.toString());
  55. _isLoading = false;
  56. }
  57. }
  58. //==============
  59. // 尝试
  60. //==============
  61. Future<void> _getAddressFromLatLng() async {
  62. try {
  63. List<Placemark> placemarks = await placemarkFromCoordinates(
  64. _currentLocation2.latitude, _currentLocation2.longitude);
  65. Placemark place = placemarks[0];
  66. setState(() {
  67. _currentAddress = "${place.locality}, ${place.country}";
  68. });
  69. } catch (e) {
  70. print(e);
  71. }
  72. }
  73. //==============
  74. // 尝试
  75. //==============
  76. Future<Position> _getCurrentLocation2() async {
  77. bool serviceEnabled;
  78. LocationPermission permission;
  79. serviceEnabled = await Geolocator.isLocationServiceEnabled();
  80. if (!serviceEnabled) {
  81. return Future.error('位置服务已禁用。');
  82. }
  83. permission = await Geolocator.checkPermission();
  84. if (permission == LocationPermission.denied) {
  85. permission = await Geolocator.requestPermission();
  86. if (permission == LocationPermission.denied) {
  87. return Future.error('位置权限已被拒绝');
  88. }
  89. }
  90. if (permission == LocationPermission.deniedForever) {
  91. // 永久拒绝权限,适当处理。
  92. return Future.error(
  93. '位置权限已永久拒绝,我们无法请求权限。');
  94. }
  95. await Geolocator.getCurrentPosition(forceAndroidLocationManager: true)
  96. .then((position) {
  97. setState(() {
  98. _currentLocation2 = position;
  99. _getAddressFromLatLng();
  100. });
  101. }).catchError((e) {
  102. print(e);
  103. });
  104. return await Geolocator.getCurrentPosition();
  105. }

希望这可以帮助您理解代码部分的中文翻译。如果有任何其他翻译需求,请随时告诉我。

英文:

Same question! Here's a bunch of links and code I've tried researching, nothing works. The above answer doesn't help because it requires the user to type into a text box their location information (placemarkFromAddress, locationFromQuery etc) but in most cases the current location should be automatic!

  1. import &#39;package:flutter/material.dart&#39;;
  2. import &#39;package:geocoding/geocoding.dart&#39; hide Location;
  3. import &#39;package:geolocator/geolocator.dart&#39;;
  4. import &#39;package:provider/provider.dart&#39;;
  5. //import &#39;package:geocode/geocode.dart&#39;;
  6. import &#39;package:location/location.dart&#39;;
  7. import &#39;package:flutter/services.dart&#39;;
  8. //==============
  9. // Attempt
  10. //==============
  11. Future&lt;void&gt; _getCurrentLocation() async {
  12. Position position = await Geolocator.getCurrentPosition();
  13. //final GeoCode geoCode = GeoCode();
  14. //final longitude = position.longitude;
  15. //final latitude = position.latitude;
  16. // Get the address from the current location
  17. try {
  18. //final addresses = await geoCode.reverseGeocoding(longitude, latitude);
  19. //final first = addresses.first;
  20. List&lt;Placemark&gt; placemarks =
  21. await placemarkFromCoordinates(position.longitude, position.latitude);
  22. setState(() {
  23. _currentLocation = &#39;${placemarks[0].subAdministrativeArea.toString()},&#39;
  24. &#39;${placemarks[0].administrativeArea.toString()},&#39;
  25. &#39;${placemarks[0].country.toString()}&#39;;
  26. _isLoading = false;
  27. });
  28. } catch (e) {
  29. // ignore: avoid_print
  30. print(e);
  31. // ignore: avoid_print
  32. print(&#39;CURRENT LOCATION DIDNT WORK&#39;);
  33. _isLoading = false;
  34. }
  35. //==============
  36. // Attempt
  37. //==============
  38. Future&lt;void&gt; _getCurrentLocation() async {
  39. Position position = await Geolocator.getCurrentPosition();
  40. GeoCode geoCode = GeoCode();
  41. // Get the address from the current location
  42. try {
  43. // Reverse geocode the latitude and longitude to get the address
  44. List&lt;Address&gt; addresses = await geoCode.reverseGeocoding(
  45. latitude: position.latitude, longitude: position.longitude) as List&lt;Address&gt;;
  46. Address address = addresses.first;
  47. setState(() {
  48. _currentLocation =
  49. &#39;${address.street}, ${address.locality}, ${address.countryName}&#39;;
  50. _isLoading = false;
  51. });
  52. } catch (e) {
  53. print(e.toString());
  54. _isLoading = false;
  55. }
  56. }
  57. //==============
  58. // Attempt
  59. //==============
  60. Future&lt;void&gt; _getAddressFromLatLng() async {
  61. try {
  62. List&lt;Placemark&gt; placemarks = await placemarkFromCoordinates(
  63. _currentLocation2.latitude, _currentLocation2.longitude);
  64. Placemark place = placemarks[0];
  65. setState(() {
  66. _currentAddress = &quot;${place.locality}, ${place.country}&quot;;
  67. });
  68. } catch (e) {
  69. print(e);
  70. }
  71. }
  72. //==============
  73. // Attempt
  74. //==============
  75. Future&lt;Position&gt; _getCurrentLocation2() async {
  76. bool serviceEnabled;
  77. LocationPermission permission;
  78. serviceEnabled = await Geolocator.isLocationServiceEnabled();
  79. if (!serviceEnabled) {
  80. return Future.error(&#39;Location services are disabled.&#39;);
  81. }
  82. permission = await Geolocator.checkPermission();
  83. if (permission == LocationPermission.denied) {
  84. permission = await Geolocator.requestPermission();
  85. if (permission == LocationPermission.denied) {
  86. return Future.error(&#39;Location permissions are denied&#39;);
  87. }
  88. }
  89. if (permission == LocationPermission.deniedForever) {
  90. // Permissions are denied forever, handle appropriately.
  91. return Future.error(
  92. &#39;Location permissions are permanently denied, we cannot request permissions.&#39;);
  93. }
  94. await Geolocator.getCurrentPosition(forceAndroidLocationManager: true)
  95. .then((position) {
  96. setState(() {
  97. _currentLocation2 = position;
  98. _getAddressFromLatLng();
  99. });
  100. }).catchError((e) {
  101. print(e);
  102. });
  103. return await Geolocator.getCurrentPosition();
  104. }

huangapple
  • 本文由 发表于 2023年4月7日 00:00:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定