Android基站定位——单基站定位(二)

基站定位原理:通过手机信号获取基站信息,然后调用第三方公开的根据基站信息查找基站的经纬度值及地址信息(大概位置)。

 一、通过手机信号获取基站信息(详细的可以参考:Android基站定位——通过手机信号获取基站信息(一)

  1. TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
  2.   
  3. // 返回值MCC + MNC   
  4. String operator = mTelephonyManager.getNetworkOperator();  
  5. mcc = Integer.parseInt(operator.substring(03));  
  6. mnc = Integer.parseInt(operator.substring(3));  
  7.   
  8. // 中国移动和中国联通获取LAC、CID的方式   
  9. GsmCellLocation location = (GsmCellLocation) mTelephonyManager.getCellLocation();  
  10. lac = location.getLac();  
  11. cid = location.getCid();  
  12.   
  13. Log.i(TAG, "MCC = " + mcc + "\t MNC = " + mnc + "\t LAC = " + lac + "\t CID = " + cid);  
                TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

                // 返回值MCC + MNC
                String operator = mTelephonyManager.getNetworkOperator();
                mcc = Integer.parseInt(operator.substring(0, 3));
                mnc = Integer.parseInt(operator.substring(3));

                // 中国移动和中国联通获取LAC、CID的方式
                GsmCellLocation location = (GsmCellLocation) mTelephonyManager.getCellLocation();
                lac = location.getLac();
                cid = location.getCid();

                Log.i(TAG, "MCC = " + mcc + "\t MNC = " + mnc + "\t LAC = " + lac + "\t CID = " + cid);

二、调用第三方公开的API(根据基站信息查找基站的经纬度值及地址信息)

      1、组拼JSON形式的请求参数

  1. /** 
  2.     * 获取JSON形式的基站信息 
  3.     * @param mcc 移动国家代码(中国的为460) 
  4.     * @param mnc 移动网络号码(中国移动为0,中国联通为1,中国电信为2);  
  5.     * @param lac 位置区域码 
  6.     * @param cid 基站编号 
  7.     * @return json 
  8.     * @throws JSONException 
  9.     */  
  10.    private String getJsonCellPos(int mcc, int mnc, int lac, int cid) throws JSONException {  
  11.        JSONObject jsonCellPos = new JSONObject();  
  12.        jsonCellPos.put("version""1.1.0");  
  13.        jsonCellPos.put("host""maps.google.com");  
  14.   
  15.        JSONArray array = new JSONArray();  
  16.        JSONObject json1 = new JSONObject();  
  17.        json1.put("location_area_code""" + lac + "");  
  18.        json1.put("mobile_country_code""" + mcc + "");  
  19.        json1.put("mobile_network_code""" + mnc + "");  
  20.        json1.put("age"0);  
  21.        json1.put("cell_id""" + cid + "");  
  22.        array.put(json1);  
  23.   
  24.        jsonCellPos.put("cell_towers", array);  
  25.        return jsonCellPos.toString();  
  26.    }  
 /**
     * 获取JSON形式的基站信息
     * @param mcc 移动国家代码(中国的为460)
     * @param mnc 移动网络号码(中国移动为0,中国联通为1,中国电信为2); 
     * @param lac 位置区域码
     * @param cid 基站编号
     * @return json
     * @throws JSONException
     */
    private String getJsonCellPos(int mcc, int mnc, int lac, int cid) throws JSONException {
        JSONObject jsonCellPos = new JSONObject();
        jsonCellPos.put("version", "1.1.0");
        jsonCellPos.put("host", "maps.google.com");

        JSONArray array = new JSONArray();
        JSONObject json1 = new JSONObject();
        json1.put("location_area_code", "" + lac + "");
        json1.put("mobile_country_code", "" + mcc + "");
        json1.put("mobile_network_code", "" + mnc + "");
        json1.put("age", 0);
        json1.put("cell_id", "" + cid + "");
        array.put(json1);

        jsonCellPos.put("cell_towers", array);
        return jsonCellPos.toString();
    }

      2、通过HTTP协议网络请求源码: 

  1. request URL:http://www.minigps.net/minigps/map/google/location  
  2. Request Method:POST  
  3. Status Code:200 OK  
  4. Request Headersview source  
  5. Accept:application/json, text/javascript, */*; q=0.01  
  6. Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3  
  7. Accept-Encoding:gzip,deflate,sdch  
  8. Accept-Language:zh-CN,zh;q=0.8  
  9. Connection:keep-alive  
  10. Content-Length:191  
  11. Content-Type:application/json; charset=UTF-8  
  12. Cookie:bdshare_firstime=1356366713546; JSESSIONID=68243935CD3355089CF07A3A22AAB372  
  13. Host:www.minigps.net  
  14. Origin:http://www.minigps.net  
  15. Referer:http://www.minigps.net/map.html  
  16. User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4  
  17. X-Requested-With:XMLHttpRequest  
  18. Request Payload  
  19. {"cell_towers":[{"mobile_network_code":"1","location_area_code":"43018","cell_id":"11152773","age":0,"mobile_country_code":"460"}],"host":"maps.google.com","version":"1.1.0"}  
  20.   
  21. Response Headersview source  
  22. Content-Type:application/json  
  23. Date:Sat, 03 Jan 2013 14:03:15 GMT  
  24. Server:Apache-Coyote/1.1  
  25. Transfer-Encoding:chunked  
request URL:http://www.minigps.net/minigps/map/google/location
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:zh-CN,zh;q=0.8
Connection:keep-alive
Content-Length:191
Content-Type:application/json; charset=UTF-8
Cookie:bdshare_firstime=1356366713546; JSESSIONID=68243935CD3355089CF07A3A22AAB372
Host:www.minigps.net
Origin:http://www.minigps.net
Referer:http://www.minigps.net/map.html
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4
X-Requested-With:XMLHttpRequest
Request Payload
{"cell_towers":[{"mobile_network_code":"1","location_area_code":"43018","cell_id":"11152773","age":0,"mobile_country_code":"460"}],"host":"maps.google.com","version":"1.1.0"}

Response Headersview source
Content-Type:application/json
Date:Sat, 03 Jan 2013 14:03:15 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

      

        3、用JAVA代码具体实现:

  1. /** 
  2.     * 调用第三方公开的API根据基站信息查找基站的经纬度值及地址信息 
  3.     */  
  4.    public String httpPost(String url, String jsonCellPos) throws IOException{  
  5.        byte[] data = jsonCellPos.toString().getBytes();  
  6.        URL realUrl = new URL(url);  
  7.        HttpURLConnection httpURLConnection = (HttpURLConnection) realUrl.openConnection();  
  8.        httpURLConnection.setConnectTimeout(6 * 1000);  
  9.        httpURLConnection.setDoOutput(true);  
  10.        httpURLConnection.setDoInput(true);  
  11.        httpURLConnection.setUseCaches(false);  
  12.        httpURLConnection.setRequestMethod("POST");  
  13.        httpURLConnection.setRequestProperty("Accept""application/json, text/javascript, */*; q=0.01");  
  14.        httpURLConnection.setRequestProperty("Accept-Charset""GBK,utf-8;q=0.7,*;q=0.3");  
  15.        httpURLConnection.setRequestProperty("Accept-Encoding""gzip,deflate,sdch");  
  16.        httpURLConnection.setRequestProperty("Accept-Language""zh-CN,zh;q=0.8");  
  17.        httpURLConnection.setRequestProperty("Connection""Keep-Alive");  
  18.        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));  
  19.        httpURLConnection.setRequestProperty("Content-Type""application/json; charset=UTF-8");  
  20.   
  21.        httpURLConnection.setRequestProperty("Host""www.minigps.net");  
  22.        httpURLConnection.setRequestProperty("Referer""http://www.minigps.net/map.html");  
  23.        httpURLConnection.setRequestProperty("User-Agent",  
  24.                "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4X-Requested-With:XMLHttpRequest");  
  25.   
  26.        httpURLConnection.setRequestProperty("X-Requested-With""XMLHttpRequest");  
  27.        httpURLConnection.setRequestProperty("Host""www.minigps.net");  
  28.   
  29.        DataOutputStream outStream = new DataOutputStream(httpURLConnection.getOutputStream());  
  30.        outStream.write(data);  
  31.        outStream.flush();  
  32.        outStream.close();  
  33.   
  34.        if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {  
  35.            InputStream inputStream = httpURLConnection.getInputStream();  
  36.            return new String(read(inputStream));  
  37.        }  
  38.        return null;  
  39.    }  
 /**
     * 调用第三方公开的API根据基站信息查找基站的经纬度值及地址信息
     */
    public String httpPost(String url, String jsonCellPos) throws IOException{
        byte[] data = jsonCellPos.toString().getBytes();
        URL realUrl = new URL(url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) realUrl.openConnection();
        httpURLConnection.setConnectTimeout(6 * 1000);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
        httpURLConnection.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
        httpURLConnection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
        httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
        httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));
        httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

        httpURLConnection.setRequestProperty("Host", "www.minigps.net");
        httpURLConnection.setRequestProperty("Referer", "http://www.minigps.net/map.html");
        httpURLConnection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4X-Requested-With:XMLHttpRequest");

        httpURLConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
        httpURLConnection.setRequestProperty("Host", "www.minigps.net");

        DataOutputStream outStream = new DataOutputStream(httpURLConnection.getOutputStream());
        outStream.write(data);
        outStream.flush();
        outStream.close();

        if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = httpURLConnection.getInputStream();
            return new String(read(inputStream));
        }
        return null;
    }

      4、读取返回的JSON数据流代码:

  1. /** 
  2.     * 读取IO流并以byte[]形式存储 
  3.     * @param inputSream InputStream 
  4.     * @return byte[] 
  5.     * @throws IOException 
  6.     */  
  7.    public byte[] read(InputStream inputSream) throws IOException {  
  8.        ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  9.        int len = -1;  
  10.        byte[] buffer = new byte[1024];  
  11.        while ((len = inputSream.read(buffer)) != -1) {  
  12.            outStream.write(buffer, 0, len);  
  13.        }  
  14.        outStream.close();  
  15.        inputSream.close();  
  16.   
  17.        return outStream.toByteArray();  
  18.    }  
 /**
     * 读取IO流并以byte[]形式存储
     * @param inputSream InputStream
     * @return byte[]
     * @throws IOException
     */
    public byte[] read(InputStream inputSream) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        int len = -1;
        byte[] buffer = new byte[1024];
        while ((len = inputSream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        outStream.close();
        inputSream.close();

        return outStream.toByteArray();
    }

 

三、请求参数及返回结果的JSON形式:

      1、请求的JSON参数值:

  1. {  
  2.   "cell_towers":  
  3.      [  
  4.       {  
  5.           "mobile_network_code":"1",  
  6.           "location_area_code":"43018",  
  7.           "cell_id":"11152773",  
  8.           "age":0,  
  9.           "mobile_country_code":"460"  
  10.       }  
  11.      ],  
  12.      "host":"maps.google.com",  
  13.      "version":"1.1.0"  
  14.  }  
       {
         "cell_towers":
            [
             {
                 "mobile_network_code":"1",
                 "location_area_code":"43018",
                 "cell_id":"11152773",
                 "age":0,
                 "mobile_country_code":"460"
             }
            ],
            "host":"maps.google.com",
            "version":"1.1.0"
        }

       2、返回的JSON结果值:

  1. {  
  2.  "location":  
  3.      {  
  4.          "latitude":"31.211389541625977",  
  5.          "longitude":"121.60332489013672",  
  6.          "address":  
  7.              {"city":  
  8.                  "上海市浦东新区居里路432号;浦东新区光启安老院、第一三共制药上海公司、SUNPLUS[附近]",  
  9.                  "country":"",  
  10.                  "country_code":"",  
  11.                  "county":"",  
  12.                  "postal_code":"",  
  13.                  "region":"",  
  14.                  "street":"",  
  15.                  "street_number":""  
  16.              }  
  17.      },  
  18.      "access_token":"dummytoken"  
  19. }  
    {
     "location":
         {
             "latitude":"31.211389541625977",
             "longitude":"121.60332489013672",
             "address":
                 {"city":
                     "上海市浦东新区居里路432号;浦东新区光启安老院、第一三共制药上海公司、SUNPLUS[附近]",
                     "country":"",
                     "country_code":"",
                     "county":"",
                     "postal_code":"",
                     "region":"",
                     "street":"",
                     "street_number":""
                 }
         },
         "access_token":"dummytoken"
    }

四、完整代码及所需权限:

Java代码:

  1. package com.easipass.test;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.DataOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.net.HttpURLConnection;  
  8. import java.net.URL;  
  9.   
  10. import org.json.JSONArray;  
  11. import org.json.JSONException;  
  12. import org.json.JSONObject;  
  13.   
  14. import android.app.Activity;  
  15. import android.content.Context;  
  16. import android.os.Bundle;  
  17. import android.telephony.TelephonyManager;  
  18. import android.telephony.gsm.GsmCellLocation;  
  19. import android.util.Log;  
  20. import android.view.View;  
  21.   
  22. /** 
  23.  * 功能描述:单基站定位 
  24.  * @author android_ls 
  25.  */  
  26. public class GSMCellLocationActivity extends Activity {  
  27.   
  28.     private static final String TAG = "GSMCellLocationActivity";  
  29.   
  30.     private int mcc;  
  31.   
  32.     private int mnc;  
  33.   
  34.     private int lac;  
  35.   
  36.     private int cid;  
  37.   
  38.     @Override  
  39.     public void onCreate(Bundle savedInstanceState) {  
  40.         super.onCreate(savedInstanceState);  
  41.         setContentView(R.layout.main);  
  42.   
  43.         // 获取基站信息   
  44.         findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {  
  45.   
  46.             @Override  
  47.             public void onClick(View v) {  
  48.   
  49.                 TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
  50.   
  51.                 // 返回值MCC + MNC   
  52.                 String operator = mTelephonyManager.getNetworkOperator();  
  53.                 mcc = Integer.parseInt(operator.substring(03));  
  54.                 mnc = Integer.parseInt(operator.substring(3));  
  55.   
  56.                 // 中国移动和中国联通获取LAC、CID的方式   
  57.                 GsmCellLocation location = (GsmCellLocation) mTelephonyManager.getCellLocation();  
  58.                 lac = location.getLac();  
  59.                 cid = location.getCid();  
  60.   
  61.                 Log.i(TAG, "MCC = " + mcc + "\t MNC = " + mnc + "\t LAC = " + lac + "\t CID = " + cid);  
  62.   
  63.                 new Thread() {  
  64.                     @Override  
  65.                     public void run() {  
  66.                         try {  
  67.                             String json = getJsonCellPos(mcc, mnc, lac, cid);  
  68.                             Log.i(TAG, "request = " + json);  
  69.   
  70.                             String url = "http://www.minigps.net/minigps/map/google/location";  
  71.                             String result = httpPost(url, json);  
  72.                             Log.i(TAG, "result = " + result);  
  73.                         } catch (Exception e) {  
  74.                             // TODO Auto-generated catch block   
  75.                             e.printStackTrace();  
  76.                         }  
  77.                     }  
  78.                 }.start();  
  79.   
  80.             }  
  81.         });  
  82.     }  
  83.   
  84.     /** 
  85.      * 调用第三方公开的API根据基站信息查找基站的经纬度值及地址信息 
  86.      */  
  87.     public String httpPost(String url, String jsonCellPos) throws IOException{  
  88.         byte[] data = jsonCellPos.toString().getBytes();  
  89.         URL realUrl = new URL(url);  
  90.         HttpURLConnection httpURLConnection = (HttpURLConnection) realUrl.openConnection();  
  91.         httpURLConnection.setConnectTimeout(6 * 1000);  
  92.         httpURLConnection.setDoOutput(true);  
  93.         httpURLConnection.setDoInput(true);  
  94.         httpURLConnection.setUseCaches(false);  
  95.         httpURLConnection.setRequestMethod("POST");  
  96.         httpURLConnection.setRequestProperty("Accept""application/json, text/javascript, */*; q=0.01");  
  97.         httpURLConnection.setRequestProperty("Accept-Charset""GBK,utf-8;q=0.7,*;q=0.3");  
  98.         httpURLConnection.setRequestProperty("Accept-Encoding""gzip,deflate,sdch");  
  99.         httpURLConnection.setRequestProperty("Accept-Language""zh-CN,zh;q=0.8");  
  100.         httpURLConnection.setRequestProperty("Connection""Keep-Alive");  
  101.         httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));  
  102.         httpURLConnection.setRequestProperty("Content-Type""application/json; charset=UTF-8");  
  103.   
  104.         httpURLConnection.setRequestProperty("Host""www.minigps.net");  
  105.         httpURLConnection.setRequestProperty("Referer""http://www.minigps.net/map.html");  
  106.         httpURLConnection.setRequestProperty("User-Agent",  
  107.                 "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4X-Requested-With:XMLHttpRequest");  
  108.   
  109.         httpURLConnection.setRequestProperty("X-Requested-With""XMLHttpRequest");  
  110.         httpURLConnection.setRequestProperty("Host""www.minigps.net");  
  111.   
  112.         DataOutputStream outStream = new DataOutputStream(httpURLConnection.getOutputStream());  
  113.         outStream.write(data);  
  114.         outStream.flush();  
  115.         outStream.close();  
  116.   
  117.         if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {  
  118.             InputStream inputStream = httpURLConnection.getInputStream();  
  119.             return new String(read(inputStream));  
  120.         }  
  121.         return null;  
  122.     }  
  123.   
  124.     /**  
  125.      * 获取JSON形式的基站信息  
  126.      * @param mcc 移动国家代码(中国的为460)  
  127.      * @param mnc 移动网络号码(中国移动为0,中国联通为1,中国电信为2);   
  128.      * @param lac 位置区域码  
  129.      * @param cid 基站编号  
  130.      * @return json  
  131.      * @throws JSONException  
  132.      */  
  133.     private String getJsonCellPos(int mcc, int mnc, int lac, int cid) throws JSONException {  
  134.         JSONObject jsonCellPos = new JSONObject();  
  135.         jsonCellPos.put("version""1.1.0");  
  136.         jsonCellPos.put("host""maps.google.com");  
  137.   
  138.         JSONArray array = new JSONArray();  
  139.         JSONObject json1 = new JSONObject();  
  140.         json1.put("location_area_code""" + lac + "");  
  141.         json1.put("mobile_country_code""" + mcc + "");  
  142.         json1.put("mobile_network_code""" + mnc + "");  
  143.         json1.put("age"0);  
  144.         json1.put("cell_id""" + cid + "");  
  145.         array.put(json1);  
  146.   
  147.         jsonCellPos.put("cell_towers", array);  
  148.         return jsonCellPos.toString();  
  149.     }  
  150.   
  151.     /** 
  152.      * 读取IO流并以byte[]形式存储 
  153.      * @param inputSream InputStream 
  154.      * @return byte[] 
  155.      * @throws IOException 
  156.      */  
  157.     public byte[] read(InputStream inputSream) throws IOException {  
  158.         ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
  159.         int len = -1;  
  160.         byte[] buffer = new byte[1024];  
  161.         while ((len = inputSream.read(buffer)) != -1) {  
  162.             outStream.write(buffer, 0, len);  
  163.         }  
  164.         outStream.close();  
  165.         inputSream.close();  
  166.   
  167.         return outStream.toByteArray();  
  168.     }  
  169.   
  170. }  
