Android - 如果关闭位置设置,是否有办法使用网络提供商获取经度和纬度

时间:2016-03-21 17:49:08

标签: android gps network-programming location

我的问题是我的代码没有从GPS获取位置,但始终来自网络提供商

我添加了这两行

Log.i("GPSTracker GPS","lat : " + latitude + ", long : " + longitude);

在我的以下代码中检查我的应用用于获取位置的内容

,而

    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) {
            this.canGetLocation = true;
            if (isGPSEnabled) {
                if (location == null) {
                    if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION)
                            != PackageManager.PERMISSION_GRANTED) {
                        // TODO: Consider calling
                        //    ActivityCompat#requestPermissions
                        // here to request the missing permissions, and then overriding
                        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                        //                                          int[] grantResults)
                        // to handle the case where the user_tab grants the permission. See the documentation
                        // for ActivityCompat#requestPermissions for more details.
                        if (!ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
                                Manifest.permission.ACCESS_FINE_LOCATION)) {

                            ActivityCompat.requestPermissions((Activity) context,
                                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                    PackageManager.PERMISSION_GRANTED);
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                                ActivityCompat.requestPermissions((Activity) context,
                                        new String[]{Manifest.permission.LOCATION_HARDWARE},
                                        PackageManager.PERMISSION_GRANTED);
                            }
                        }
                    }
                    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();
                            Log.i("GPSTracker GPS","lat : " + latitude + ", long : " + longitude);
                        }
                    }
                }
            }
            if (isNetworkEnabled) {

                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.INTERNET)
                        != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user_tab grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    if (!ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
                            Manifest.permission.INTERNET)) {

                        ActivityCompat.requestPermissions((Activity) context,
                                new String[]{Manifest.permission.INTERNET},
                                PackageManager.PERMISSION_GRANTED);
                    }
                    if (!ActivityCompat.shouldShowRequestPermissionRationale((Activity) context,
                            Manifest.permission.ACCESS_COARSE_LOCATION)) {

                        ActivityCompat.requestPermissions((Activity) context,
                                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                                PackageManager.PERMISSION_GRANTED);
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                            ActivityCompat.requestPermissions((Activity) context,
                                    new String[]{Manifest.permission.LOCATION_HARDWARE},
                                    PackageManager.PERMISSION_GRANTED);
                        }
                    }
                }
                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();
                        Log.i("GPSTracker Net","lat : " + latitude + ", long : " + longitude);
                    }
                }
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    locationManager.removeUpdates(this);
    return location;
}
永远不会调用

我的整个代码都在关注

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

我的清单文件是

std::vector<unsigned char> v {1, 2, 3};

std::string s; // result

auto sep = ""; // continuation separator
for(auto n: v)
{
    s += sep + std::to_string(n);
    sep = "-"; // change the continuation separator after first element
}

std::cout << s << '\n';

我希望我的应用在位置开启时从GPS获取位置 如果设置中的位置已关闭 - 如果有方法

,则从网络提供商处获取经度和纬度

2 个答案:

答案 0 :(得分:1)

有两个原因。

1)你的代码以错误的顺序执行。它检查gps,然后检查网络。因此,如果两者都被启用(99%的时间),gps将被覆盖。

2)GPS需要时间。你试图调用getLastKnownLocation,这几乎不会起作用。如果您想要GPS位置,请等待回叫。

旁注:这段代码在多种方式上存在严重缺陷。 GPSTracker严重受损。不要使用它。在http://gabesechansoftware.com/location-tracking/

了解原因

答案 1 :(得分:0)

是,您可以检查设备是否启用了A-GPS。如果启用了A-GPS,则可以 通过A-GPS(辅助GPS)网络进行拉特龙。

您可以使用适合我的以下代码示例:

 public Location getLocation(Context context) {
        location=null;
        try {
            locationManager = (LocationManager) context
                    .getSystemService(Context.LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    Toast.makeText(context,"Please! Provide Location Access Permission.....", Toast.LENGTH_LONG).show();
                    return TODO;
                }
                TrackLogin trackLogin = new DatabaseManager(context).GetTrackLogout();
                // if GPS Enabled get lat/long using GPS Services
                //  if (isGPSEnabled) {
                if (trackLogin.LocationProvider.equals("G")) {
                    if(isGPSEnabled) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                location.setProvider("G");
                               // SyncWithDB(latitude, longitude, "G");
                            }
                        }
                    }
                    if (isNetworkEnabled && location==null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("Network", "Network");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                location.setProvider("A");
                                //SyncWithDB(latitude,longitude,"A");
                            }
                        }
                    }
                }
                if (trackLogin.LocationProvider.equals("A")) {
                    if (isNetworkEnabled) {
                        locationManager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("Network", "Network");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                location.setProvider("A");
                                //SyncWithDB(latitude,longitude,"A");
                            }
                        }
                    }
                    if(isGPSEnabled && location==null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                location.setProvider("G");
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
            new DatabaseManager(context).save_ErrorLog("LocationCls","getLocation()->catch",e.getMessage());
        }

        return location;
    }

注意: 我将GPS网络放在首位。如果无法使用,那我就从A-GPS网络上拉了很久。 希望对您有帮助。