Flutter中Google Places Autocomplete始终返回加拿大地址求助
地址自动补全持续返回加拿大结果(已设置美国过滤)
我在Flutter应用中使用flutter_google_places_hoc081098(或官方Android Places SDK)实现地址自动补全功能。尽管已明确设置美国国家过滤器或经纬度偏好,仍持续返回加拿大地址结果。我实际位于美国,但建议结果仍偏向加拿大。
已尝试的操作
- 移除所有代码限制:省略
components: [Component(Component.country, "us")],或设置setCountries(listOf("US")),结果仍显示加拿大地址建议。 - 强制设置美国位置+半径:传入纽约坐标(lat:40.7128, lng:-74.0060)、半径80000并开启
strictbounds:true,理论应仅显示纽约周边地址,但仍以加拿大本地结果为主,除非输入"New York, NY, USA"这类精准查询。 - 检查设备/模拟器位置:在Android模拟器的Extended Controls→Location中设置佛罗里达或纽约坐标,确认经纬度在美国,但建议无变化。
- 验证IP位置:确认IP定位在美国,仍在Places浮层中看到加拿大地址;尝试美国节点VPN,无持续改善。
- 确保旧版Places API启用:已开启计费,启用了"Places API (New)"及Google Cloud中的旧版"Places API";密钥限制(应用包名、SHA-1)正确,密钥和计费正常(仅在密钥限制错误时出现REQUEST_DENIED)。
- 输入明确美国查询:输入"New Y"或"Los A"时,有时仍显示本地的"New York Style Pizza"或"Los Amigos",需完整输入"New York, NY, USA"才能显示对应城市,未达到默认优先美国地址的预期。
地址自动补全代码
import 'package:flutter/material.dart'; import 'package:flutter_google_places_hoc081098/flutter_google_places_hoc081098.dart'; import 'package:google_maps_webservice/places.dart'; const kGoogleApiKey = "GOOGLE API KEY"; class LocationAutocompleteField extends StatelessWidget { final Function(String) onLocationSelected; const LocationAutocompleteField({Key? key, required this.onLocationSelected}) : super(key: key); @override Widget build(BuildContext context) { return TextFormField( readOnly: true, decoration: InputDecoration( hintText: "Enter your address", border: OutlineInputBorder(borderRadius: BorderRadius.circular(12)), prefixIcon: Icon(Icons.location_on), ), onTap: () async { Prediction? prediction = await PlacesAutocomplete.show( context: context, apiKey: kGoogleApiKey, mode: Mode.overlay, language: "en", components: [Component(Component.country, "us")], // Change "ca" to "us" for US places or remove this line to not restrict by country. ); if (prediction != null && prediction.description != null) { onLocationSelected(prediction.description!); } }, ); } }
内容的提问来源于stack exchange,提问作者Alejandro Ruthman
相关产品推荐
相关产品推荐

