利用谷歌接口实现基站定位

package lab.sodino.location;
002  
003 import java.io.BufferedReader;
004 import java.io.IOException;
005 import java.io.InputStream;
006 import java.io.InputStreamReader;
007 import java.io.UnsupportedEncodingException;
008 import org.apache.http.HttpEntity;
009 import org.apache.http.HttpResponse;
010 import org.apache.http.client.ClientProtocolException;
011 import org.apache.http.client.methods.HttpPost;
012 import org.apache.http.entity.StringEntity;
013 import org.apache.http.impl.client.DefaultHttpClient;
014 import org.json.JSONArray;
015 import org.json.JSONException;
016 import org.json.JSONObject;
017 import android.app.Activity;
018 import android.content.Context;
019 import android.os.Bundle;
020 import android.telephony.TelephonyManager;
021 import android.telephony.gsm.GsmCellLocation;
022 import android.view.View;
023 import android.widget.Button;
024 import android.widget.TextView;
025  
026 /**
027  * Google定位的实现.<br/>
028  * Geolocation的详细信息请参见:<br/>
029  * <a
030  * href="http://code.google.com/apis/gears/geolocation_network_protocol.html">
031  * http://code.google.com/apis/gears/geolocation_network_protocol.html</a>
032  */
033 public class LocationAct extends Activity {
034     private TextView txtInfo;
035     public void onCreate(Bundle savedInstanceState) {
036         super.onCreate(savedInstanceState);
037         setContentView(R.layout.main);
038         Button btn = (Button) findViewById(R.id.btnStart);
039         txtInfo = (TextView) findViewById(R.id.txtInfo);
040         btn.setOnClickListener(new Button.OnClickListener() {
041             public void onClick(View view) {
042                 getLocation();
043             }
044         });
045     }
046     private void getLocation() {
047         TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
048         GsmCellLocation gsmCell = (GsmCellLocation) tm.getCellLocation();
049         int cid = gsmCell.getCid();
050         int lac = gsmCell.getLac();
051         String netOperator = tm.getNetworkOperator();
052         int mcc = Integer.valueOf(netOperator.substring(03));
053         int mnc = Integer.valueOf(netOperator.substring(35));
054         JSONObject holder = new JSONObject();
055         JSONArray array = new JSONArray();
056         JSONObject data = new JSONObject();
057         try {
058             holder.put("version""1.1.0");
059             holder.put("host""maps.google.com");
060             holder.put("address_language""zh_CN");
061             holder.put("request_address"true);
062             holder.put("radio_type""gsm");
063             holder.put("carrier""HTC");
064             data.put("cell_id", cid);
065             data.put("location_area_code", lac);
066             data.put("mobile_countyr_code", mcc);
067             data.put("mobile_network_code", mnc);
068             array.put(data);
069             holder.put("cell_towers", array);
070         catch (JSONException e) {
071             e.printStackTrace();
072         }
073         DefaultHttpClient client = new DefaultHttpClient();
074         HttpPost httpPost = new HttpPost("http://www.google.com/loc/json");
075         StringEntity stringEntity = null;
076         try {
077             stringEntity = new StringEntity(holder.toString());
078         catch (UnsupportedEncodingException e) {
079             e.printStackTrace();
080         }
081         httpPost.setEntity(stringEntity);
082         HttpResponse httpResponse = null;
083         try {
084             httpResponse = client.execute(httpPost);
085         catch (ClientProtocolException e) {
086             e.printStackTrace();
087         catch (IOException e) {
088             e.printStackTrace();
089         }
090         HttpEntity httpEntity = httpResponse.getEntity();
091         InputStream is = null;
092         try {
093             is = httpEntity.getContent();
094         catch (IllegalStateException e) {
095             e.printStackTrace();
096         catch (IOException e) {
097             // TODO Auto-generated catch block
098             e.printStackTrace();
099         }
100         InputStreamReader isr = new InputStreamReader(is);
101         BufferedReader reader = new BufferedReader(isr);
102         StringBuffer stringBuffer = new StringBuffer();
103         try {
104             String result = "";
105             while ((result = reader.readLine()) != null) {
106                 stringBuffer.append(result);
107             }
108         catch (IOException e) {
109             e.printStackTrace();
110         }
111         System.out.println("[sodino]" + stringBuffer.toString());
112         txtInfo.setText(stringBuffer.toString());
113     }
114 }

[代码] 权限设定

1 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
2 <uses-permission android:name="android.permission.INTERNET"></uses-permission>

[图片] 1.jpg

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值