您的位置:首页 > 理论基础 > 计算机网络

Android无网络状态下使用GPS定位到省市县的一种简单实现方式

2017-11-25 12:24 756 查看

1、场景说明

先说一下使用场景,之前公司是做移动GIS的,有这么一条需求,在野外使用android设备,无SIM卡无WIFI的情况下,需要定位到当前的省市县。。。客户大于一切,没办法,搞吧。

2、实现思路

首先,看一下公司的数据支持,行政编码、省市县的名称、left、top、bottom、right的经纬度,也就是说给出了每个地区的矩形范围,总结一下就是这个样子的;



数据库的范围是经纬度,使用场景又在野外,所以就选择了GPS定位的方式,用获取到的经纬度在数据库里进行筛选,得出一个最小最适合的范围——县,然后用县的行政编码往上查依次获取到对应的市和省;下面是代码片段。

//这里GpsInfo.getInstance()里注册了广播接收器,用于获取经纬度;
double log = GpsInfo.getInstance(mContext).getLog();
longitude = new DecimalFormat("0.000000").format(log);
double lat = GpsInfo.getInstance(mContext).getLat();
latitude = new DecimalFormat("0.000000").format(lat);
//Gpsinfo类里log、lat默认-1,以此判断是否接收到GPS
if (!longitude.equals("-1.000000") && !latitude.equals("-1.000000")) {
//查询方式,下面会拆分一下
String findScope="select * from 'GB2008县市表' where MinX < '" + longitude+ "' and MaxX > '" + longitude + "' and MinY < '" + latitude + "' and MaxY > '" + latitude+ "'";
String sql ="select * from ("+findScope+") where ABS(MinX-'" + longitude + "')= (select min(ABS(MinX-'" + longitude+ "')) from ("+findScope+"))";
//获取到县的记录
List<LinkedHashMap<String,String>> query =cityHelper.query(sql);
if (query!=null && query.size() > 0) {
//查询到的县的名称
et_bt_area.setText(query.get(0).get("名称"));
//县行政编码
String code=query.get(0).get("行政区划代码2008");
//县编码截取前两位再补上0000即为对应的省编码
String privacecode = code.substring(0, 2) + "0000";
//县编码截取前四位再补上00即为对应市编码
String citycode = code.substring(0, 4) + "00";
//根据编码查到对应的省、市名称
List<LinkedHashMap<String, String>> privincequery = cityHelper.query(null, "where 行政区划代码2008='" + privacecode+ "'", null);
List<LinkedHashMap<String, String>> cityquery = cityHelper.query(null, "where 行政区划代码2008='" + citycode + "'",null);
et_bt_city.setText(cityquery.get(0).get("名称"));
et_bt_province.setText(privincequery.get(0).get("名称"));
}else {
//GPS信号差
}


思路很简单,主要说一下定位县的sql语句:

//获取到经纬度所在矩形范围内的所有记录
String findScope="select * from 'GB2008县市表' where MinX < '" + longitude+ "' and MaxX > '" + longitude + "' and MinY < '" + latitude + "' and MaxY > '" + latitude+ "'";
//根据经度距离最小找到县的范围,出于效率考虑,只考虑经度(X方向)
String sql ="select * from ("+findScope+")
where ABS(MinX-'" + longitude + "')=
(select min(ABS(MinX-'" + longitude+ "')) from ("+findScope+"))";


因为数据和数学水平有限,只能根据范围定位,误差肯定是有的,但用户使用过几个月反馈来看,只在县界边的地方会判断不准,后面与底层开发同事配合,使用图层文件用轮廓判断,这样就能保证了在边界处也能准确的定位到县;

据我了解还有一些实现方式,比如IOS的通过记录wifi位置,离线状态只要检测到wifi就能查到对应的位置,这种就很厉害了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android gis gps
相关文章推荐