Я страдаю от проблемы получения значения 0.0 по широте и долготе в Android Marshmallow API 23. и другие API работают отлично. Проблема заключается в карте Google, которая не получает текущее местоположение.Не получить широту и долготу в Android Marshmallow API 23
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"/>
<мета-данные андроид: имя = "com.google.android.maps.v2.API_KEY" андроид: значение = "YOUR_KEY" /> да я использовать это, но не получаю –
У вас включить разрешение из настроек? –
есть.my код работающий на android др. Версия. Я получаю эту ошибку 23 API. –