`

android 基于基站,apn,gps,wifi,network 根据不同手机sim卡获取经纬度

阅读更多

一:新建MyLocation类,本类主要管理使用各种获取经纬度的方法,由于代码比较多就不一一解释直接上代码:

 

Java代码  收藏代码
  1. package com.android.location2;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.io.UnsupportedEncodingException;  
  7. import java.util.ArrayList;  
  8. import org.apache.http.HttpEntity;  
  9. import org.apache.http.HttpResponse;  
  10. import org.apache.http.client.ClientProtocolException;  
  11. import org.apache.http.client.methods.HttpPost;  
  12. import org.apache.http.entity.StringEntity;  
  13. import org.apache.http.impl.client.DefaultHttpClient;  
  14. import org.json.JSONArray;  
  15. import org.json.JSONException;  
  16. import org.json.JSONObject;  
  17. import android.app.Activity;  
  18. import android.content.Context;  
  19. import android.location.Location;  
  20. import android.location.LocationManager;  
  21. import android.os.AsyncTask;  
  22. import android.telephony.TelephonyManager;  
  23. import android.telephony.cdma.CdmaCellLocation;  
  24. import android.widget.Toast;  
  25. import com.android.gps.AddressTask;  
  26. import com.android.gps.CellIDInfo;  
  27. import com.android.gps.GpsTask;  
  28. import com.android.gps.GpsTask.GpsData;  
  29. import com.android.gps.GpsTaskCallBack;  
  30. import com.android.gps.IAddressTask;  
  31. import com.android.gps.IAddressTask.MLocation;  
  32.   
  33. public class MyLocation {  
  34.   
  35.     private LocationCallBack callBack;  
  36.     private boolean status = false;  
  37.   
  38.     public MyLocation(LocationCallBack callBack, Activity cxt) {  
  39.         this.callBack = callBack;  
  40.         if (getMobileType(cxt) == 3) {  
  41.   
  42.             TelephonyManager tm = (TelephonyManager) cxt  
  43.                     .getSystemService(Context.TELEPHONY_SERVICE);  
  44.             CdmaCellLocation location = (CdmaCellLocation) tm.getCellLocation();  
  45.             if (location == null)  
  46.                 return;  
  47.             int sid = location.getSystemId();// 系统标识 mobileNetworkCode  
  48.             int bid = location.getBaseStationId();// 基站小区号 cellId  
  49.             int nid = location.getNetworkId();// 网络标识 locationAreaCode  
  50.             ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();  
  51.             CellIDInfo info = new CellIDInfo();  
  52.             info.cellId = bid;  
  53.             info.locationAreaCode = nid;  
  54.             info.mobileNetworkCode = String.valueOf(sid);  
  55.             info.mobileCountryCode = tm.getNetworkOperator().substring(03);  
  56.             info.mobileCountryCode = tm.getNetworkOperator().substring(35);  
  57.             info.radioType = "cdma";  
  58.             CellID.add(info);  
  59.             Location mLocation = callGear(CellID);  
  60.             if (mLocation != null) {  
  61.                 callBack.onCurrentLocation(mLocation.getLatitude(),  
  62.                         mLocation.getLongitude());  
  63.             }  
  64.         } else {  
  65.             Location mLocation = getLocation(cxt);  
  66.             if (mLocation != null) {  
  67.                 callBack.onCurrentLocation(mLocation.getLatitude(),  
  68.                         mLocation.getLongitude());  
  69.             } else {  
  70.                 getMeLocation(cxt);  
  71.             }  
  72.         }  
  73.     }  
  74.   
  75.     public interface LocationCallBack {  
  76.         void onCurrentLocation(double latitude, double longitude);  
  77.   
  78.     }  
  79.   
  80.     // 调用google gears的方法,该方法调用gears来获取经纬度  
  81.     private Location callGear(ArrayList<CellIDInfo> cellID) {  
  82.         if (cellID == null)  
  83.             return null;  
  84.   
  85.         DefaultHttpClient client = new DefaultHttpClient();  
  86.         HttpPost post = new HttpPost("http://www.google.com/loc/json");  
  87.         JSONObject holder = new JSONObject();  
  88.   
  89.         try {  
  90.             holder.put("version""1.1.0");  
  91.             holder.put("host""maps.google.com");  
  92.             holder.put("home_mobile_country_code",  
  93.                     cellID.get(0).mobileCountryCode);  
  94.             holder.put("home_mobile_network_code",  
  95.                     cellID.get(0).mobileNetworkCode);  
  96.             holder.put("radio_type", cellID.get(0).radioType);  
  97.             holder.put("request_address"true);  
  98.             if ("460".equals(cellID.get(0).mobileCountryCode))  
  99.                 holder.put("address_language""zh_CN");  
  100.             else  
  101.                 holder.put("address_language""en_US");  
  102.   
  103.             JSONObject data, current_data;  
  104.   
  105.             JSONArray array = new JSONArray();  
  106.   
  107.             current_data = new JSONObject();  
  108.             current_data.put("cell_id", cellID.get(0).cellId);  
  109.             current_data.put("location_area_code",  
  110.                     cellID.get(0).locationAreaCode);  
  111.             current_data.put("mobile_country_code",  
  112.                     cellID.get(0).mobileCountryCode);  
  113.             current_data.put("mobile_network_code",  
  114.                     cellID.get(0).mobileNetworkCode);  
  115.             current_data.put("age"0);  
  116.             current_data.put("signal_strength", -60);  
  117.             current_data.put("timing_advance"5555);  
  118.             array.put(current_data);  
  119.   
  120.             holder.put("cell_towers", array);  
  121.   
  122.             StringEntity se = new StringEntity(holder.toString());  
  123.             post.setEntity(se);  
  124.             HttpResponse resp = client.execute(post);  
  125.   
  126.             HttpEntity entity = resp.getEntity();  
  127.   
  128.             BufferedReader br = new BufferedReader(new InputStreamReader(  
  129.                     entity.getContent()));  
  130.             StringBuffer sb = new StringBuffer();  
  131.             String result = br.readLine();  
  132.             while (result != null) {  
  133.                 sb.append(result);  
  134.                 result = br.readLine();  
  135.             }  
  136.   
  137.             data = new JSONObject(sb.toString());  
  138.             data = (JSONObject) data.get("location");  
  139.   
  140.             Location loc = new Location(LocationManager.NETWORK_PROVIDER);  
  141.             loc.setLatitude((Double) data.get("latitude"));  
  142.             loc.setLongitude((Double) data.get("longitude"));  
  143.             loc.setAccuracy(Float.parseFloat(data.get("accuracy").toString()));  
  144.             loc.setTime(System.currentTimeMillis());// AppUtil.getUTCTime());  
  145.             return loc;  
  146.         } catch (JSONException e) {  
  147.             e.printStackTrace();  
  148.             return null;  
  149.         } catch (UnsupportedEncodingException e) {  
  150.             e.printStackTrace();  
  151.         } catch (ClientProtocolException e) {  
  152.             e.printStackTrace();  
  153.         } catch (IOException e) {  
  154.             e.printStackTrace();  
  155.         }  
  156.         return null;  
  157.     }  
  158.   
  159.     @SuppressWarnings("unchecked")  
  160.     public void getMeLocation(final Activity cxt) {  
  161.         do_apn(cxt);  
  162.         if (status) {  
  163.             status = false;  
  164.             do_wifi(cxt);  
  165.         } else if (status) {  
  166.             status = false;  
  167.             GpsTask gpstask = new GpsTask(cxt, new GpsTaskCallBack() {  
  168.   
  169.                 public void gpsConnectedTimeOut() {  
  170.                     Toast.makeText(cxt, "Error", Toast.LENGTH_LONG).show();  
  171.                 }  
  172.   
  173.                 public void gpsConnected(GpsData gpsdata) {  
  174.                     do_gps(gpsdata, cxt);  
  175.                 }  
  176.   
  177.             }, 3000);  
  178.             gpstask.execute();  
  179.         }  
  180.   
  181.     }  
  182.   
  183.     private void do_apn(final Activity cxt) {  
  184.   
  185.         new AsyncTask<Void, Void, MLocation>() {  
  186.   
  187.             protected MLocation doInBackground(Void... params) {  
  188.                 MLocation location = null;  
  189.                 try {  
  190.                     location = new AddressTask(cxt, IAddressTask.DO_APN)  
  191.                             .doApnPost();  
  192.                 } catch (Exception e) {  
  193.                     e.printStackTrace();  
  194.                 }  
  195.                 if (location != null)  
  196.                     return location;  
  197.                 else  
  198.                     return null;  
  199.             }  
  200.   
  201.             protected void onPreExecute() {  
  202.                 super.onPreExecute();  
  203.             }  
  204.   
  205.             protected void onPostExecute(MLocation result) {  
  206.                 if (result != null) {  
  207.                     setData(result);  
  208.                     status = true;  
  209.                 } else {  
  210.                     status = false;  
  211.                 }  
  212.                 super.onPostExecute(result);  
  213.             }  
  214.   
  215.         }.execute();  
  216.   
  217.     }  
  218.   
  219.     private void do_gps(final GpsData gpsdata, final Activity cxt) {  
  220.         new AsyncTask<Void, Void, MLocation>() {  
  221.   
  222.             protected MLocation doInBackground(Void... params) {  
  223.                 MLocation location = null;  
  224.                 try {  
  225.                     location = new AddressTask(cxt, IAddressTask.DO_GPS)  
  226.                             .doGpsPost(gpsdata.getLatitude(),  
  227.                                     gpsdata.getLongitude());  
  228.                 } catch (Exception e) {  
  229.                     e.printStackTrace();  
  230.                 }  
  231.                 return location;  
  232.             }  
  233.   
  234.             protected void onPreExecute() {  
  235.                 super.onPreExecute();  
  236.             }  
  237.   
  238.             protected void onPostExecute(MLocation result) {  
  239.                 // gps_tip.setText(result);  
  240.                 if (result != null) {  
  241.                     setData(result);  
  242.                     status = true;  
  243.                 } else {  
  244.                     status = false;  
  245.                 }  
  246.                 super.onPostExecute(result);  
  247.             }  
  248.   
  249.         }.execute();  
  250.   
  251.     }  
  252.   
  253.     private void do_wifi(final Activity cxt) {  
  254.         new AsyncTask<Void, Void, MLocation>() {  
  255.   
  256.             protected MLocation doInBackground(Void... params) {  
  257.                 MLocation location = null;  
  258.                 try {  
  259.                     location = new AddressTask(cxt, IAddressTask.DO_WIFI)  
  260.                             .doWifiPost();  
  261.                 } catch (Exception e) {  
  262.                     e.printStackTrace();  
  263.                 }  
  264.                 if (location == null) {  
  265.                     return null;  
  266.                 } else  
  267.                     return location;  
  268.             }  
  269.   
  270.             protected void onPreExecute() {  
  271.                 super.onPreExecute();  
  272.             }  
  273.   
  274.             protected void onPostExecute(MLocation result) {  
  275.                 if (result != null) {  
  276.                     setData(result);  
  277.                     status = true;  
  278.                 } else {  
  279.                     status = false;  
  280.                 }  
  281.                 super.onPostExecute(result);  
  282.             }  
  283.   
  284.         }.execute();  
  285.   
  286.     }  
  287.   
  288.     private void setData(MLocation result) {  
  289.   
  290.         callBack.onCurrentLocation(result.getLatitude(), result.getLongitude());  
  291.     }  
  292.   
  293.     // Get the Location by GPS or WIFI  
  294.     public Location getLocation(Context context) {  
  295.         LocationManager locMan = (LocationManager) context  
  296.                 .getSystemService(Context.LOCATION_SERVICE);  
  297.         Location location = locMan  
  298.                 .getLastKnownLocation(LocationManager.GPS_PROVIDER);  
  299.         if (location == null) {  
  300.             location = locMan  
  301.                     .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  
  302.         }  
  303.         return location;  
  304.     }  
  305.   
  306.     private int getMobileType(Context context) {  
  307.         TelephonyManager iPhoneManager = (TelephonyManager) context  
  308.                 .getSystemService(Context.TELEPHONY_SERVICE);  
  309.         String iNumeric = iPhoneManager.getSimOperator();  
  310.         if (iNumeric.length() > 0) {  
  311.             if (iNumeric.equals("46000") || iNumeric.equals("46002")) {  
  312.                 return 1;  
  313.             } else if (iNumeric.equals("46001")) {  
  314.                 return 2;  
  315.             } else if (iNumeric.equals("46003")) {  
  316.                 return 3;  
  317.             }  
  318.   
  319.         }  
  320.         return 1;  
  321.   
  322.     }  
  323. }  
 

 

二:在MainActivity 中实现LocationCallBack接口,如下:

 

Java代码  收藏代码
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.widget.TextView;  
  4. import com.android.location2.MyLocation.LocationCallBack;  
  5. public class MainActivity extends Activity implements LocationCallBack{  
  6. private TextView desText;  
  7.     @Override  
  8.     public void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.         desText = (TextView) this.findViewById(R.id.text);  
  12.         new MyLocation(MainActivity.thisthis);  
  13. }  
  14. //回调方法  
  15. public void onCurrentLocation(double latitude, double longitude) {  
  16. desText.setText("当前经度:" + longitude + "\n当前纬度:"+ latitude);  
  17. }  
  18.       
  19. }  
 

 

三:获取经纬度使用的辅助类,这里补贴代码后面直接上传源码:


四:效果如下图:


 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics