Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How TO change componentRestrictions -- Country Dynamically? #103

Open
Rohan527523 opened this issue Dec 30, 2021 · 4 comments
Open

How TO change componentRestrictions -- Country Dynamically? #103

Rohan527523 opened this issue Dec 30, 2021 · 4 comments

Comments

@Rohan527523
Copy link

I want to change componentRestrictions country dynamically.

@Juxlarry
Copy link

To be able to do this, I had to do a little digging and that is the solution I find online.

For instance, you want to pass your current location dynamically as the country. Google has the Geolocation API that can be called and used to get current location. To do this, since you're already using the Google API I'm sure you already have the Keys setup. Go and enable the Google Geolocation API as part of your project as well.
Then you need to hit the Geolocation API Url, you'll find that in the Docs. This is the link here.
When you read the Docs, you can choose not to pass anything as by default IP is passed to the request body. This returns a response from where you can fetch the latitude and longitude of your current location. Well that is the first step.

Next, you need to enable Google GeoCoding as part of your APIs from Google as well. With this you can now pass your lat and lng to Google Geocode to return a response. This is known as reverse geocoding by Google. Again this is the link to the docs here .
The response object is a lot so I formatted mine to return as little data as possible (Thinking of pricing here). This is my sample url link : https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&key=YOUR_API_KEY&result_type=country

Added the result type to country as that is what I want. Now after getting this, I can save the value of the country code to a variable and then pass this to the options object, before it is passed to the Input field to restrict the places to your country dynamically.

I hope this helps. You can ask this as a Stack Overflow Question and I provide the answer. I believe it will help a lot.

@sscholle
Copy link

sscholle commented Jul 21, 2022

I think what he is asking is, how can one "change" the country restriction... this seemingly is only applied once on INIT.

Google's JS API does support changing this via public API, but no support is enabled on this component for that kind of behavior.

@coding-noddy
Copy link

Expecting what @sscholle said. We are not able to put multiple countries under componentRestriction because country type is string only where in google api it is string | Array<string>. When this can be fixed?

@dancornilov
Copy link

Maybe this one can help you
angular-magic/ngx-gp-autocomplete#2 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants