android 基於GeolocationAPI的基站定位

來源:互聯網
上載者:User

api 地址為http://code.google.com/p/gears/wiki/GeolocationAPI

發送的格式:

                              {
 "location": {
   "latitude": 51.0,
   "longitude": -0.1,
   "altitude": 30.1,
   "accuracy": 1200.1,
   "altitude_accuracy": 10.1,
   "address": {
     "street_number": "100",
     "street": "Amphibian Walkway",
     "postal_code": "94043",
     "city": "Mountain View",
     "county": "Mountain View County",
     "region": "California",
     "country": "United States of America",
     "country_code": "US"
   }
 }
}

返回的格式:              {
 "location": {
   "latitude": 51.0,
   "longitude": -0.1,
   "altitude": 30.1,
   "accuracy": 1200.1,
   "altitude_accuracy": 10.1,
   "address": {
     "street_number": "100",
     "street": "Amphibian Walkway",
     "postal_code": "94043",
     "city": "Mountain View",
     "county": "Mountain View County",
     "region": "California",
     "country": "United States of America",
     "country_code": "US"
   }
 }
}

  得到LAC 和CellId後,其它的就是json解析了;

</p><p>import java.io.BufferedReader;<br />import java.io.InputStream;<br />import java.io.InputStreamReader;<br />import java.net.URL;<br />import java.util.Date;</p><p>import org.apache.http.HttpEntity;<br />import org.apache.http.HttpResponse;<br />import org.apache.http.client.methods.HttpPost;<br />import org.apache.http.entity.StringEntity;<br />import org.apache.http.impl.client.DefaultHttpClient;<br />import org.json.JSONArray;<br />import org.json.JSONObject;</p><p>import android.app.Activity;<br />import android.content.Context;<br />import android.os.Bundle;<br />import android.telephony.TelephonyManager;<br />import android.telephony.gsm.GsmCellLocation;<br />import android.view.View;<br />import android.widget.Button;<br />import android.widget.TextView;</p><p>public class LocationStation extends Activity {<br />TextView mTextView;<br />Button mButton;<br />TelephonyManager tm;</p><p>/** Called when the activity is first created. */<br />@Override<br />public void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.main);</p><p>mTextView = (TextView) findViewById(R.id.textView);<br />mButton = (Button) findViewById(R.id.Button);<br />tm = (TelephonyManager) this<br />.getSystemService(Context.TELEPHONY_SERVICE);</p><p>mButton.setOnClickListener(new Button.OnClickListener() {</p><p>@Override<br />public void onClick(View v) {<br />// TODO Auto-generated method stub<br />GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();<br />int cid = gcl.getCid();<br />int lac = gcl.getLac();<br />System.out.println("operator"+tm.getNetworkOperator()); //中國移動43600<br />int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,<br />3));<br />int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,<br />5));<br />/*<br />* 發送的格式:{<br />"version": "1.1.0" ,<br />"host": "maps.google.com",<br />"access_token": "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe",<br />"home_mobile_country_code": 460,<br />"home_mobile_network_code":0,<br />"address_language": "zh_CN",<br />"radio_type": "gsm",<br />"request_address": true ,<br />"cell_towers":[<br />{<br />"cell_id":36526,<br />"location_area_code":14556,<br />"mobile_country_code":460,<br />"mobile_network_code":0,<br />"timing_advance":5555<br />}<br />]<br />}<br /> */<br />try {<br />// 組裝JSON查詢字串<br />JSONObject holder = new JSONObject();<br />holder.put("version", "1.1.0");<br />holder.put("host", "maps.google.com");<br />holder.put("address_language", "zh_CN");<br />holder.put("request_address", true);</p><p>JSONArray array = new JSONArray();<br />JSONObject data = new JSONObject();<br />data.put("cell_id", cid); // 25070<br />data.put("location_area_code", lac);// 4474<br />data.put("mobile_country_code", mcc);// 460<br />data.put("mobile_network_code", mnc);// 0<br />array.put(data);<br />holder.put("cell_towers", array);</p><p>// 建立串連,發送請求並接受回應<br />DefaultHttpClient client = new DefaultHttpClient();</p><p>HttpPost post = new HttpPost(<br />"http://www.google.com/loc/json");</p><p>StringEntity se = new StringEntity(holder.toString());</p><p>post.setEntity(se);<br />HttpResponse resp = client.execute(post);</p><p>HttpEntity entity = resp.getEntity();</p><p>BufferedReader br = new BufferedReader(<br />new InputStreamReader(entity.getContent()));<br />StringBuffer sb = new StringBuffer();<br />String result = br.readLine();</p><p>while (result != null) {</p><p>sb.append(result);<br />result = br.readLine();<br />}<br />/*<br /> * 返回格式: {<br /> "location": {<br /> "latitude": 51.0,<br /> "longitude": -0.1,<br /> "altitude": 30.1,<br /> "accuracy": 1200.1,<br /> "altitude_accuracy": 10.1,<br /> "address": {<br /> "street_number": "100",<br /> "street": "Amphibian Walkway",<br /> "postal_code": "94043",<br /> "city": "Mountain View",<br /> "county": "Mountain View County",<br /> "region": "California",<br /> "country": "United States of America",<br /> "country_code": "US"<br /> }<br /> }<br />}<br /> */<br />JSONObject jsonObject = new JSONObject(sb.toString());</p><p>JSONObject jsonObject1 = new JSONObject(jsonObject<br />.getString("location"));</p><p>getAddress(jsonObject1.getString("latitude"), jsonObject1<br />.getString("longitude"));</p><p>//mTextView.setText(sb.toString());<br />} catch (Exception e) {<br />// TODO: handle exception<br />}</p><p>}</p><p>});<br />}</p><p>void getAddress(String lat, String lag) {<br />try {</p><p>URL url = new URL("http://maps.google.cn/maps/geo?key=abcdefg&q="<br />+ lat + "," + lag);<br />InputStream inputStream = url.openConnection().getInputStream();<br />InputStreamReader inputReader = new InputStreamReader(inputStream,<br />"utf-8");<br />BufferedReader bufReader = new BufferedReader(inputReader);</p><p>String line = "", lines = "";</p><p>while ((line = bufReader.readLine()) != null) {<br />lines += line;<br />}<br />if (!lines.equals("")) {</p><p>JSONObject jsonobject = new JSONObject(lines);<br />JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark")<br />.toString());<br />for (int i = 0; i < jsonArray.length(); i++) {</p><p>mTextView.setText( jsonArray.getJSONObject(i).getString("address"));</p><p>}</p><p>}</p><p>} catch (Exception e) {<br />;<br />}</p><p>}<br />}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.