我正在开发一个安卓应用程序。我想使用设备的IP地址确定设备的位置。我该从哪里开始?Google APIs上的链接不够明确。谢谢。
我正在开发一个安卓应用程序。我想使用设备的IP地址确定设备的位置。我该从哪里开始?Google APIs上的链接不够明确。谢谢。
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();
}
});
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;
}
}
请查看Google Maps Geolocation API。
示例POST:
https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY
响应示例:
{
"location": {
"lat": 51.0,
"lng": -0.1
},
"accuracy": 1200.4
}
限制:
标准API用户:
每天免费查询2500次
每用户每秒10个查询 启用按需计费以解锁更高配额:$0.50美元/每1000次附加查询,每天最高可达100,000次。
https://geolocation-db.com/json/your_ip_address
限制:每个IP地址每分钟最多发出45个请求
String url = "https://freegeoip.net/json/"
Request mRequest = new Request.Builder()
.url(url)
.build();
Context mContext // A UI context
new OkHttpClient().newCall(mRequest).enqueue(new Callback() {
Handler mainHandler = new Handler(mContext.getApplicationContext().getMainLooper());
@Override
public void onResponse(Call call, Response response) {
String responseBody = null;
try {
responseBody = response.body().string();
} catch (final IOException e) {
mainHandler.post(new Runnable() {
@Override
public void run() {
// handle error
}
});
}
final String finalResponseBody = responseBody;
mainHandler.post(new Runnable() {
@Override
public void run() {
// handle response body
try {
JSONObject locationObject = new JSONObject(finalResponseBody);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
@Override
public void onFailure(Call call, final IOException e) {
mainHandler.post(new Runnable() {
@Override
public void run() {
mNetworkListener.onNetworkError(e.getMessage());
}
});
}
});
响应看起来会像这样:
{
"ip": "192.30.253.113",
"country_code": "US",
"country_name": "United States",
"region_code": "CA",
"region_name": "California",
"city": "San Francisco",
"zip_code": "94107",
"time_zone": "America/Los_Angeles",
"latitude": 37.7697,
"longitude": -122.3933,
"metro_code": 807
}
我使用this来获取设备的IP地址、国家和城市。
为了测试,您可以使用RESTCLIENT,这是一个用于进行HTTP请求的Firefox插件。
只需复制https://addons.mozilla.org/en-us/firefox/addon/restclient/这个URL,在RESTCLIENT中进行测试或者直接粘贴到浏览器地址栏中即可。
希望这能帮到您!