placemarkFromCoordinates geocoding error on flutter

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

placemarkFromCoordinates geocoding error on flutter

问题

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

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

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

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

英文:

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

Here is my Location Provider code:

  List&lt;Placemark&gt; placemarks =
      await placemarkFromCoordinates(latitude, longitude);
  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:

// Get placemark from address
final addresses = await placemarkFromAddress(&#39;1600 Amphitheatre Parkway, Mountain View, CA&#39;);
final selectedAddress = addresses.first;
print(selectedAddress);

// Get location from query
final location = await locationFromQuery(&#39;Central Park, New York, NY&#39;);
print(location.latitude);
print(location.longitude);

// Get places nearby
final places = await placesNearby(Location(40.748817, -73.985428), 1000);
places.forEach((place) {
  print(place.name);
});

// Get autocomplete predictions
final predictions = await placesAutocomplete(&#39;Times Square&#39;);
predictions.forEach((prediction) {
  print(prediction.description);
});

答案2

得分: 1

以下是代码部分的翻译:

相同的问题!这里有一堆链接和我尝试过的代码,但都不起作用。上面的答案不适用,因为它要求用户在文本框中键入他们的位置信息(placemarkFromAddresslocationFromQuery等),但在大多数情况下,当前位置应该是自动的!

import 'package:flutter/material.dart';
import 'package:geocoding/geocoding.dart' 隐藏 Location;
import 'package:geolocator/geolocator.dart;
import 'package:provider/provider.dart';
//import 'package:geocode/geocode.dart';
import 'package:location/location.dart';
import 'package:flutter/services.dart';

//==============
//   尝试
//==============
Future<void> _getCurrentLocation() async {
    Position position = await Geolocator.getCurrentPosition();
    //final GeoCode geoCode = GeoCode();
    //final longitude = position.longitude;
    //final latitude = position.latitude;
    // 从当前位置获取地址
    try {
      //final addresses = await geoCode.reverseGeocoding(longitude, latitude);
      //final first = addresses.first;

      List<Placemark> placemarks =
          await placemarkFromCoordinates(position.longitude, position.latitude);

      setState(() {
        _currentLocation = '${placemarks[0].subAdministrativeArea.toString()},'
            '${placemarks[0].administrativeArea.toString()},'
            '${placemarks[0].country.toString()}';
        _isLoading = false;
      });
    } catch (e) {
      // 忽略打印
      print(e);
      // 忽略打印
      print('当前位置不起作用');
      _isLoading = false;
    }


//==============
//   尝试
//==============
Future<void> _getCurrentLocation() async {
  Position position = await Geolocator.getCurrentPosition();
  GeoCode geoCode = GeoCode();

  // 从当前位置获取地址
  try {
    // 反向地理编码纬度和经度以获取地址
    List<Address> addresses = await geoCode.reverseGeocoding(
        latitude: position.latitude, longitude: position.longitude) as List<Address>;
    Address address = addresses.first;

    setState(() {
      _currentLocation =
          '${address.street}, ${address.locality}, ${address.countryName}';
      _isLoading = false;
    });
  } catch (e) {
    print(e.toString());
    _isLoading = false;
  }
}

//==============
//   尝试
//==============
Future<void> _getAddressFromLatLng() async {
    try {
      List<Placemark> placemarks = await placemarkFromCoordinates(
          _currentLocation2.latitude, _currentLocation2.longitude);

      Placemark place = placemarks[0];

      setState(() {
        _currentAddress = "${place.locality}, ${place.country}";
      });
    } catch (e) {
      print(e);
    }
  }

  //==============
  //   尝试
  //==============
  Future<Position> _getCurrentLocation2() async {
    bool serviceEnabled;
    LocationPermission permission;
    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      return Future.error('位置服务已禁用。');
    }
    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.denied) {
        return Future.error('位置权限已被拒绝');
      }
    }
    if (permission == LocationPermission.deniedForever) {
      // 永久拒绝权限,适当处理。
      return Future.error(
          '位置权限已永久拒绝,我们无法请求权限。');
    }
    await Geolocator.getCurrentPosition(forceAndroidLocationManager: true)
        .then((position) {
      setState(() {
        _currentLocation2 = position;
        _getAddressFromLatLng();
      });
    }).catchError((e) {
      print(e);
    });
    return await Geolocator.getCurrentPosition();
  }

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

英文:

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!

    import &#39;package:flutter/material.dart&#39;;
import &#39;package:geocoding/geocoding.dart&#39; hide Location;
import &#39;package:geolocator/geolocator.dart&#39;;
import &#39;package:provider/provider.dart&#39;;
//import &#39;package:geocode/geocode.dart&#39;;
import &#39;package:location/location.dart&#39;;
import &#39;package:flutter/services.dart&#39;;
//==============
//   Attempt
//==============
Future&lt;void&gt; _getCurrentLocation() async {
Position position = await Geolocator.getCurrentPosition();
//final GeoCode geoCode = GeoCode();
//final longitude = position.longitude;
//final latitude = position.latitude;
// Get the address from the current location
try {
//final addresses = await geoCode.reverseGeocoding(longitude, latitude);
//final first = addresses.first;
List&lt;Placemark&gt; placemarks =
await placemarkFromCoordinates(position.longitude, position.latitude);
setState(() {
_currentLocation = &#39;${placemarks[0].subAdministrativeArea.toString()},&#39;
&#39;${placemarks[0].administrativeArea.toString()},&#39;
&#39;${placemarks[0].country.toString()}&#39;;
_isLoading = false;
});
} catch (e) {
// ignore: avoid_print
print(e);
// ignore: avoid_print
print(&#39;CURRENT LOCATION DIDN’T WORK&#39;);
_isLoading = false;
}
//==============
//   Attempt
//==============  
Future&lt;void&gt; _getCurrentLocation() async {
Position position = await Geolocator.getCurrentPosition();
GeoCode geoCode = GeoCode();
// Get the address from the current location
try {
// Reverse geocode the latitude and longitude to get the address
List&lt;Address&gt; addresses = await geoCode.reverseGeocoding(
latitude: position.latitude, longitude: position.longitude) as List&lt;Address&gt;;
Address address = addresses.first;
setState(() {
_currentLocation =
&#39;${address.street}, ${address.locality}, ${address.countryName}&#39;;
_isLoading = false;
});
} catch (e) {
print(e.toString());
_isLoading = false;
}
}
//==============
//   Attempt
//==============
Future&lt;void&gt; _getAddressFromLatLng() async {
try {
List&lt;Placemark&gt; placemarks = await placemarkFromCoordinates(
_currentLocation2.latitude, _currentLocation2.longitude);
Placemark place = placemarks[0];
setState(() {
_currentAddress = &quot;${place.locality}, ${place.country}&quot;;
});
} catch (e) {
print(e);
}
}
//==============
//   Attempt
//==============
Future&lt;Position&gt; _getCurrentLocation2() async {
bool serviceEnabled;
LocationPermission permission;
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return Future.error(&#39;Location services are disabled.&#39;);
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error(&#39;Location permissions are denied&#39;);
}
}
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately.
return Future.error(
&#39;Location permissions are permanently denied, we cannot request permissions.&#39;);
}
await Geolocator.getCurrentPosition(forceAndroidLocationManager: true)
.then((position) {
setState(() {
_currentLocation2 = position;
_getAddressFromLatLng();
});
}).catchError((e) {
print(e);
});
return await Geolocator.getCurrentPosition();
}

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:

确定