zoukankan      html  css  js  c++  java
  • android gps定位LocationManager

    android location provider有: 

        * LocationManager.GPS_PROVIDER:GPS,精度比较高,但是慢而且消耗电力,而且可能因为天气原因或者障碍物而无法获取卫星信息,另外设备可能没有GPS模块; 
        * LocationManager.NETWORK_PROVIDER:通过网络获取定位信息,精度低,耗电少,获取信息速度较快,不依赖GPS模块。 

    这种定位方式取决于服务器,即取决于将基站或WIF节点信息翻译成位置信息的服务器的能力。由于目前大部分Android手机没有安装google官方的location manager库,大陆网络也不允许,即没有服务器来做这个事情,自然该方法基本上没法实现定位。

    只有NETWORK_PROVIDER这种模式才是室内定位可靠的方式,只不过由于大陆的网络,且大部分厂商也不会用google的服务,这种定位方式默认是没法用的。那怎么办?好办,找个替代的服务商就可以了,百度的位置信息sdk就可以解决这个问题。它的基本原理在上面已经提到过了,就是搜集你的wifi节点信息和你的手机基站信息来定位。

     * LocationManager.PASSIVE_PROVIDER:

              被动定位方式,这个意思也比较明显,就是用现成的,当其他应用使用定位更新了定位信息,系统会保存下来,该应用接收到消息后直接读取就可以了。比如如果系统中已经安装了百度地图,高德地图(室内可以实现精确定位),你只要使用它们定位过后,再使用这种方法在你的程序肯定是可以拿到比较精确的定位信息。

    android有一个Criteria类可以直接判断当前适合用gps或者network 并且设置LocationListener监听器实时更新location

    private void getLocalAddress() {
             LocationManager locationManager;
                locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                
                Criteria criteria = new Criteria();
                criteria.setAccuracy(Criteria.ACCURACY_FINE);
                criteria.setAltitudeRequired(false);
                criteria.setBearingRequired(false);
                criteria.setCostAllowed(true);
                criteria.setPowerRequirement(Criteria.POWER_LOW);
                String provider = locationManager.getBestProvider(criteria, true);
                
                Location location = locationManager.getLastKnownLocation(provider);
                
                locationManager.requestLocationUpdates(provider, 2000, 10,
                                locationListener);
                updateWithNewLocation(location);
        }
    
    
    private final LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                updateWithNewLocation(location);
            }
    
            public void onProviderDisabled(String provider) {
                updateWithNewLocation(null);
            }
    
            public void onProviderEnabled(String provider) {
            }
    
            public void onStatusChanged(String provider, int status, Bundle extras) {
            }
        };
    
    
    private void updateWithNewLocation(Location location) {
            String latLongString;
            TextView myLocationText;
            if (location != null) {
                double lat = location.getLatitude();
                double lng = location.getLongitude();
                latLongString = "纬度:" + lat + "
    经度:" + lng;
            } else {
                latLongString = "无法获取地理信息";
            }
            Toast.makeText(SearchActivity.this, latLongString,
                    Toast.LENGTH_LONG).show();
        }
  • 相关阅读:
    【leetcode】1215.Stepping Numbers
    【leetcode】1214.Two Sum BSTs
    【leetcode】1213.Intersection of Three Sorted Arrays
    【leetcode】1210. Minimum Moves to Reach Target with Rotations
    【leetcode】1209. Remove All Adjacent Duplicates in String II
    【leetcode】1208. Get Equal Substrings Within Budget
    【leetcode】1207. Unique Number of Occurrences
    【leetcode】689. Maximum Sum of 3 Non-Overlapping Subarrays
    【leetcode】LCP 3. Programmable Robot
    【leetcode】LCP 1. Guess Numbers
  • 原文地址:https://www.cnblogs.com/l2rf/p/5550079.html
Copyright © 2011-2022 走看看