(002)安卓基站定位获得经纬度和地理信息

1.首先获得基站信息

public static SCell getSCellInfo(Context context) throws Exception

{...}

2.通过基站信息获得经纬度

public static SItude getItude(SCell cell) throws Exception

{...}

3.通过经纬度信息来获得具体的地理位置信息

public static String getLocation(SItude itude) throws Exception

{...}

 

ps:其中SCell类封装基站信息的数据,SItude类封装经纬度的数据

完整代码如下:

 

GpsUtil.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
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.content.Context;
import android.telephony.TelephonyManager;
import android.telephony.gsm.GsmCellLocation;
import android.util.Log;

public class GpsUtil
{

    /**
     * 获得基站信息
     * @param context 传入android上下文对象
     * @return 返回基站信息对象
     * @throws Exception
     */
    public static SCell getSCellInfo(Context context) throws Exception   
    {
        SCell sCell = new SCell();
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        GsmCellLocation gsmCellLocation = (GsmCellLocation) telephonyManager.getCellLocation();
        if(null == gsmCellLocation)
        {
            throw new Exception("获取基站信息失败");
        }
        String operator = telephonyManager.getNetworkOperator();
        int mcc = Integer.parseInt(operator.substring(0, 3));
        int mnc = Integer.parseInt(operator.substring(3));
        int cid = gsmCellLocation.getCid();
        int lac = gsmCellLocation.getLac();
        sCell.MCC = mcc;
        sCell.MNC = mnc;
        sCell.LAC = lac;
        sCell.CID = cid;
        return sCell;
    }
    
    /**
     * 获取经纬度信息
     * @param cell 传入基站信息对象
     * @return 返回经纬度
     * @throws Exception
     */
    public static SItude getItude(SCell cell) throws Exception
    {
        SItude situde = new SItude();
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost("http://www.google.com/loc/json");
        try
        {
            JSONObject holderJsonObject = new JSONObject();
            holderJsonObject.put("version", "1.1.0");
            holderJsonObject.put("host", "maps.google.com");
            holderJsonObject.put("address_language", "zh_CN");
            holderJsonObject.put("request_address", true);
            holderJsonObject.put("radio_type", "gsm");
            holderJsonObject.put("carrier", "HTC");
            JSONObject towerJsonObject = new JSONObject();
            towerJsonObject.put("mobile_country_code", cell.MCC);
            towerJsonObject.put("mobile_network_code", cell.MNC);
            towerJsonObject.put("cell_id", cell.CID);
            towerJsonObject.put("location_area_code", cell.LAC);
            JSONArray towerJsonArray = new JSONArray();
            towerJsonArray.put(towerJsonObject);
            holderJsonObject.put("cell_towers", towerJsonArray);
            StringEntity query = new StringEntity(holderJsonObject.toString());
            httpPost.setEntity(query);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpEntity.getContent()));
            StringBuffer stringBuffer = new StringBuffer();
            String result = null;
            while ((result = bufferedReader.readLine()) != null)
            {
                stringBuffer.append(result);
            }
            JSONObject jsonObject = new JSONObject(stringBuffer.toString());
            JSONObject subJsonObject = new JSONObject(jsonObject.getString("location"));
            situde.latitude = subJsonObject.getString("latitude");
            situde.longitude = subJsonObject.getString("longitude");
            Log.i("Itude", situde.latitude + situde.longitude);
        }
        catch (Exception e)
        {
            Log.e(e.getMessage(), e.toString());
            throw new Exception("获取经纬度出现错误:" + e.getMessage());
        }
        finally
        {
            httpPost.abort();
            httpClient = null;
        }
        return situde;
    }
    
    /**
     * 获得地理位置信息
     * @param itude 传入经纬度信息对象
     * @return 返回地理位置信息
     * @throws Exception
     */
    public static String getLocation(SItude itude) throws Exception
    {
        String resultString = "";
        String urlStr = String.format("http://maps.google.cn/maps/geo?key=abcdefg&q=%s,%s", itude.latitude,
                itude.longitude);
        Log.i("URL", urlStr);
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(urlStr);
        try
        {
            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpEntity.getContent()));
            StringBuffer strBuff = new StringBuffer();
            String result = null;
            while ((result = bufferedReader.readLine()) != null)
            {
                strBuff.append(result);
            }
            resultString = strBuff.toString();
            /** 解析JSON数据,获得物理地址 */
            if (resultString != null && resultString.length() > 0)
            {
                JSONObject jsonObject = new JSONObject(resultString);
                JSONArray jsonArray = new JSONArray(jsonObject.get("Placemark").toString());
                resultString = "";
                for (int i = 0; i < jsonArray.length(); i++)
                {
                    resultString = jsonArray.getJSONObject(i).getString("address");
                }
            }
        }
        catch (Exception e)
        {
            throw new Exception("获取物理位置出现错误:" + e.getMessage());
        }
        finally
        {
            httpGet.abort();
            httpClient = null;
        }
        return resultString;
    }

}
SCell.java
/**
 * 存放基站信息
 * 
 * @author Administrator
 * 
 */
public class SCell
{

    // Mobile Country Code,移动国家代码(中国的为460)
    public int MCC;
    // Mobile Network Code,移动网络号码(中国移动为00,中国联通为01)
    public int MNC;
    // Location Area Code,位置区域码
    public int LAC;
    // Cell Identity,基站编号,是个16位的数据(范围是0到65535)
    public int CID;
}
SItude
/**
 * 存放经纬度信息
 * @author Administrator
 *
 */
public class SItude
{
    //纬度
    public String latitude; 
    //经度
    public String longitude; 
}

 

 

posted @ 2012-06-28 10:11  雪中飞雁  阅读(378)  评论(0)    收藏  举报