Android获取基站坐标代码

002
003import java.io.BufferedReader;
004import java.io.IOException;
005import java.io.InputStreamReader;
006
007import org.apache.http.HttpEntity;
008import org.apache.http.HttpResponse;
009import org.apache.http.client.methods.HttpPost;
010import org.apache.http.entity.StringEntity;
011import org.apache.http.impl.client.DefaultHttpClient;
012import org.json.JSONArray;
013import org.json.JSONObject;
014
015import android.app.Activity;
016import android.content.Context;
017import android.location.Location;
018import android.location.LocationManager;
019import android.os.Bundle;
020import android.telephony.TelephonyManager;
021import android.telephony.gsm.GsmCellLocation;
022import android.util.Log;
023import android.widget.Toast;
024
025public class TestStationLocationActivity extends Activity {
026private static final String TAG = "TestStationLocationActivity";
027
028/** Called when the activity is first created. */
029@Override
030public void onCreate(Bundle savedInstanceState) {
031super.onCreate(savedInstanceState);
032setContentView(R.layout.main);
033Location location = getportLocation();
034location.getLongitude();
035Toast.makeText(this, location.getLatitude()+location.getLatitude()+"", 100).show();
036
037}
038
039private Location getportLocation() {
040Location loc = null ;
041TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
042BufferedReader br = null;
043try
044{
045GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation();
046if (null == gcl)
047{
048return null;
049}
050int cid = gcl.getCid();
051int lac = gcl.getLac();
052int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0,3));
053int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3,5));
054JSONObject holder = new JSONObject();
055holder.put("version", "1.1.0");
056holder.put("host", "maps.google.com");
057holder.put("request_address", true);
058
059JSONArray array = new JSONArray();
060JSONObject data = new JSONObject();
061
062data.put("cell_id", cid);
063data.put("location_area_code", lac);
064data.put("mobile_country_code", mcc);
065data.put("mobile_network_code", mnc);
066array.put(data);
067holder.put("cell_towers", array);
068DefaultHttpClient client = new DefaultHttpClient();
069HttpPost post = new HttpPost("http://www.google.com/loc/json");
070StringEntity se = new StringEntity(holder.toString());
071post.setEntity(se);
072HttpResponse resp = client.execute(post);
073if (resp.getStatusLine().getStatusCode() == 200)
074{
075HttpEntity entity = resp.getEntity();
076br = new BufferedReader(new InputStreamReader(entity.getContent()));
077StringBuffer sb = new StringBuffer();
078String result = br.readLine();
079while (result != null)
080{
081sb.append(result);
082result = br.readLine();
083}
084
085JSONObject data_ = new JSONObject(sb.toString());
086data_ = (JSONObject) data_.get("location");
087loc = new Location(LocationManager.NETWORK_PROVIDER);
088loc.setLatitude((Double) data_.get("latitude"));
089loc.setLongitude((Double) data_.get("longitude"));
090Log.i(TAG, "latitude : " + loc.getLatitude() + " longitude : " + loc.getLongitude());
091return loc;
092}
093return null;
094
095}
096
097catch (Exception e)
098{
099android.util.Log.e(TAG, "network get the latitude and longitude ocurr Exception error", e);
100}
101finally
102{
103if (null != br)
104{
105try
106{
107br.close();
108}
109catch (IOException e)
110{
111android.util.Log.e(TAG, "network get the latitude and longitude when closed BufferedReader ocurr IOException error", e);
112}
113}
114}
115return loc;
116}
117
118}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值