0

I'm trying to get the difference between 2 locations of which i have taken from the gps, Whenever i click on a button to go to my MapsActivity i get crashed with this error

Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference

And When i comment the code which checks difference in distance it works just fine, i don't understand the connection between the Marker options and the DistanceTo Here's my Code (Keep in mind that all works fine it's just the DistanceTo which doesn't work)

locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            //get the latitude and longitude from the location
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
            //get the location name from latitude and longitude
            Geocoder geocoder = new Geocoder(getApplicationContext());
            try {
                if(location != null) 
// this gives address list from the maps, not important and commented

                {
                    List<Address> addresses =
                            geocoder.getFromLocation(latitude, longitude, 1);
// Marker options where the issue occurs
                    //String result = addresses.get(0).getSubLocality()+":";
                    //result += addresses.get(0).getLocality()+":";
                    // result += addresses.get(0).getCountryCode();
                    LatLng latLng = new LatLng(latitude, longitude);
                    mMap.addMarker(new MarkerOptions().position(latLng).title("Marker Title"));
                    mMap.setMaxZoomPreference(20);
                    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
//Where it checks for the locations and the distance between them
            if(LocationA == null) {
                LocationA = location;
            }

            LocationB = location;


            LocationA = LocationB;

            DiffernceInDistance = LocationA.distanceTo(LocationB);

        }

Code where i made the Map

  public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
        if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {


            buildGoogleApiClient();
            mMap.setMyLocationEnabled(true);

        }
        else {
            buildGoogleApiClient();
            mMap.setMyLocationEnabled(true);


        }
}
captindfru
  • 233
  • 1
  • 3
  • 13

1 Answers1

0

The issue was that i was asking for locations and also applying a marker at the same time, so markers couldn't load to the location, to solve it I commented, It doesn't crash anymore but it didn't solve my main issue (DistanceTo not working but that's not the reason i made this post therefore i won't ask for anymore help thanks )

mMap.addMarker(new MarkerOptions().position(latLng).title("Marker Title"));
captindfru
  • 233
  • 1
  • 3
  • 13
  • It is possible to get location updates _before_ your map is ready so you need to account for this. –  Apr 22 '18 at 13:37