80,351
社区成员
发帖
与我相关
我的任务
分享
private String GetLocationString()
{
LocationManager locationmanager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
List<String>lp = locationmanager.getAllProviders();
for(String _param : lp)
Log.i("可用位置",_param);
criteria.setCostAllowed(false);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String providername = locationmanager.getBestProvider(criteria, true);
if(providername != null)
{
Log.i("所用定位",providername);
Location location = locationmanager.getLastKnownLocation(providername);
if(location == null) return "error occured while getting location";
double latitude = location.getLatitude();
double longitude = location.getLongitude();
return ""+latitude + " " + longitude;
}
return "error";
}
public static String GetAddr(String latitude, String longitude) {
String addr = "error Loading location Info";
/*
* 也可以是http://maps.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s,不过解析出来的是英文地址
* 密钥可以随便写一个key=abc
* output=csv,也可以是xml或json,不过使用csv返回的数据最简洁方便解析
*/
String url = String.format(
"maps.google.cn/maps/geo?output=csv&key=abcdef&q=%s,%s",
latitude, longitude);
URL myURL = null;
URLConnection httpsConn = null;
try {
myURL = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
try {
httpsConn = (URLConnection) myURL.openConnection();
if (httpsConn != null) {
InputStreamReader insr = new InputStreamReader(
httpsConn.getInputStream(), "UTF-8");
BufferedReader br = new BufferedReader(insr);
String data = null;
if ((data = br.readLine()) != null) {
String[] retList = data.split(",");
if (retList.length > 2 && ("200".equals(retList[0]))) {
addr = retList[2];
} else {
addr = "error Loading location";
}
}
insr.close();
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
return addr;
}
}