pedily 2011-11-29
一般定位都是通过gps定位器,但这个过程比较慢.这里可以通过基站方式辅助定位
代码:
private TelephonyManager TMmanager; private void helpGetLocion() throws Exception { //获取服务 TMmanager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); GsmCellLocation gcl = (GsmCellLocation) TMmanager.getCellLocation(); //获取位置信息 int cid = gcl.getCid(); int lac = gcl.getLac(); int mcc = Integer.valueOf(TMmanager.getNetworkOperator() .substring(0, 3)); int mnc = Integer.valueOf(TMmanager.getNetworkOperator() .substring(3, 5)); // String[] getNumber = ""; // getNumber+={"cid:"+cid+"\n"}; //create json JSONObject jobject = new JSONObject(); JSONArray jarray = new JSONArray(); JSONObject jData = new JSONObject(); jobject.put("version", "1.1.0"); jobject.put("host", "maps.google.com"); jobject.put("request_address", true); Log.d("tracy", "mcc=" + mcc); if (mcc == 460) { jobject.put("addess_language", "zh_CN"); } else { jobject.put("addess_language", "en_US"); } jData.put("cell_id", cid); jData.put("location_area_code", lac); jData.put("mobile_country_code", mcc); jData.put("mobile_network_code", mnc); jarray.put(jData); jobject.put("cell_towers", jarray); //建立连接到服务器 DefaultHttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost("http://www.google.com/loc/json"); StringEntity se = new StringEntity(jobject.toString()); post.setEntity(se); HttpResponse resp = client.execute(post); BufferedReader br = null; if (resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { Log.d("tracy", "HttpStatus.SC_OK"); br = new BufferedReader(new InputStreamReader(resp.getEntity() .getContent())); } StringBuffer sb = new StringBuffer(); String result = br.readLine(); while (result != null) { sb.append(result); result = br.readLine(); } //返回的数据,这个是json格式的数据,如果要解析可以网上搜下json object mTextView.setText(sb.toString()); }