有关GPS的一些记录点

1 篇文章 0 订阅
1 篇文章 0 订阅

做了一些有关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);
    }





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值