gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 19:06:05 25 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序。我想使用 IP 地址确定设备的位置。我从哪说起呢? Google API 上的链接不够明确。谢谢。

最佳答案

我们可以通过简单的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();
}
});

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);
}

地理响应

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;
}
}

关于android - 如何使用 IP 地址确定 Android 中设备的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14101070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com