有问题欢迎交流,QQ:464899731
当已经获取了手机所在位置的基站信息时,怎么知道手机的具体位置?三种方法:
1:查询基站的信息表;
2:通过安特网:http://www.anttna.com/ 目前已经关闭;
3:http://www.lbsgps.org/?p=86,该网站通过调用google的json接口获取地理位置信息;
在这里我们介绍第三种方法;
有一个网站写的很好,http://618119.com/tag/json,但是完全照搬代码是不行的,不过还是要感谢这位朋友的解释;
google 提供了公开的接口,通过,这个接口,根据gps或基站信息或wifi热点信息来获取当前位置的地理信息.
gps信息是经纬度,基站信息是基站的cellid等信息,wifi热点信息是wifi的mac地址是热点名字,信号强度等。
因此可以通过java程序获取相应信息,在LBS项目中可以用上:
api接口定义描述在:
http://code.google.com/p/gears/wiki/GeolocationAPI
现在附上代码:
这段代码是生成google 的 json 接口所需的格式:
import java.net.Proxy; import java.net.URL; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; public static String map2Json(Map<String, Object> map) { if (map.isEmpty()) return "{}"; StringBuilder sb = new StringBuilder(map.size() << 4); sb.append('{'); Set<String> keys = map.keySet(); for (String key:keys) { Object value = map.get(key); sb.append('\"'); sb.append(key); sb.append('\"'); sb.append(':'); sb.append(toJson(value)); sb.append(','); } // 将最后的 ',' 变为 '}': sb.setCharAt(sb.length()-1, '}'); return sb.toString(); } private static String toJson(Object o) { if (o==null) return "null"; if (o instanceof String) return string2Json((String)o); if (o instanceof Boolean) return boolean2Json((Boolean)o); if (o instanceof Number) return number2Json((Number)o); if (o instanceof Map) return map2Json((Map<String, Object>)o); if (o instanceof ArrayList) return array2Json((ArrayList)o); throw new RuntimeException("Unsupported type: " + o.getClass().getName()); } private static String string2Json(String s) { StringBuilder sb = new StringBuilder(s.length()+20); sb.append('\"'); for (int i=0; i<s.length(); i++) { char c = s.charAt(i); switch (c) { case '\"': sb.append("\\\""); break; case '\\': sb.append("\\\\"); break; case '/': sb.append("\\/"); break; case '\b': sb.append("\\b"); break; case '\f': sb.append("\\f"); break; case '\n': sb.append("\\n"); break; case '\r': sb.append("\\r"); break; case '\t': sb.append("\\t"); break; default: sb.append(c); } } sb.append('\"'); return sb.toString(); } private static String number2Json(Number number) { return number.toString(); } private static String boolean2Json(Boolean bool) { return bool.toString(); } private static String array2Json(ArrayList array) { if (array.size()==0) return "[]"; StringBuilder sb = new StringBuilder(array.size() << 4); sb.append('['); int len=array.size(); int i; for (i=0;i<len;i++){ sb.append(toJson(array.get(i))); sb.append(','); } // for (Map<String, Object> o : List) { // sb.append(toJson(o)); // sb.append(','); // } // 将最后添加的 ',' 变为 ']': sb.setCharAt(sb.length()-1, ']'); return sb.toString(); }
public static String getJSON(int lac,int cid){ String jsonString=""; String url = "http://www.google.com.hk/loc/json"; Map<String, Object> jMap = new HashMap<String, Object>(); // Map<String, Object> jMapArray = new HashMap<String, Object>(); // Map<String, Object> jMapObject = new HashMap<String, Object>(); Map<String, Object> jMapcell = new HashMap<String, Object>(); Map<String, Object> jMapwifi = new HashMap<String, Object>(); List jMapcells = new ArrayList(); List jMapwifis = new ArrayList(); jMap.put("version", "1.1.0"); jMap.put("host", "maps.google.com.hk"); //加上.hk
jMap.put("home_mobile_country_code", 460);// 国家代码 jMap.put("home_mobile_network_code", 0);// 移动运营商代码 jMap.put("radio_type", "gsm"); jMap.put("carrier", "quxianglin"); jMap.put("request_address", true); jMap.put("address_language", "zh_CN"); jMapcell.put("mobile_country_code", 460);// 国家代码,mcc jMapcell.put("mobile_network_code", 0);// 移动运营商代码,mnc jMapcell.put("location_area_code", lac);// 位置区域代码,lac jMapcell.put("cell_id", cid);// 移动基站id jMapcells.add(jMapcell); jMap.put("cell_towers", jMapcells); jMap.put("wifi_towers", jMapwifis); jMapwifi.put("mac_address", "00-00-00-00-0-00");// jMapwifi.put("ssid", "TPLINK000000");// 无线路由器的名字 jMapwifi.put("signal_strength", 8);// 信号强度 jMapwifi.put("age", 0); jMapwifis.add(jMapwifi); // System.out.println(jMap.toString()); jsonString=map2Json(jMap); try { jsonString=downloadUrlbyPOST(url,jsonString,null,"UTF-8"); } catch (Exception e) { // TODO: handle exception } return jsonString; } private static String downloadUrlbyPOST(String urlStr, String query,String referer, String encoding) throws Exception { String line = ""; StringBuilder sb=new StringBuilder(); HttpURLConnection httpConn=null; try { URL url=new URL(urlStr); // System.out.println(urlStr + "?" + query); Proxy proxy=new Proxy(Proxy.Type.HTTP,new InetSocketAddress("proxy.google.com.hk", 80)); proxy=Proxy.NO_PROXY; httpConn=(HttpURLConnection) url.openConnection(proxy); httpConn.setDoInput(true); httpConn.setDoOutput(true); httpConn.setRequestMethod("POST"); if (referer != null) { httpConn.setRequestProperty("Referer", referer); } httpConn.setConnectTimeout(5000); // httpConn.getOutputStream().write( // java.net.URLEncoder.encode(query, "UTF-8").getBytes()); httpConn.getOutputStream().write(query.getBytes()); httpConn.getOutputStream().flush(); httpConn.getOutputStream().close(); BufferedReader in = null; if (httpConn.getResponseCode() != 200) { System.err.println("error:" + httpConn.getResponseMessage()); in = new BufferedReader(new InputStreamReader(httpConn.getErrorStream(), "UTF-8")); } else { in = new BufferedReader(new InputStreamReader(httpConn.getInputStream(), "UTF-8")); } while ((line = in.readLine()) != null) { sb.append(line).append('\n'); } // 关闭连接 httpConn.disconnect(); return sb.toString(); } catch (Exception e) { // TODO: handle exception // 关闭连接 httpConn.disconnect(); System.out.println(e.getMessage()); throw e; } }
程序返回的结果:{"location":{"latitude":40.1012203,"longitude":113.4588556,"address":{"country":"中国","country_code":"CN","region":"山西省","city":"大同市","street":"301省道"},"accuracy":2685.0},"access_token":"2:TgElaNhAt0yjOShR:2fLmIyqIo0pZj1Gw"}
完毕!
java调用时调用getJSON函数即可。