eva*_*aus 2 google-maps dart google-places-api flutter google-places-autocomplete
我正在关注以下链接中有关使用 Google Maps API 创建搜索功能以在地图上查找地点的教程:
以下是我认为构成问题核心的代码:
/**
* Retrieves the user's location
* NOTE: This pretty much does the same thing as _animateToUser,
* but I separated the two
*/
Future<LatLng> getUserLocation() async {
var currentLocation = <String, double>{};
final uLocation = LocationManager.Location();
try {
currentLocation = await uLocation.getLocation();
final lat = currentLocation["latitude"];
final lng = currentLocation["longitude"];
final center = LatLng(lat, lng);
return center;
} on Exception {
currentLocation = null;
return null;
}
}
/**
* Searches for locations using Google Maps API
*/
Future<void> _search() async {
try {
final center = await getUserLocation();
Prediction p = await PlacesAutocomplete.show(
context: context,
strictbounds: center == null ? false : true,
apiKey: kGoogleApiKey,
onError: onError,
mode: Mode.overlay,
language: "en",
location: center == null
? null
: Location(center.latitude, center.longitude),
radius: center == null ? null : 10000);
showDetailPlace(p.placeId);
} catch (e) {
return;
}
}
/**
* Shows details of a place
*/
Future<Null> showDetailPlace(String placeId) async {
if (placeId != null) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) =>
PlaceDetailWidget(placeId)),
);
}
} /**
* Retrieves the user's location
* NOTE: This pretty much does the same thing as _animateToUser,
* but I separated the two
*/
Future<LatLng> getUserLocation() async {
var currentLocation = <String, double>{};
final uLocation = LocationManager.Location();
try {
currentLocation = await uLocation.getLocation();
final lat = currentLocation["latitude"];
final lng = currentLocation["longitude"];
final center = LatLng(lat, lng);
return center;
} on Exception {
currentLocation = null;
return null;
}
}`
Run Code Online (Sandbox Code Playgroud)
问题是,无论我在搜索栏中输入什么,它都不会自动完成/建议任何内容。
在这个例子中,我想列出一个包含“grand”这个词的地方的列表,例如“Grand Central Station”。
在控制台中查找错误。就我而言,我的账单已链接并启用了相关 API,但它仍然从不显示预测。然后我添加以下strictBounds和类型作为非空值。
Prediction p = await PlacesAutocomplete.show(
context: context,
apiKey: Local.googleMapsAPIKey,
radius: 10000000,
types: [],
strictbounds: false,
mode: Mode.overlay,
language: "fr",
decoration: InputDecoration(
hintText: 'Search',
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(20),
borderSide: BorderSide(
color: Colors.white,
),
),
),
components: [Component(Component.country, "fr")],
);
Run Code Online (Sandbox Code Playgroud)
我想到了。该_search()函数有一个半径radius: center == null ? null : 10000);,用于计算您给定的半径范围内您周围的区域。我的模拟器有我的位置在大西洋中间(单独的问题,但非常小)。由于在我位于海洋中间的位置附近没有以“grand”开头的地方,因此什么也没有发生。我将半径从10000to 增加到10000000它开始寻找位置。
| 归档时间: |
|
| 查看次数: |
5713 次 |
| 最近记录: |