【问题标题】:How to get the mode of Location when using FusedLocationApi in Android在Android中使用FusedLocationApi时如何获取Location的模式
【发布时间】:2016-01-05 06:19:57
【问题描述】:

我正在使用以下代码来获取用户的位置。

LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);

这会返回一个android.location.Location 对象,我可以成功获取纬度和经度,但是当我尝试获取模式时,它给了我“融合”。

而我想知道获取位置的方式是使用 GPS 还是网络。

【问题讨论】:

    标签: java android


    【解决方案1】:

    如果您想检查 gps 和网络提供商,请使用以下代码。我认为使用融合 api 我们无法区分它

    public Location getLocation() {
        try {
            locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
    
            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    
            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    
            if(!isGPSEnabled && !isNetworkEnabled) {
    
            } else {
                this.canGetLocation = true;
    
                if (isNetworkEnabled) {
    
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
    
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    
                        if (location != null) {
    
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
    
                }
    
                if(isGPSEnabled) {
                    if(location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        if(locationManager != null) {
                            location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    
                            if(location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }
    

    【讨论】:

      【解决方案2】:

      您可以确定使用的准确性。如果精度高,则来自 GPS。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-26
        • 2017-05-14
        • 1970-01-01
        • 2016-04-24
        • 2016-04-11
        • 2019-06-08
        相关资源
        最近更新 更多