Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine location of device in Android using IP address

I am developing an Android app. I want to determine the location of the device using its IP address. Where do I start? The links on Google APIs are not conclusive enough. Thanks.

like image 910
mungaih pk Avatar asked Dec 31 '12 11:12

mungaih pk


2 Answers

Checkout Google Maps Geolocation API

Sample POST:

https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY

Sample response:

{
  "location": {
    "lat": 51.0,
    "lng": -0.1
  },
  "accuracy": 1200.4
}

Limits:

Users of the standard API:

2,500 free queries per day 10 query per second, per user Enable pay-as-you-go billing to unlock higher quotas:

$0.50 USD / 1000 additional queries, up to 100,000 daily.

MORE

like image 72
Dawid Avatar answered Oct 21 '22 21:10

Dawid


We can get the location by simple API call, but it will have low accuracy.

LocationApiService apiService = getGeoApiService();
    apiService.getLocation().enqueue(new Callback<GeoResponse>() {
        @Override
        public void onResponse(Call<GeoResponse> call, Response<GeoResponse> response) {
            response.body().getLatitude();
            response.body().getLongitude();
        }

        @Override
        public void onFailure(Call<GeoResponse> call, Throwable t) {
            t.getMessage();
        }
    });

LocationApiService

public interface LocationApiService {
    @GET("json")
    Call<GeoResponse> getLocation();
}

getGeoApiService()

public static final String BASE_URL = "http://ip-api.com/";

public static LocationApiService getGeoApiService() {
        return new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(LocationApiService.class);
    }

GeoResponse

public class GeoResponse {

    private String as;
    private String city;
    private String country;
    private String countryCode;
    private String isp;
    @SerializedName("lat")
    private double latitude;
    @SerializedName("lon")
    private double longitude;
    private String org;
    private String query;
    private String region;
    private String regionName;
    private String timezone;

    @Override
    public String toString() {
        return "countryCode: " + countryCode + ", isp: " + isp + ", city: " + city;
    }

    public String getAs() {
        return as;
    }

    public void setAs(String as) {
        this.as = as;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCountryCode() {
        return countryCode;
    }

    public void setCountryCode(String countryCode) {
        this.countryCode = countryCode;
    }

    public String getIsp() {
        return isp;
    }

    public void setIsp(String isp) {
        this.isp = isp;
    }

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public String getOrg() {
        return org;
    }

    public void setOrg(String org) {
        this.org = org;
    }

    public String getQuery() {
        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public String getRegion() {
        return region;
    }

    public void setRegion(String region) {
        this.region = region;
    }

    public String getRegionName() {
        return regionName;
    }

    public void setRegionName(String regionName) {
        this.regionName = regionName;
    }


    public String getTimezone() {
        return timezone;
    }

    public void setTimezone(String timezone) {
        this.timezone = timezone;
    }
}
like image 30
Levon Petrosyan Avatar answered Oct 21 '22 23:10

Levon Petrosyan