基站定位获取位置坐标
2012-01-08 20:38
204 查看
package cn.MyAndroid.Test; import java.io.BufferedReader; import java.io.InputStreamReader; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONObject; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.telephony.TelephonyManager; import android.telephony.gsm.GsmCellLocation; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MyLocation extends Activity { TextView mTextView; Button mButton; TelephonyManager tm; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView = (TextView) findViewById(R.id.TextView01); mButton = (Button) findViewById(R.id.Button01); tm = (TelephonyManager) this .getSystemService(Context.TELEPHONY_SERVICE); mButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub GsmCellLocation gcl = (GsmCellLocation) tm.getCellLocation(); int cid = gcl.getCid(); int lac = gcl.getLac(); int mcc = Integer.valueOf(tm.getNetworkOperator().substring(0, 3)); int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3, 5)); try { //组装JSON查询字符串 JSONObject holder = new JSONObject(); holder.put("version", "1.1.0"); holder.put("host", "maps.google.com"); // holder.put("address_language", "zh_CN"); holder.put("request_address", true); JSONArray array = new JSONArray(); JSONObject data = new JSONObject(); data.put("cell_id", cid); // 25070 data.put("location_area_code", lac);// 4474 data.put("mobile_country_code", mcc);// 460 data.put("mobile_network_code", mnc);// 0 array.put(data); holder.put("cell_towers", array); // 创建连接,发送请求并接受回应 DefaultHttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost( "http://www.google.com/loc/json"); StringEntity se = new StringEntity(holder.toString()); post.setEntity(se); HttpResponse resp = client.execute(post); HttpEntity entity = resp.getEntity(); BufferedReader br = new BufferedReader( new InputStreamReader(entity.getContent())); StringBuffer sb = new StringBuffer(); String result = br.readLine(); while (result != null) { sb.append(result); result = br.readLine(); } mTextView.setText(sb.toString()); } catch (Exception e) { // TODO: handle exception } } }); } }注意要给Activity加入以下权限:<uses-permission android:name="android.permission.PERMISSION_NAME"
/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
关于基站查询协议的话,请参考这个链接:http://code.google.com/intl/zh-CN/apis/gears/geolocation_network_protocol.html
相关文章推荐
- android基站定位程序获取地理位置
- 使用天地图进行定位,获取坐标位置及省市等信息
- android基站定位程序获取地理位置
- web app实现基站定位获取精确地理位置的一种简单方法
- iOS开发——定位获取经纬度坐标,并反编码获得地理位置信息(具体地址,国家、省、市等)
- 百度地图实时获取当前位置坐标并实时定位
- gprs 获取基站信息,根据基站信息定位地理位置(基站免费查询接口)
- iOS GPS定位CLLocationManager获取当前位置坐标 示例代码
- js获取控件坐标以及位置定位
- 通过gps,wifi,基站定位获取当前位置
- android基站定位程序获取地理位置
- 位置与地图:定位获取设备的位置
- Android 获取控件的坐标位置
- Android Map Api 使用和开发(2) 定位我的位置、地图弹出泡泡、通过经纬度获取地址
- Android Service获取当前位置(GPS+基站)
- Android获取点击屏幕的位置坐标
- js与jquery中获取当前鼠标的x、y坐标位置的代码
- iOS定位操作,获取当前位置,计算两点之间距离
- IOS 地图定位,标记位置,获取经纬度,MKMapView的使用
- Android获取基站坐标代码