Error google maps api v2

2

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;
}

}
    
asked by Alex B 05.04.2016 в 00:29
source

2 answers

2

Call the method in onStart() the following method:

 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();
}

This method is the one that reviews the permissions in Android 6.0 and activates the map in case they are enabled:

mapFragment.getMap().setMyLocationEnabled(true);
    
answered by 06.04.2016 в 08:54
0

Request permissions in onStart , since this is being called to execute before getLastKnownLocation , so the code:

mapFragment.getMap().setMyLocationEnabled(true);

myLocation = getLastKnownLocation();

is never being executed because if you do not have the permissions do a return.

eh implements onRequestPermissionsResult , for in case the user accepts the permission actives the location with:

mapFragment.getMap().setMyLocationEnabled(true);
myLocation = getLastKnownLocation();
    
answered by 30.04.2016 в 02:09