简体   繁体   中英

How to get device country address through IP address?

I am currently working on a project requiring to identify where a device's country address is. I have tried using Montemagno's Geolocator but the problem is if the user does not use location services or gps on his device. I am thinking that it might be possible using IP address but I don't know how or any recommendation might help. Thanks.

You need to create a http GET request to get the results on JSON format you may use IP API to get country information by ip address. Example get request on :

Example Reguest

return a json data which have country information.

{ "query": "24.48.0.1", "status": "success", "country": "Canada", "countryCode": "CA", "region": "QC", "regionName": "Quebec", "city": "Montreal", "zip": "H1S", "lat": 45.5808, "lon": -73.5825, "timezone": "America/Toronto", "isp": "Le Groupe Videotron Ltee", "org": "Videotron Ltee", "as": "AS5769 Videotron Telecom Ltee" }

"You can use IpInfo to get a user's country by their internet address. unless they're under VPN or Proxy this is your best bet." From user Stavm on This question

You should use a service like MaxMind's GeoIP2 and periodically download databases with updates. If you stay contacting a remote API each time, it will be slow. It's better to have a local database that you refresh on a schedule.

There's a free version , too.

Do you have enough space in the device and bandwidth?

If you have more than 5MB free space and can download update, then you can download the offine gelocation database and query it locally. You can visit IP2Location LITE for free database. However, please take note that you need to update it periodically.

If you do not have enough space, you can use remote API call to get the information. One such free service is IPInfoDB and you can find sample codes in the page. It is a free service since 2010.

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