有關GPS的一些記錄點

做了一些有關GPS的東西,需要記錄的如下幾點:


1、判斷GPS是否是打開狀態

    public boolean isGpsOn()
    {
        LocationManager alm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        if( alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
        {
            return true;
        }
        else
        {
            return false;
        }
    }


2、通過GPS得到位置

    public void getLocation()
    {
        try
        {
            String serviceName = Context.LOCATION_SERVICE;
            mLocationManager = (LocationManager)getSystemService(serviceName);
            
            Criteria criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE); // 精度低
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(true);
            criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
            
            String provider = mLocationManager.getBestProvider(criteria, true); // 獲取GPS信息
            Location location = mLocationManager.getLastKnownLocation(provider);    // 通過GPS獲取位置
            
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }



3、通過Google地圖來獲取位置和座標

        try
            {
                TelephonyManager tm = (TelephonyManager)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)); // 國家嗎,中國460
                int mnc = Integer.valueOf(tm.getNetworkOperator().substring(3, 5)); // 網絡運營商碼 00 01
                
                // 開始拼裝JSON查詢字符串
                JSONObject holder = null;
                holder = new JSONObject();
                
                holder.put("version", "1.1.0");
                holder.put("host", "maps.google.com");
                holder.put("request_address", true);
                
                JSONArray array = new JSONArray();
                JSONObject data = new JSONObject();
                data.put("cell_id", cid); // 25070
                data.put("location_area_code", lac);// 4474
                data.put("mobile_country_code", mcc);// 460
                data.put("mobile_network_code", mnc);// 0
                array.put(data);
                
                holder.put("cell_towers", array);
                
                String url = "http://www.google.com.hk/loc/json";
                
                DefaultHttpClient client = HttpClientFactory.getInstance();
                HttpPost post = new HttpPost(url);
                
                StringEntity entity = new StringEntity(holder.toString());
                post.setEntity(entity);
                
                HttpResponse response = client.execute(post);
                retCode = response.getStatusLine().getStatusCode();
                
                String result = EntityUtils.toString(response.getEntity());
                Log.i("OBD", "Google 返回結果: " + result);
                
                if(result.equals("{}"))
                {
                    return -1;
                }
                
                // gps = 3; gps緩存=2 cellid=1; google=0 ;文件緩存 -1;優先級
                if( level<0 )
                {
                    JSONObject jsonObject = new JSONObject(result);
                    
                    String location = jsonObject.getString("location");
                    Log.i("OBD", "Google返回的地址爲: " + location);
                    
                    JSONObject jsonJWD = new JSONObject(location);
                    Log.i("OBD", "Google返回的 經度:" + jsonJWD.getString("longitude") + " 緯度: " + jsonJWD.getString("latitude"));
                    
                    longitude = jsonJWD.getString("longitude");
                    latitude = jsonJWD.getString("latitude");
                    Log.i("OBD", "getTaskGoogle " + "longitude " +longitude + " latitude " + latitude);
                    
                    String address = jsonJWD.getString("address");
                    JSONObject jsonAddress = new JSONObject(address);
                    
                    if (address.contains("street_number")) {
                        Log.i("OBD", jsonAddress.get("city") + " "
                                + jsonAddress.get("street") + " "
                                + jsonAddress.get("street_number"));
                        
                        String accuracy = jsonJWD.getString("accuracy");
                        Log.i("OBD", "accuracy: " + accuracy);

                        position = jsonAddress.get("city") + " " + jsonAddress.get("street")
                                + " " + jsonAddress.get("street_number")
                                + " 精確距離: " + accuracy
                                + "米";

                    } else {
                       Log.i("OBD", jsonAddress.get("city") + " "
                               + jsonAddress.get("street"));
                        String accuracy = jsonJWD.getString("accuracy");
                        Log.i("OBD", "accuracy: " + accuracy);

                        position = jsonAddress.get("city") + " " + jsonAddress.get("street")
                                + " 精確距離: " + accuracy
                                + "米";
                    }
                    
                }
                else
                {
                    Log.i("OBD", "已經獲得更高級的定位,忽略掉Google_CellID定位");
                    return -1;
                }                
            }
            catch (UnsupportedEncodingException e) {
                System.out.println(e);
            } catch (ClientProtocolException e) {
                System.out.println(e);
            } catch (ParseException e) {
                System.out.println(e);
            } catch (IOException e) {
                System.out.println(e);
            } catch (Exception e) {
                System.out.println(e);
            }




4、通過Google來進行逆地理解析,即通過上傳經緯度座標,來獲得所代表的具體位置,包括省份、市級、縣級等。

http://maps.googleapis.com/maps/api/geocode/json?latlng=39.983434,116.316547&sensor=true&language=zh-CN

參數latlng是上傳的經緯度座標,注意中間沒有空格,先是緯度後是經度,

參數language是附加的上傳語言類型,因爲在PC上返回的是中文,在模擬器或真機上返回的是英文的,所以加上這個參數。



5、在2.2API下打開GPS

    /**
     * 打開或者關閉GPS開關,原理只是一個變量的兩個狀態,一次開啓一次關閉
     */
    public void openCloseGPS()
    {
        Intent myIntent = new Intent();
        myIntent.setClassName("com.android.settings",
                "com.android.settings.widget.SettingsAppWidgetProvider");
        myIntent.addCategory("android.intent.category.ALTERNATIVE");
        myIntent.setData(Uri.parse("3"));
        sendBroadcast(myIntent);
    }





發佈了36 篇原創文章 · 獲贊 4 · 訪問量 48萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章