android 获取GPS定位,
来源:互联网 发布:天猫魔盒直播软件 编辑:程序博客网 时间:2024/04/30 12:36
/**
* 得到位置信息
*/
private void getLocation() {
// 获取位置管理服务
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服务信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
updateToNewLocation(location);
try {
tv1.setText(getLocation(location));
} catch (Exception e) {
e.printStackTrace();
Log.e("", e.getMessage());
}
}
/**
* 根据精度和纬度定位到城市
*
* @param itude
* @return
* @throws Exception
*/
private String getLocation(Location itude) throws Exception {
String resultString = "";
/** 这里采用get方法,直接将参数加到URL上 */
String urlString = String.format(
"http://maps.google.cn/maps/geo?key=abcdefg&q=%s,%s",
itude.getLatitude(), itude.getLongitude());
Log.i("URL", urlString);
/** 新建HttpClient */
HttpClient client = new DefaultHttpClient();
/** 采用GET方法 */
HttpGet get = new HttpGet(urlString);
try {
/** 发起GET请求并获得返回数据 */
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
BufferedReader buffReader = new BufferedReader(
new InputStreamReader(entity.getContent()));
StringBuffer strBuff = new StringBuffer();
String result = null;
while ((result = buffReader.readLine()) != null) {
strBuff.append(result);
}
resultString = strBuff.toString();
/** 解析JSON数据,获得物理地址 */
if (resultString != null && resultString.length() > 0) {
JSONObject jsonobject = new JSONObject(resultString);
JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark")
.toString());
resultString = "";
for (int i = 0; i < jsonArray.length(); i++) {
resultString = jsonArray.getJSONObject(i).getString(
"address");
}
}
} catch (Exception e) {
throw new Exception("获取物理位置出现错误:" + e.getMessage());
} finally {
get.abort();
client = null;
}
return resultString;
* 得到位置信息
*/
private void getLocation() {
// 获取位置管理服务
LocationManager locationManager;
String serviceName = Context.LOCATION_SERVICE;
locationManager = (LocationManager) this.getSystemService(serviceName);
// 查找到服务信息
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE); // 高精度
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW); // 低功耗
String provider = locationManager.getBestProvider(criteria, true); // 获取GPS信息
Location location = locationManager.getLastKnownLocation(provider); // 通过GPS获取位置
updateToNewLocation(location);
try {
tv1.setText(getLocation(location));
} catch (Exception e) {
e.printStackTrace();
Log.e("", e.getMessage());
}
}
/**
* 根据精度和纬度定位到城市
*
* @param itude
* @return
* @throws Exception
*/
private String getLocation(Location itude) throws Exception {
String resultString = "";
/** 这里采用get方法,直接将参数加到URL上 */
String urlString = String.format(
"http://maps.google.cn/maps/geo?key=abcdefg&q=%s,%s",
itude.getLatitude(), itude.getLongitude());
Log.i("URL", urlString);
/** 新建HttpClient */
HttpClient client = new DefaultHttpClient();
/** 采用GET方法 */
HttpGet get = new HttpGet(urlString);
try {
/** 发起GET请求并获得返回数据 */
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
BufferedReader buffReader = new BufferedReader(
new InputStreamReader(entity.getContent()));
StringBuffer strBuff = new StringBuffer();
String result = null;
while ((result = buffReader.readLine()) != null) {
strBuff.append(result);
}
resultString = strBuff.toString();
/** 解析JSON数据,获得物理地址 */
if (resultString != null && resultString.length() > 0) {
JSONObject jsonobject = new JSONObject(resultString);
JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark")
.toString());
resultString = "";
for (int i = 0; i < jsonArray.length(); i++) {
resultString = jsonArray.getJSONObject(i).getString(
"address");
}
}
} catch (Exception e) {
throw new Exception("获取物理位置出现错误:" + e.getMessage());
} finally {
get.abort();
client = null;
}
return resultString;
}
补充一下:
在AndroidMenifest.xml里面需要加上
android.permission.INTERNET、android.permission.ACCESS_COARSE_LOCATION、android.permission.READ_PHONE_STATE权限,否则会出错。
- android 获取GPS定位,
- Android获取GPS定位数据
- Android GPS定位 获取经纬度
- Android GPS定位获取当前位置
- Android GPS定位 获取经纬度
- Android GPS定位 获取经纬度
- Android GPS/WIFI定位 获取经纬度
- android网络-GPS获取定位信息
- android 获取GPS定位(原文地址)
- Android APP - GPS定位并获取地理位置
- Android开发之GPS定位获取位置
- Android 获取GPS和网络定位信息
- Android GPS定位(获取经纬度)
- Android GPS定位(获取经纬度)
- xamarin android使用gps定位获取经纬度
- Android获取位置信息 - GPS定位 Network定位
- 通过SIM卡获取GPS,android基站定位原理
- Android获取当前位置(GPS和网络定位)
- JMS
- 原码与补码的转换
- 折半查找法的两种实现
- Hadoop实战-初级部分 之 开启云计算之旅 第一部分:我们为什么要学习云计算技术
- 当验证码也被破解,如何才能证明自己是人类?!
- android 获取GPS定位,
- Oracle常用sql操作总结
- TabHost中使用startActivityForResult无法接收返回值的解决方案
- java 与 c#
- C++各大有名库的介绍
- VC++基于LSP拦截数据封包
- UIActionSheet详解
- Android中使用HTTP服务
- ASP里,cookies如何设置有效期一小时、一天、一个月