Getting country code with the help of device IP ad

2019-07-06 17:24发布

问题:

I am working on an app that have registration process and my requirements are:

Requirements:

  1. Automatically select country code with the help of device IP address tracing.
  2. 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