微信实现定位城市并获取城市编码
时间:2017-09-15 12:26:10
收藏:0
阅读:1778
最近在做一个项目是将用户的当前所在市县定位出来并展示在手机端页面,同时还要获取到该市县的城市编码从而进行数据过滤,这里重点讲定位城市及获取城市编码
前端页面代码:
首先引用腾讯地图的一个js
<script type="text/javascript" src="https://3gimg.qq.com/lightmap/components/geolocation/geolocation.min.js" ></script>
同时在页面加载时调用下面js:
$(function() { getLocation(); })
function getLocation() { var geolocation = new qq.maps.Geolocation("OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77", "myapp"); if (geolocation) { var options = {timeout: 8000}; geolocation.getLocation(onSuccess, onError, options); } else { alert("定位尚未加载"); } } //成功时 function onSuccess(position) { //经度 var longitude = position.lng; //纬度 var latitude = position.lat; var param = { longitude : longitude, latitude : latitude }; $.ajax({ type : "post", url : "getCode.jspx", data : param, dataType : "json", success : function(data) {
alert(data.admCode);
alert(data.adm);
}
})
}
//失败时
function onError(error) {
switch (error.code) {
case 1:
alert("位置服务被拒绝");
break;
case 2:
alert("暂时获取不到位置信息");
break;
case 3:
alert("获取信息超时");
break;
case 4:
alert("未知错误");
break;
}
}
这里可以获取用户当前所在市县的经纬度,然后经过经纬度再去后台调用接口获取城市编码,我们看后台代码:
@RequestMapping("/getCode.jspx") public String getCode(HttpServletRequest request,HttpServletResponse response, ModelMap model) { String longitude = request.getParameter("longitude"); String latitude = request.getParameter("latitude"); String add = getAdd(longitude, latitude); JSONObject jsonObject = JSONObject.fromObject(add); JSONArray jsonArray = JSONArray.fromObject(jsonObject.getString("addrList")); JSONObject j_2 = JSONObject.fromObject(jsonArray.get(0)); String allAdd = j_2.getString("admName"); String arr[] = allAdd.split(","); System.out.println("省:" + arr[0] + "\n市:" + arr[1] + "\n区:" + arr[2]); Map<String,String> map = new HashMap<String,String>(); if(arr[2] != null && !"".equals(arr[2])){ map.put("adm", arr[2]); }else if(arr[1] != null && !"".equals(arr[1])){ map.put("adm", arr[1]); }else{ map.put("adm", arr[0]); } String admCode = j_2.getString("admCode"); System.out.println(admCode); map.put("admCode", admCode); String json = JSONObject.fromObject(map).toString(); return json } public String getAdd(String log, String lat) { // lat 小 log 大 // 参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项) String urlString = "http://gc.ditu.aliyun.com/regeocoding?l=" + lat + "," + log + "&type=010"; String res = ""; try { URL url = new URL(urlString); java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("POST"); java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(),"UTF-8")); String line; while ((line = in.readLine()) != null) { res += line + "\n"; } in.close(); } catch (Exception e) { System.out.println("error in wapaction,and e is " + e.getMessage()); } System.out.println(res); return res; }
控制台打印输出:
{"queryLocation":[34.797817,113.601715],"addrList":[{"type":"poi","status":1,"name":"郑州热电厂家属院(南门)","id":"ANB01730IC2W","admCode":"410102","admName":"河南省,郑州市,中原区,","addr":"","nearestPoint":[113.60046,34.79849],"distance":142.299}]}
省:河南省
市:郑州市
区:中原区
410102
将得到的数据封装成json返回前台即可
评论(0)