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

来源:互联网 发布:linux修改locale 重启 编辑:程序博客网 时间:2024/06/05 20:15
package lab.sodino.location;
002 
003import java.io.BufferedReader;
004import java.io.IOException;
005import java.io.InputStream;
006import java.io.InputStreamReader;
007import java.io.UnsupportedEncodingException;
008import org.apache.http.HttpEntity;
009import org.apache.http.HttpResponse;
010import org.apache.http.client.ClientProtocolException;
011import org.apache.http.client.methods.HttpPost;
012import org.apache.http.entity.StringEntity;
013import org.apache.http.impl.client.DefaultHttpClient;
014import org.json.JSONArray;
015import org.json.JSONException;
016import org.json.JSONObject;
017import android.app.Activity;
018import android.content.Context;
019import android.os.Bundle;
020import android.telephony.TelephonyManager;
021import android.telephony.gsm.GsmCellLocation;
022import android.view.View;
023import android.widget.Button;
024import 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 */
033public 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

原创粉丝点击