如何使用IP地址确定Android设备的位置

mun*_* pk 15 android geolocation

我正在开发一款Android应用.我想使用其IP地址确定设备的位置.我从哪里开始?Google API上的链接不够完善.谢谢.

Dyn*_*ind 6

您应该使用fallowing API来获取使用ip的位置.

Ip2Location API

  • 遗憾的是,这些年之后,此链接上的任何内容均无效 (2认同)

Lev*_*yan 6

我们可以通过简单的API调用来获取位置,但精度较低。

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();
        }
    });
Run Code Online (Sandbox Code Playgroud)

位置API服务

public interface LocationApiService {
    @GET("json")
    Call<GeoResponse> getLocation();
}
Run Code Online (Sandbox Code Playgroud)

获取GeoApiService()

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);
    }
Run Code Online (Sandbox Code Playgroud)

地理响应

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;
    }
}
Run Code Online (Sandbox Code Playgroud)


cyn*_*yn0 2

有多种网络服务可以为您提供 IP 地址的纬度和经度值。

api.ipinfodb.com就是其中之一

例如,您可以通过发送请求来获取纬度和经度

 http://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>
               &ip=74.125.45.100&format=json
Run Code Online (Sandbox Code Playgroud)

这将返回 json 格式的数据,您可以通过设置 format=xml 来获取 XML 格式的响应。(您需要注册才能获取 API 密钥)。

或者您可以从此链接下载数据库

你可以下载数据集,但你必须不断更新它。