最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

flutter - Google Places Autocomplete always returns Canadian addresses, even when setting a US locationbias - Stack Overflow

matteradmin2PV0评论

I’m using flutter_google_places_hoc081098 (or the official Android Places SDK) to autocomplete addresses in my Flutter app. Despite explicitly setting country filters or lat/lng bias for the United States, I keep getting results for Canadian locations. I’m physically located in the US (or so I believe), but the suggestions are still biased toward Canada What I’ve Tried Removing all code restrictions

Omitted components: [Component(Component.country, "us")], or set setCountries(listOf("US")) to see if that helps.

Same result: still seeing Canadian suggestions.

Forcing a US location + radius

Passed location: Location(lat: 40.7128, lng: -74.0060) (New York) and radius: 80000, plus strictbounds: true.

In theory, that should show addresses only around NYC.

Still mostly local Canadian results unless I type a very specific query like “New York, NY, USA.”

Checking the device’s or emulator’s location

Set the Android emulator location to Florida or NYC in Extended Controls → Location.

Verified the lat/lng is actually in the US.

No change in suggestions.

Confirming IP location

Visited iplocation to verify my IP geolocates in the US.

Still seeing Canadian addresses in the Places overlay.

Tried a VPN with a US endpoint; no consistent improvement.

Ensuring the old “Places API” is enabled

Billing is on, “Places API (New)” is enabled, also enabled the old “Places API” in Google Cloud.

Key restrictions are correct (app package name, SHA-1).

Key and billing are definitely working (no more REQUEST_DENIED unless I restrict the key incorrectly).

Typing a distinct US query

If I type “New Y” or “Los A,” I sometimes still get local “New York Style Pizza” or “Los Amigos” near me.

Must type “New York, NY, USA” fully to see the city.

This is better, but still not the auto-suggestions I want for US addresses by default.

this is my code to autocomplelocation: 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!);
    }
  },
);

} }

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far