My app should display a map in the first fragment. The app is organised by a Navigation Drawer. The map get's displayed without a problem, BUT it doesn't show the default zoom and location I provided.
The first error I got while creating the app was a NullPointerException
. I know how to fix it, so I said that my map-fragment (which is mapFragment
) is not null. The app now started working, but the code I wrote in my method onMapReady
as well as the Button to get the current location and move the camera to this specific location didn't show/didn't work.
I also tried changing the code for the map but I always ended up in the same spot. If I don't write map!=null
I get a NullPointerException
. If I do write it, the app doesn't zoom in properly to the specific location.
In the Mainactivity
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment)
getSupportFragmentManager()
.findFragmentById(R.id.mapFragment);
if(mapFragment != null) {
mapFragment.getMapAsync(this);
}
}
this is were I have to prevent the NullPointerException
(mapFragment)
Here I set the default Location and zoom, which doesn't get applied to the map (while having the mapFragment!= null
):
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION) ==
PackageManager.PERMISSION_GRANTED)
{
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
LatLng Luxembourg = new LatLng(49.611622, 6.131935);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom
(Luxembourg, 14));
}
this is the method to retrieve the user's current location (which also doesn't work while having mapFragment!= null):
@Override
public void onLocationChanged(Location location) {
lastLocation = location;
if(currentUserLocationMarker != null)
{
currentUserLocationMarker.remove();
}
LatLng latLng = new LatLng(location.getLatitude(),
location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("current user location");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker
(BitmapDescriptorFactory.HUE_RED));
currentUserLocationMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomBy(14));
if(googleApiClient != null)
{
LocationServices.FusedLocationApi.removeLocationUpdates
(googleApiClient, this);
}
}
this is the mapFragment itself:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapFragment"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context="com.example.mapwithmarker.MapsMarkerActivity" />
</FrameLayout>
I expect the map to have the default location and zoom I provided as well as the button to move the map to the user's current location.