Android gps wifi 基站 定位集合

 

          其中GPS定位首先是GpsTask类异步返回GPS经纬度信息

GpsTask gpstask = new GpsTask(GpsActivity.this,new GpsTaskCallBack() {                        @Override                       
	public void gpsConnectedTimeOut() {                           
	gps_tip.setText("获取GPS超时了");                       
	}                       
	@Override                       
	public void gpsConnected(GpsData gpsdata) {                           
	do_gps(gpsdata);                       
	}                   
	}, 3000);          
	gpstask.execute();
           其中3000是设置获取gps数据timeout时间。GpsTask是根据
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
获取最后一次GPS信息,如果返回为Null则是根据监听获取Location信息发生改变时返回的GPS信息。因为手机获取GPS信息时间比较长,所以这个类实际使用时可能还存在一些BUG。 IAddressTask封装了获取地理位置的方法,具体代码如下:
 

	package com.maxtech.common.gps;

	import java.io.BufferedReader;
	import java.io.InputStreamReader;
	import org.apache.http.HttpEntity;
	import org.apache.http.HttpResponse;
	import org.json.JSONArray;
	import org.json.JSONObject;
	import android.app.Activity;
	import android.content.Context;
	import android.net.wifi.WifiManager;
	import android.telephony.TelephonyManager;
	import android.telephony.gsm.GsmCellLocation;

	public abstract class IAddressTask {
	        protected Activity context;

	        public IAddressTask(Activity context) {
	                this.context = context;
	        }

	        public abstract HttpResponse execute(JSONObject params) throws Exception;

	        public MLocation doWifiPost() throws Exception {
	                return transResponse(execute(doWifi()));
	        }

	        public MLocation doApnPost() throws Exception {
	                return transResponse(execute(doApn()));
	        }

	        public MLocation doGpsPost(double lat, double lng) throws Exception {
	                return transResponse(execute(doGps(lat, lng)));
	        }

	        private MLocation transResponse(HttpResponse response) {
	                MLocation location = null;
	                if (response.getStatusLine().getStatusCode() == 200) {
	                        location = new MLocation();
	                        HttpEntity entity = response.getEntity();
	                        BufferedReader br;
	                        try {
	                                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 json = new JSONObject(sb.toString());
	                                JSONObject lca = json.getJSONObject("location");
	                                location.Access_token = json.getString("access_token");
	                                if (lca != null) {
	                                        if (lca.has("accuracy"))
	                                                location.Accuracy = lca.getString("accuracy");
	                                        if (lca.has("longitude"))
	                                                location.Latitude = lca.getDouble("longitude");
	                                        if (lca.has("latitude"))
	                                                location.Longitude = lca.getDouble("latitude");
	                                        if (lca.has("address")) {
	                                                JSONObject address = lca.getJSONObject("address");
	                                                if (address != null) {
	                                                        if (address.has("region"))
	                                                                location.Region = address.getString("region");
	                                                        if (address.has("street_number"))
	                                                                location.Street_number = address
	                                                                                .getString("street_number");
	                                                        if (address.has("country_code"))
	                                                                location.Country_code = address
	                                                                                .getString("country_code");
	                                                        if (address.has("street"))
	                                                                location.Street = address.getString("street");
	                                                        if (address.has("city"))
	                                                                location.City = address.getString("city");
	                                                        if (address.has("country"))
	                                                                location.Country = address.getString("country");
	                                                }
	                                        }
	                                }
	                        } catch (Exception e) {
	                                e.printStackTrace();
	                                location = null;
	                        }
	                }
	                return location;
	        }

	        private JSONObject doGps(double lat, double lng) throws Exception {
	                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);
	                JSONObject data = new JSONObject();
	                data.put("latitude", lat);
	                data.put("longitude", lng);
	                holder.put("location", data);
	                return holder;
	        }

	        private JSONObject doApn() throws Exception {
	                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);
	                TelephonyManager tm = (TelephonyManager) context
	                                .getSystemService(Context.TELEPHONY_SERVICE);
	                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));
	                JSONArray array = new JSONArray();
	                JSONObject data = new JSONObject();
	                data.put("cell_id", cid);
	                data.put("location_area_code", lac);
	                data.put("mobile_country_code", mcc);
	                data.put("mobile_network_code", mnc);
	                array.put(data);
	                holder.put("cell_towers", array);
	                return holder;
	        }

	        private JSONObject doWifi() throws Exception {<br />        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);                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);                if(wifiManager.getConnectionInfo().getBSSID() == null) {            throw new RuntimeException("bssid is null");        }                JSONArray array = new JSONArray();        JSONObject data = new JSONObject();        data.put("mac_address", wifiManager.getConnectionInfo().getBSSID());          data.put("signal_strength", 8);          data.put("age", 0);          array.put(data);        holder.put("wifi_towers", array);                return holder;    }        public static class MLocation {
	                public String Access_token;
	                public double Latitude;
	                public double Longitude;
	                public String Accuracy;
	                public String Region;
	                public String Street_number;
	                public String Country_code;
	                public String Street;
	                public String City;
	                public String Country;

	                @Override
	                public String toString() {
	                        StringBuffer buffer = new StringBuffer();
	                        buffer.append("Access_token:" + Access_token + "\n");
	                        buffer.append("Region:" + Region + "\n");
	                        buffer.append("Accuracy:" + Accuracy + "\n");
	                        buffer.append("Latitude:" + Latitude + "\n");
	                        buffer.append("Longitude:" + Longitude + "\n");
	                        buffer.append("Country_code:" + Country_code + "\n");
	                        buffer.append("Country:" + Country + "\n");
	                        buffer.append("City:" + City + "\n");
	                        buffer.append("Street:" + Street + "\n");
	                        buffer.append("Street_number:" + Street_number + "\n");
	                        return buffer.toString();
	                }
	        }
	} 

	 

 


 


26130305_r9Ul.jpg
转载:http://www.adobex.com/android/source/details/00000442.htm

转载于:https://my.oschina.net/androidcode/blog/105114

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值