问题
I am working on an app that have registration process and my requirements are:
Requirements:
- Automatically select country code with the help of device IP address tracing.
- Detect user location during registration and save it.
I searched a bit but did not find any useful information that would lead to an answer, there must be way but what is that? Please let me guide to the way
回答1:
If you address from user as an input during registration, you can find geo coordinates from address. Fore more information see https://developers.google.com/maps/documentation/geocoding/#Geocoding
Regarding country code from IP address, you can also utilize an online service like http://www.whois.net/ip-address-lookup/ and parse the response to retrieve the country code. Here is how you can parse it (the code is in PHP but you can easily modify it)
回答2:
Get user location:
String locationProvider = LocationManager.NETWORK_PROVIDER;
// Or use LocationManager.GPS_PROVIDER
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);
http://developer.android.com/guide/topics/location/strategies.html
Use GeoCoder to get the Address (with country)
GeoCoder.getFromLocation()
http://developer.android.com/reference/android/location/Geocoder.html
来源:https://stackoverflow.com/questions/17475232/getting-country-code-with-the-help-of-device-ip-address