如何使用IP地址确定Android中设备的位置
我正在开发一个Android应用程序。我想使用设备的IP地址确定设备的位置。我从哪里开始?谷歌API上的链接不够明确。谢谢。有多个web服务,可以从IP地址为您提供纬度和经度值 其中之一就是api.ipinfo.com 对于ex,您可以通过发送如下请求来确定纬度和经度如何使用IP地址确定Android中设备的位置,android,geolocation,Android,Geolocation,我正在开发一个Android应用程序。我想使用设备的IP地址确定设备的位置。我从哪里开始?谷歌API上的链接不够明确。谢谢。有多个web服务,可以从IP地址为您提供纬度和经度值 其中之一就是api.ipinfo.com 对于ex,您可以通过发送如下请求来确定纬度和经度 http://api.ipinfodb.com/v3/ip-city/?key=<your_api_key> &ip=74.125.45.100&format=json
http://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>
&ip=74.125.45.100&format=json
http://api.ipinfodb.com/v3/ip-city/?key=
&ip=74.125.45.100&格式=json
这将以json格式返回数据,您可以通过设置format=XML获得XML响应(您需要注册才能获得API密钥)
或者您可以从下载数据库并链接
你可以下载数据集,但你必须不断更新它。我用它来获取设备的ip、国家和城市
为了测试您是否可以使用firefox插件进行HTTP请求
只需复制此URL并在RESTCLIENT中进行测试,或在浏览器地址栏中进行测试
希望这有帮助 结帐
示例帖子:
https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY
{
"location": {
"lat": 51.0,
"lng": -0.1
},
"accuracy": 1200.4
}
样本响应:
https://www.googleapis.com/geolocation/v1/geolocate?key=YOUR_API_KEY
{
"location": {
"lat": 51.0,
"lng": -0.1
},
"accuracy": 1200.4
}
限制:
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次额外查询,每天最多100000次
您可以使用
这是一项免费的开源服务。您可以直接使用该服务,也可以在个人服务器上运行该服务。它也可以很容易地部署到Heroku
要获取IP位置,只需使用OkHttp发送http/https请求:
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
}
我们可以通过简单的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();
}
});
georresponse
public interface LocationApiService {
@GET("json")
Call<GeoResponse> getLocation();
}
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;
}
}
我已尝试获取此网站的API密钥,但未成功。即使我提供了该密钥,它仍会不断请求我的位置。您愿意共享您的密钥吗?