【问题标题】:Not getting latitude and longitude in Android Marshmallow API 23在 Android Marshmallow API 23 中未获取经纬度
【发布时间】:2017-01-01 10:34:21
【问题描述】:

我遇到了在 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"/>

【问题讨论】:

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


    【解决方案1】:

    您必须在 marshmallow Here 中实现运行时权限,如果您没有实现此功能,则必须从设置中手动启用权限

    检查您从 Google 控制台帐户获得的 AndroidManifest.xml 中的 API 密钥

      <meta-data
           android:name="com.google.android.maps.v2.API_KEY"
           android:value="YOUR_KEY" />
    

    【讨论】:

    • 是的,我正在使用这个,但没有得到
    • 您是否从设置中启用了权限?
    • yes.my 代码在 android 其他版本上工作。我收到此错误 23 API。
    • 我的错误是没有得到 latitude 和 longitude 。仅显示 0,0 个。
    • @RAKESHDADHICH 面临同样的情况
    【解决方案2】:
    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;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多