在Android Marshmallow API 23中没有获得经度和纬度

时间:2016-08-25 06:02:26

标签: android latitude-longitude android-6.0-marshmallow android-gps

我遇到了在 Android Marshmallow API 23 中获得价值0.0作为纬度和经度的问题。和其他API工作正常。问题是谷歌地图没有获得当前位置。

 public Location getLocation() {
            try {
                locationManager = (LocationManager) mContext.getSystemService(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;
                    // First get location from Network Provider
                    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();
                            }
                        }
                    }
                    // if GPS Enabled get lat/long using GPS Services
                    if (isGPSEnabled) {
                        if (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();
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return location;
        }
public void stopUsingGPS() {
        if (locationManager != null) {
            locationManager.removeUpdates(GPSTracker.this);
        }
    }
    public double getLatitude() {
        if (location != null) {
            latitude = location.getLatitude();
        }
        // return latitude
        return latitude;
    }
    public double getLongitude() {
        if (location != null) {
            longitude = location.getLongitude();
        }
        // return longitude
        return longitude;
    }
    /**
     * Function to check GPS/wifi enabled
     *
     * @return boolean
     * */
    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    /**
     * Function to show settings alert dialog On pressing Settings button will
     * lauch Settings Options
     * */
    public void showSettingsAlert() {
        final Dialog dialog = new Dialog(mContext,android.R.style.Theme_Translucent_NoTitleBar);
        dialog.setContentView(R.layout.upgrade_now_layout);
        LinearLayout upgradeParent = (LinearLayout) dialog
                .findViewById(R.id.upgradeParent);
        LinearLayout laterParent = (LinearLayout) dialog.findViewById(R.id.laterParent);
        TextView title = (TextView) dialog.findViewById(R.id.title);
        TextView discription = (TextView) dialog.findViewById(R.id.discription);
        TextView yes = (TextView) dialog.findViewById(R.id.yes);
        TextView no = (TextView) dialog.findViewById(R.id.no);
        title.setVisibility(View.GONE);
        discription.setText("  GPS is not enabled. Do you want to go to settings menu?    \n");
        yes.setText("Setting");
        no.setText("Cancel");
        laterParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Splash.again = true;
                dialog.dismiss();
            }
        });
        upgradeParent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // super.onBackPressed();
                Intent intent = new Intent(
                        Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                mContext.startActivity(intent);
                Splash.again = true;
                dialog.dismiss();

            }
        });
        dialog.show();
    }

Android Manifest.xml

 <uses-permission-sdk-23 android:name="android.permission.ACCESS_COARSE_LOCATION"/>
 <uses-permission-sdk-23 android:name="android.permission.ACCESS_FINE_LOCATION"/>

2 个答案:

答案 0 :(得分:1)

您必须在marshmallow Here中实施运行时权限,如果您没有实现此操作,则必须手动启用“设置”权限

检查您从Google控制台帐户

获取的AndroidManifest.xml中的API密钥
  <meta-data
       android:name="com.google.android.maps.v2.API_KEY"
       android:value="YOUR_KEY" />

答案 1 :(得分:0)

Implement run time permission for mashmallow
 if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        checkLocationPermission();
    } else {
        Log.d("onCreate", "Google Play Services available.");
    }
public boolean checkLocationPermission() {
        if (ContextCompat.checkSelfPermission(getActivity(),
                Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

            // Asking user if explanation is needed
            if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(),
                    Manifest.permission.ACCESS_FINE_LOCATION)) {

                // Show an explanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

                //Prompt the user once explanation has been shown
                new AlertDialog.Builder(getActivity())
                        .setTitle("Permission needed")
                        .setMessage("Please allow permission to access your location to continue service")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                //Prompt the user once explanation has been shown
                                ActivityCompat.requestPermissions(getActivity(),
                                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                        MY_PERMISSIONS_REQUEST_LOCATION);
                            }
                        })
                        .create()
                        .show();

            } else {
                // No explanation needed, we can request the permission.
                ActivityCompat.requestPermissions(getActivity(),
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        MY_PERMISSIONS_REQUEST_LOCATION);
            }
            return false;
        } else {
            return true;
        }
    }