单车定位案例(GeoHash,HTTPClient)

编程入门 行业动态 更新时间:2024-10-24 11:15:34

<a href=https://www.elefans.com/category/jswz/34/1768999.html style=单车定位案例(GeoHash,HTTPClient)"/>

单车定位案例(GeoHash,HTTPClient)

案例来自www.51doit.com多易教育–行哥的小案例练习

数据:

单车信息数据,北京市的poi位置信息.

需求需求和流程分析:
  • 使用GeoHash算法将poi中的数据转换成对应的geoHash值对应地理位置,
    获取所有的单车数据的经纬度对应的地理位置
  • 先从本地的地理仓库位置中获取数据,找到匹配的数据返回,如果没有数据,去官网地图获取数据,并将数据存储在本地的地理位置库中


1 . poi数据清洗写到本地仓库

public static void readAndWriteDataToLocalFromPoi() throws Exception {//解决乱码,指定gbk读取BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("d:/data/data(1)/bj-poi-1.csv"), "gbk"));BufferedWriter bw = new BufferedWriter(new FileWriter("d:/data/bi-poi-bak"));String line;/** 左所屯中区294号楼,0 116.161015,2 40.532779,3 北京市延庆区左所屯中区295号楼,4 北京市,5 延庆区,7 小后街 */while ((line = br.readLine()) != null) {String[] split = line.split(",");String name = split[0];String locationx = split[2];String locationy = split[3];String address = split[4];String province = split[5];String district = split[7];String street = split[8];// 丑儿岭,116.066357,39.969067,北京市门头沟区妙峰山镇丑儿岭,北京市,门头沟区,陇驾庄前街String str = name.concat(",").concat(locationx).concat(",").concat(locationy).concat(",").concat(address).concat(",").concat(province).concat(",").concat(district).concat(",").concat(street);bw.write(str);bw.newLine();}bw.flush();bw.close();br.close();}

2. 将本地仓库文件读取到Map中,以便查找数据

//使用GeoHash类,转化经纬度得到字符串,这里12是字符串的长度
String str = GeoHash.geoHashStringWithCharacterPrecision(locationy, locationx, 12);
//将GeoHash得到的字符串当key,value就是每一行的bean
map.put(str, addressBean);

AddressBean

    private String name;//具体地址private double locationx;//经度private double locationy;//纬度private String addr;//大致地址private String province;//省private String district;//区private String street;//街道

END. 万事具备,开始分析bike数据

  • IO流读入,使用JSON得到经纬度,通过GeoHash得到12位字符串
//得到经纬度
BikeBean bean = JSON.parseObject(line, BikeBean.class);
// 得到经纬度的字符串
String str = GeoHash.geoHashStringWithCharacterPrecision(latitude, longitude, 12);

BikeBean

private String longitude;
private String latitude;
  • 拿到经纬度字符串就能在Map中查找,此时 注意 Map放在static代码块中,否则会重复读入Map使程序速度变得非常慢
    //在本地找if (map.containsKey(str)) {AddressBean addressBean = map.get(str);System.out.println(addressBean);} else {//在网上找Result res = MyUtils.getPositionByHttpClient(latitude,longitude);...//将数据按本地仓库的格式拼接,写到本地bw.write(data);//添加到map中,map是static的只能读一次,更新map//直接添加,走到这步,表示map中不会包含经纬度的字符串map.put(str, addressBean);

使用HTTPClient向网络请求数据

public static Result getPositionByHttpClient(double latitude, double longitude) throws Exception {HttpClient client = new HttpClient();String url = "=" + latitude + "," + longitude + "&type=010";GetMethod m = new GetMethod(url);client.executeMethod(m);//得到JSON数据String string = m.getResponseBodyAsString();//转成javabeanResult res = JSON.parseObject(string, Result.class);return res;}

Result : 根据JSON数据的格式创建

public class Result {/*** {"queryLocation": [39.776652, 115.444855],"addrList": [{"type": "poi","status": 1,"name": "鱼斗泉村","id": "ANB000A7ZXRH","admCode": "110111","admName": "北京市,北京市,房山区,","addr": "","nearestPoint": [115.44478, 39.77674],"distance": 11.402}]
}*/private double[] queryLocation;private List<Address> addrList;

Address : 只需要name和admName

public class Address {/**"name": "鱼斗泉村","admName": "北京市,北京市,房山区,",*/private String name;private String admName;

更多推荐

单车定位案例(GeoHash,HTTPClient)

本文发布于:2023-07-28 21:05:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1318648.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单车   案例   HTTPClient   GeoHash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!