0

I've got a problem with the mapMapfragment of GoogleMaps in Android.

I used this Method, which have to be override:

@Override
public void onMapReady(GoogleMap googleMap) {

    gm = googleMap;
    gm.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    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) {
        Toast.makeText(getApplicationContext(), getApplicationContext().getResources().getString(R.string.choose_festival_gps_disabled), Toast.LENGTH_SHORT).show();
    } else {
        gm.setMyLocationEnabled(true);
    }
    gm.getUiSettings().setZoomControlsEnabled(true);
    gm.addMarker(new MarkerOptions().position(new LatLng(latitude(), longitude())).title(name()));
    gm.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(latitude(), longitude())));
    gm.animateCamera(CameraUpdateFactory.zoomTo(15));

}

Then I called this one in an Async Task:

((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMapAsync(Activity.this);

Now I got two problems, I've searched for a long time but got no clue how to solve it:

  1. The Map zoom is not exactly to my location, maybe 5 km northern of it.

  2. If I open and close the Activity high frequently, I've got the following Fatal Exception.

    java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.MapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference

Community
  • 1
  • 1
Sven Guthe
  • 15
  • 1
  • 6

1 Answers1

0

1. The Map zoom is not exactly to my location, maybe 5 km northern of it.

You can check on this SO thread. It advised to add a LocationSource to your GoogleMap and respond to onLocationChanged events. You can check the sample code here.

2. If I open and close the Activity high frequently, I've got the following Fatal Exception.

Maybe you are attempting to find a fragment before it exists. Or maybe you're trying to get at the map fragment from inside another fragment in the fragment's onCreateView() method. It was stated in this SO post. You can also see here that you might encounter that error if you are using specific version of Google Play Services but the Gradle build select the most recent version.

Community
  • 1
  • 1
abielita
  • 13,147
  • 2
  • 17
  • 59