Getting country code with the help of device IP address

余生颓废 提交于 2019-12-10 19:07:02

问题


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



来源:https://stackoverflow.com/questions/17475232/getting-country-code-with-the-help-of-device-ip-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!