-1

I've already tried the previously mentioned solutions.

 public class LocationActivity extends FragmentActivity implements
                LocationListener,
                GoogleApiClient.ConnectionCallbacks,
                GoogleApiClient.OnConnectionFailedListener, OnMapReadyCallback {

        private static final String TAG = "LocationActivity";
        private static final long INTERVAL = 1000 * 60 * 1; //1 minute
        private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute
        Button btnFusedLocation;
        TextView tvLocation;
        LocationRequest mLocationRequest;
        GoogleApiClient mGoogleApiClient;
        Location mCurrentLocation;
        String mLastUpdateTime;
        GoogleMap googleMap;

        protected void createLocationRequest() {
            mLocationRequest = new LocationRequest();
            mLocationRequest.setInterval(INTERVAL);
            mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            if (!isGooglePlayServicesAvailable()) {
                finish();
            }
            createLocationRequest();
            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            setContentView(R.layout.activity_location_google_map);
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
            fm.getMapAsync(this);
        }

        @Override
        public void onMapReady(GoogleMap map) {

            map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            map.setMyLocationEnabled(true);
            map.setTrafficEnabled(true);
            map.setIndoorEnabled(true);
            map.setBuildingsEnabled(true);
            map.getUiSettings().setZoomControlsEnabled(true);
        }


        private void addMarker() {
            MarkerOptions options = new MarkerOptions();
            IconGenerator iconFactory = new IconGenerator(this);
            iconFactory.setStyle(IconGenerator.STYLE_PURPLE);
            options.icon(BitmapDescriptorFactory.fromBitmap(iconFactory.makeIcon(mLastUpdateTime)));
            options.anchor(iconFactory.getAnchorU(), iconFactory.getAnchorV());

            LatLng currentLatLng = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude());
            options.position(currentLatLng);
            Marker mapMarker = googleMap.addMarker(options);
            long atTime = mCurrentLocation.getTime();
            mLastUpdateTime = DateFormat.getTimeInstance().format(new Date(atTime));
            mapMarker.setTitle(mLastUpdateTime);
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng,
                    13));
        }

        }
    }

I'm trying to add a marker with label as time at which the latitude and longitude have been measured using the gps. But, when this method is invoked I'm getting an error: addMarker(..) was invoked on null object. The latitude and longitude were shown correctly when run in debug mode. So, apparently the map itself is null. Could someone tell me how to proceed with this issue ? I've given the relevant code. Thanks in advance :)

Jab
  • 119
  • 1
  • 2
  • 17
  • check out here: http://stackoverflow.com/questions/38388282/finding-current-location-of-the-user-in-android/38397092#38397092 – Jaydeep Devda Apr 08 '17 at 11:35
  • It's not clear from your code where and when you are calling your addMarker function. Is it after onMapReady is called? – Ivan Wooll Apr 08 '17 at 11:36
  • @Override public void onLocationChanged(Location location) { mCurrentLocation = location; mLastUpdateTime = DateFormat.getTimeInstance().format(new Date()); addMarker(); } – Jab Apr 08 '17 at 11:39
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Selvin Apr 08 '17 at 11:40
  • Obviously `googleMap` is `null` – Selvin Apr 08 '17 at 11:40
  • Yes, googleMap is null and I'm looking for the problem. As mentioned, I've seen the answers already none of which worked for me. – Jab Apr 08 '17 at 11:44

1 Answers1

0

Initialize googleMap inside onMapReady

googleMap = map;

@Override
    public void onMapReady(GoogleMap map) {
        googleMap = map;
        map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        map.setMyLocationEnabled(true);
        map.setTrafficEnabled(true);
        map.setIndoorEnabled(true);
        map.setBuildingsEnabled(true);
        map.getUiSettings().setZoomControlsEnabled(true);
    }
Kishore Jethava
  • 6,666
  • 5
  • 35
  • 51