简体   繁体   中英

Add dropdown with cities

I have created a React Native app and want a function where the user can specify in which city they live so that it can be shown to other users. It should be a text input where the user can search for a city and get a dropdown of choices to choose from. The city should then preferably be saved as an ID in our database that can be converted back to a localized version of the city for all users. Let's say someone lives in the UK it should say Gothenburg and if you live in Sweden it should say Göteborg. I have looked at the following options but none is optimal for us:

  • Google Places Autocomplete API to fetch the city, save the place ID in our database, and reverse geocode it. This becomes very expensive for us because it is a social app and each user would do approximately 20 geocoding requests when starting the app
  • Create a database with names of cities. This is not very scalable as you need to add new entries for each new country you want to support
  • Let users manually enter their city. This is prone to errors because users can write whatever they want

I would be happy to receive some suggestions on how to solve this issue.

Here are the things you currently want:

  • Provide your users an Autocomplete functionality
  • Get the city based on their selection
  • Save the city and store it on your database
  • provide users a localize text of the city they chose

For the third one, storing the city provided by the API may not be allowed by Google. I can see that you are aware of the terms, but here is the Service Specific Terms of what is allowed to be cached.

Depending on your use case tho, this can still be doable. In terms of pricing and efficiency, see this pricing sheet for your reference. Right now, you are doing Places Autocomplete and Reverse Geocoding the selection to get the City. You are actually paying a little more. Assuming that you are even using per session autocomplete, this is what you are paying for every 1000 request:

  • Autocomplete without Places Details - Per Session = $17.00 +
  • Geocoding API = $5.00
  • Total Cost: $22 per thousand calls

So instead of using Reverse Geocoding, you can instead use Place Details which serves the same purpose. The city can be retrieve on a Place Detail request which is under the Basic Data, with the Address Component field. See this link for the list of fields Place Details can retrieve. When retrieving fields with Basic Data, those are free of charge, unlike Contact and Atmosphere Data. So make sure to make use of the "fields" parameter for the Place Detail request that you will use as if you did not specify that, all Data will be retrieve and you will be charged accordingly. So if you use that with Autocomplete, your cost now will be:

  • Autocomplete (included with Places Details) - Per Session + Places Details + Basic Data = $17.00 per thousand calls

Now that you have the City, you would now want to present it in the customer's local language. Place Details contains the optional parameter "language". And by simply getting the customer's current language in the device, you can achieve this. Here is how you can get it for your reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM