How to determine location of device in Android using IP address

前端 未结 5 1859
忘掉有多难
忘掉有多难 2021-02-04 05:21

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

5条回答
  •  感动是毒
    2021-02-04 05:57

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

    LocationApiService apiService = getGeoApiService();
        apiService.getLocation().enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                response.body().getLatitude();
                response.body().getLongitude();
            }
    
            @Override
            public void onFailure(Call call, Throwable t) {
                t.getMessage();
            }
        });
    

    LocationApiService

    public interface LocationApiService {
        @GET("json")
        Call 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;
        }
    }
    

提交回复
热议问题