I'm making an application that contains a map, using the google maps api v2, what the map does is show you your location and the point closest to you, looking at a series of previously saved points.
The problem is that I have tried different devices and it works perfectly in all, except in android 6.0, which simply loads the map without pointing location or anything and does not give any kind of error.
Here is the code.
public class TiendaCercana extends Activity {
protected void onCreate(Bundle savedInstanceState) {
eventMarkerMap = new HashMap<Marker, EventInfo>();
super.onCreate(savedInstanceState);
setContentView(R.layout.mapa_layout);
mapFragment = new MainMapFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.map, mapFragment);
ft.commit();
}
@Override
protected void onStart() {
super.onStart();
setUpEventSpots();
}
private void setUpEventSpots() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mapFragment.getMap().setMyLocationEnabled(true);
myLocation = getLastKnownLocation();
}
LocationManager mLocationManager;
private Location getLastKnownLocation() {
mLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
android.Manifest.permission.ACCESS_FINE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}
}
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
bestLocation = l;
}
}
return bestLocation;
}
}
And this is the fragment of the map
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainMapFragment extends MapFragment {
public Marker placeMarker(EventInfo eventInfo) {
Marker m = getMap().addMarker(new MarkerOptions()
.position(eventInfo.getLatLong())
.title(eventInfo.getNomCom()));
return m;
}
}