英文:
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<Placemark> 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
以下是已翻译的内容:
-
placemarkFromAddress - 此方法接受表示地址的字符串,并返回与地址匹配的Placemark对象列表。
-
placemarkFromQuery - 此方法接受表示查询的字符串,并返回与查询匹配的Placemark对象列表。查询可以是描述位置的任何文本,例如地标、地址或城市名称。
-
locationFromAddress - 此方法接受表示地址的字符串,并返回包含地址的纬度和经度的Location对象。
-
locationFromQuery - 此方法接受表示查询的字符串,并返回包含与查询匹配的位置的纬度和经度的Location对象。
-
placesNearby - 此方法接受一个Location对象和以米为单位的半径,返回在指定位置半径范围内的Place对象列表。
-
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:
-
placemarkFromAddress - This method takes a string representing an address and returns a list of Placemark objects that match the address.
-
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.
-
locationFromAddress - This method takes a string representing an address and returns a Location object containing the latitude and longitude of the address.
-
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.
-
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.
-
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('1600 Amphitheatre Parkway, Mountain View, CA');
final selectedAddress = addresses.first;
print(selectedAddress);
// Get location from query
final location = await locationFromQuery('Central Park, New York, NY');
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('Times Square');
predictions.forEach((prediction) {
print(prediction.description);
});
答案2
得分: 1
以下是代码部分的翻译:
相同的问题!这里有一堆链接和我尝试过的代码,但都不起作用。上面的答案不适用,因为它要求用户在文本框中键入他们的位置信息(placemarkFromAddress、locationFromQuery等),但在大多数情况下,当前位置应该是自动的!
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 'package:flutter/material.dart';
import 'package:geocoding/geocoding.dart' hide 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';
//==============
// Attempt
//==============
Future<void> _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<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) {
// ignore: avoid_print
print(e);
// ignore: avoid_print
print('CURRENT LOCATION DIDN’T WORK');
_isLoading = false;
}
//==============
// Attempt
//==============
Future<void> _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<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;
}
}
//==============
// Attempt
//==============
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);
}
}
//==============
// Attempt
//==============
Future<Position> _getCurrentLocation2() async {
bool serviceEnabled;
LocationPermission permission;
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
return Future.error('Location services are disabled.');
}
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
return Future.error('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately.
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
}
await Geolocator.getCurrentPosition(forceAndroidLocationManager: true)
.then((position) {
setState(() {
_currentLocation2 = position;
_getAddressFromLatLng();
});
}).catchError((e) {
print(e);
});
return await Geolocator.getCurrentPosition();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论