package com.easipass.test;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONArray;
import org.json.JSONException;
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.util.Log;
import android.view.View;

/**
 * 功能描述:单基站定位
 * @author android_ls
 */
public class GSMCellLocationActivity extends Activity {

    private static final String TAG = "GSMCellLocationActivity";

    private int mcc;

    private int mnc;

    private int lac;

    private int cid;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // 获取基站信息
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

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

                // 返回值MCC + MNC
                String operator = mTelephonyManager.getNetworkOperator();
                mcc = Integer.parseInt(operator.substring(0, 3));
                mnc = Integer.parseInt(operator.substring(3));

                // 中国移动和中国联通获取LAC、CID的方式
                GsmCellLocation location = (GsmCellLocation) mTelephonyManager.getCellLocation();
                lac = location.getLac();
                cid = location.getCid();

                Log.i(TAG, "MCC = " + mcc + "\t MNC = " + mnc + "\t LAC = " + lac + "\t CID = " + cid);

                new Thread() {
                    @Override
                    public void run() {
                        try {
                            String json = getJsonCellPos(mcc, mnc, lac, cid);
                            Log.i(TAG, "request = " + json);

                            String url = "http://www.minigps.net/minigps/map/google/location";
                            String result = httpPost(url, json);
                            Log.i(TAG, "result = " + result);
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }.start();

            }
        });
    }

    /**
     * 调用第三方公开的API根据基站信息查找基站的经纬度值及地址信息
     */
    public String httpPost(String url, String jsonCellPos) throws IOException{
        byte[] data = jsonCellPos.toString().getBytes();
        URL realUrl = new URL(url);
        HttpURLConnection httpURLConnection = (HttpURLConnection) realUrl.openConnection();
        httpURLConnection.setConnectTimeout(6 * 1000);
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);
        httpURLConnection.setUseCaches(false);
        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01");
        httpURLConnection.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
        httpURLConnection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
        httpURLConnection.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8");
        httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
        httpURLConnection.setRequestProperty("Content-Length", String.valueOf(data.length));
        httpURLConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

        httpURLConnection.setRequestProperty("Host", "www.minigps.net");
        httpURLConnection.setRequestProperty("Referer", "http://www.minigps.net/map.html");
        httpURLConnection.setRequestProperty("User-Agent",
                "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4X-Requested-With:XMLHttpRequest");

        httpURLConnection.setRequestProperty("X-Requested-With", "XMLHttpRequest");
        httpURLConnection.setRequestProperty("Host", "www.minigps.net");

        DataOutputStream outStream = new DataOutputStream(httpURLConnection.getOutputStream());
        outStream.write(data);
        outStream.flush();
        outStream.close();

        if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
            InputStream inputStream = httpURLConnection.getInputStream();
            return new String(read(inputStream));
        }
        return null;
    }

    /**
     * 获取JSON形式的基站信息
     * @param mcc 移动国家代码(中国的为460)
     * @param mnc 移动网络号码(中国移动为0,中国联通为1,中国电信为2); 
     * @param lac 位置区域码
     * @param cid 基站编号
     * @return json
     * @throws JSONException
     */
    private String getJsonCellPos(int mcc, int mnc, int lac, int cid) throws JSONException {
        JSONObject jsonCellPos = new JSONObject();
        jsonCellPos.put("version", "1.1.0");
        jsonCellPos.put("host", "maps.google.com");

        JSONArray array = new JSONArray();
        JSONObject json1 = new JSONObject();
        json1.put("location_area_code", "" + lac + "");
        json1.put("mobile_country_code", "" + mcc + "");
        json1.put("mobile_network_code", "" + mnc + "");
        json1.put("age", 0);
        json1.put("cell_id", "" + cid + "");
        array.put(json1);

        jsonCellPos.put("cell_towers", array);
        return jsonCellPos.toString();
    }

    /**
     * 读取IO流并以byte[]形式存储
     * @param inputSream InputStream
     * @return byte[]
     * @throws IOException
     */
    public byte[] read(InputStream inputSream) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        int len = -1;
        byte[] buffer = new byte[1024];
        while ((len = inputSream.read(buffer)) != -1) {
            outStream.write(buffer, 0, len);
        }
        outStream.close();
        inputSream.close();

        return outStream.toByteArray();
    }

}

在AndroidManifest.xml添加获取位置信息的权限:

  1. <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
  2. <uses-permission android:name="android.permission.INTERNET" />  
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

五、测试网址:http://www.minigps.net/map.html

六、Google的基站定位http://www.google.com/loc/json或者http://www.google.com.hk/loc/json已不可用,访问返回404。官方给出的答复:https://developers.google.com/gears/?hl=zh-TW

 参考过的博客:echo3博主的json基站定位接口 免费使用

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值