flutter_google_places 未显示自动完成搜索结果
flutter_google_places not showing autocomplete search results
我正在学习以下 link 中关于使用 Google 地图 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;
}
}`
问题是,无论我在搜索栏中输入什么,都不会 autocomplete/suggest 任何内容。
示例:
在这个例子中,我想列出一个包含单词 grand 的地方,例如 'Grand Central Station'。
我明白了。 _search()
函数有一个半径 radius: center == null ? null : 10000);
,它用来计算你周围在你给它的半径范围内的区域。我的模拟器将我的位置设置为在大西洋中部(单独的问题,但非常小)。由于在我位于海洋中央的位置附近没有以 'grand' 开头的地方,因此没有任何结果。我将半径从 10000
增加到 10000000
,它开始寻找位置。
检查您的账单是否在 Google APIS
上激活
在控制台中查找错误。就我而言,我的帐单已关联且相关 API 已启用,但它从未显示预测。然后我添加了以下 strictBounds & types 作为非空值。
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")],
);
我正在学习以下 link 中关于使用 Google 地图 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;
}
}`
问题是,无论我在搜索栏中输入什么,都不会 autocomplete/suggest 任何内容。
示例:
在这个例子中,我想列出一个包含单词 grand 的地方,例如 'Grand Central Station'。
我明白了。 _search()
函数有一个半径 radius: center == null ? null : 10000);
,它用来计算你周围在你给它的半径范围内的区域。我的模拟器将我的位置设置为在大西洋中部(单独的问题,但非常小)。由于在我位于海洋中央的位置附近没有以 'grand' 开头的地方,因此没有任何结果。我将半径从 10000
增加到 10000000
,它开始寻找位置。
检查您的账单是否在 Google APIS
上激活在控制台中查找错误。就我而言,我的帐单已关联且相关 API 已启用,但它从未显示预测。然后我添加了以下 strictBounds & types 作为非空值。
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")],
);