Android 基站定位源代码(有问题)

来源:互联网 发布:大数据应用平台 编辑:程序博客网 时间:2024/06/11 14:40

2012-12-13经过测试,貌似不能通过基站号获得经纬度了。

http://www.google.com/loc/json

网站出现404不能用了,暂时还没有想到怎样通过基站号获得经纬度

开始收费了:

https://developers.google.com/maps/documentation/business/geolocation/




下面是我查的的资料:

经过几天的调研以及测试,终于解决了联通2G、移动2G、电信3G的基站定位代码。团队里面只有这些机器的制式了。下面就由我来做一个详细的讲解吧。

1 相关技术内容
Google Android Api里面的TelephonyManager的管理。
联通、移动、电信不同制式在获取基站位置的代码区别。
通过基站的基本信息,通过Google Gears获取对应的GPS经纬度。
通过Google Map API根据GPS经纬度获取当前位置。

2 目前存在的几个问题
由于得到的GPS经纬度在Google Map上面显示需要偏移,这块暂时没有进行处理。
没有使用PhoneStateListener来对状态实时进行更新。
没有使用线程异步获取数据
没有使用服务的方式来实时获取数据
所以如果是商业使用的话,还需进一步修改。
3 当然本部分代码已经移植到我们的家庭卫士的项目中了,2提到的问题全部解决了。
下面我针对第一部分的四大内容进行代码注解。
1 Google Android Api里面的TelephonyManager的管理。

TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

通过这个方式就可以得到TelephonyManager接口。这个接口的源代码可以通过设置在项目里面查看,这里不具体附上了。得到TelephonyManager后,由于针对不同的运营商,代码有所不同,所以需要判断getNetworkType()在源代码里面有如下的类型定义    





/** Network type is unknown */    public static final int NETWORK_TYPE_UNKNOWN = 0;    /** Current network is GPRS */    public static final int NETWORK_TYPE_GPRS = 1;    /** Current network is EDGE */    public static final int NETWORK_TYPE_EDGE = 2;    /** Current network is UMTS */    public static final int NETWORK_TYPE_UMTS = 3;    /** Current network is CDMA: Either IS95A or IS95B*/    public static final int NETWORK_TYPE_CDMA = 4;    /** Current network is EVDO revision 0*/    public static final int NETWORK_TYPE_EVDO_0 = 5;    /** Current network is EVDO revision A*/    public static final int NETWORK_TYPE_EVDO_A = 6;    /** Current network is 1xRTT*/    public static final int NETWORK_TYPE_1xRTT = 7;    /** Current network is HSDPA */    public static final int NETWORK_TYPE_HSDPA = 8;    /** Current network is HSUPA */    public static final int NETWORK_TYPE_HSUPA = 9;    /** Current network is HSPA */    public static final int NETWORK_TYPE_HSPA = 10;



2 联通、移动、电信不同制式在获取基站位置的代码区别。

这部分是我实际测试出来的,经过无数次的拆机,放卡,才实现了不同制式的完美实现。



TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);int type = tm.getNetworkType();//中国电信为CTC//NETWORK_TYPE_EVDO_A是中国电信3G的getNetworkType//NETWORK_TYPE_CDMA电信2G是CDMAif (type == TelephonyManager.NETWORK_TYPE_EVDO_A || type == TelephonyManager.NETWORK_TYPE_CDMA || type ==TelephonyManager.NETWORK_TYPE_1xRTT){}//移动2G卡 + CMCC + 2//type = NETWORK_TYPE_EDGEelse if(type == TelephonyManager.NETWORK_TYPE_EDGE){}//联通的2G经过测试 China Unicom   1 NETWORK_TYPE_GPRSelse if(type == TelephonyManager.NETWORK_TYPE_GPRS){  }else{tv.setText("Current Not Support This Type.");}

完成代码:
package cn.LocationStation;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.net.URL;import java.util.Date;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 LocationStation extends Activity {TextView mTextView;Button mButton;TelephonyManager tm;/*<uses-permission android:name="android.permission.INTERNET"></uses-permission><uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission><uses-permission android:name="android.permission.PERMISSION_NAME"></uses-permission>/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mTextView = (TextView) findViewById(R.id.textView001);mButton = (Button) findViewById(R.id.Button001);tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);mButton.setOnClickListener(new Button.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubGsmCellLocation 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); // 25070data.put("location_area_code", lac);// 4474data.put("mobile_country_code", mcc);// 460data.put("mobile_network_code", mnc);// 0array.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();}JSONObject jsonObject = new JSONObject(sb.toString());JSONObject jsonObject1 = new JSONObject(jsonObject.getString("location"));getAddress(jsonObject1.getString("latitude"), jsonObject1.getString("longitude"));//mTextView.setText(sb.toString());} catch (Exception e) {// TODO: handle exception}}});}void getAddress(String lat, String lag) {try {URL url = new URL("http://maps.google.cn/maps/geo?key=abcdefg&q="+ lat + "," + lag);InputStream inputStream = url.openConnection().getInputStream();InputStreamReader inputReader = new InputStreamReader(inputStream,"utf-8");BufferedReader bufReader = new BufferedReader(inputReader);String line = "", lines = "";while ((line = bufReader.readLine()) != null) {lines += line;}if (!lines.equals("")) {JSONObject jsonobject = new JSONObject(lines);JSONArray jsonArray = new JSONArray(jsonobject.get("Placemark").toString());for (int i = 0; i < jsonArray.length(); i++) {mTextView.setText(mTextView.getText() + "\n"+ jsonArray.getJSONObject(i).getString("address"));}}} catch (Exception e) {;}}}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 支付宝借钱不还怎么办 鞋子里鞋垫老跑怎么办 鞋垫在鞋里老串怎么办 网贷已经借不到怎么办 骨龄比实际年龄大怎么办 小孩崴脚了肿了怎么办 报到证过期2年多怎么办 报到证过了期限怎么办 报到证超过两年怎么办 报到证过期4年了怎么办 学校没发报到证怎么办 未报到证过期了怎么办 专升本的报到证怎么办 蓝色报到证丢了怎么办 报到证用过了怎么办 大学报到证丢了怎么办 qq好被冻结了怎么办啊 qq突然被冻结了怎么办 扣扣密码被冻结怎么办 蠕动泵不抽水了怎么办 失眠多梦压力大怎么办 大福的皮硬了怎么办 安卓手机root失败怎么办 你的手机已root怎么办 移动网不能看片怎么办 网站的密码忘了怎么办 拍拍贷换手机了怎么办 六个半月的宝宝发烧怎么办 5个月的小孩发烧怎么办 3个月小孩发烧怎么办 2岁宝宝感冒发烧怎么办 2岁宝宝发烧38度怎么办 2岁宝宝反复发烧怎么办 八个月的孩子发烧怎么办 2岁宝宝咳嗽厉害怎么办 2岁宝宝睡觉咳嗽怎么办 2岁宝宝感冒咳嗽怎么办 2岁宝宝晚上咳嗽怎么办 1岁宝宝老便秘怎么办 2岁多宝宝老便秘怎么办 1岁半宝宝便秘怎么办