任何人都知道是否存在任何API来获取使用公共IP的Android设备的估计地理位置(纬度,经度)?
现在我可以使用以下方法获取IP地址:
private void getIPAddress(){
URL url;
BufferedReader bufferedReader=null;
InputStreamReader in=null;
HttpURLConnection urlConnection = null;
try {
url = new URL("http://ip2country.sourceforge.net/ip2c.php?format=JSON");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
in = new InputStreamReader(urlConnection.getInputStream());
bufferedReader = new BufferedReader(in);
String line;
StringBuffer buffer = new StringBuffer();
while((line = bufferedReader.readLine()) != null) {
buffer.append(line);
buffer.append('\r');
}
bufferedReader.close();
in.close();
JSONObject json_data = new JSONObject(buffer.toString());
ip = json_data.getString("ip");
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
bufferedReader.close();
in.close();
} catch (IOException e) {}
if(urlConnection != null) {
urlConnection.disconnect();
}
}
}
由于
答案 0 :(得分:0)
为了避免调用外部Web服务......另一种方法是查询LocationManager.NETWORK_PROVIDER
String provider = LocationManager.NETWORK_PROVIDER;
Location lastKnownLocation = locationManager.getLastKnownLocation(provider);
答案 1 :(得分:0)
您可以使用网络服务来请求您的IP位置。 This API以XML格式返回IP地址(国家,地区,城市,邮政编码,纬度,经度)和相关时区的位置。
答案 2 :(得分:0)
您可以使用此链接(http://freegeoip.net/json/)获取以下所有信息。提供的IP是您连接的外部IP地址。
JSON格式:(但您可以选择格式)
{
"ip": "xx.xxx.xxx.xx",
"country_code": "US",
"country_name": "United States",
"region_code": "XX",
"region_name": "XXX XXXX",
"city": "XXX XXXX",
"zip_code": "10XXX",
"time_zone": "America/New_York",
"latitude": 4X.XXXX,
"longitude": -7X.XXXX,
"metro_code": 501
}