0

I have an Activity that gets position with fused location and puts it in two text boxes.

In a frame, I have a Google map.

In two text boxes, I have my latitude and longitude.

I want map center to adjust as location changes.

@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
    latitud.setText(String.valueOf(mCurrentLocation.getLatitude()));
    longitud.setText(String.valueOf(mCurrentLocation.getLongitude()));
    tiempo.setText(mLastUpdateTime);


}

My idea was adding to onLocationChanged something like this:

@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
    mLastUpdateTime = DateFormat.getTimeInstance().format(new Date());
    latitud.setText(String.valueOf(mCurrentLocation.getLatitude()));
    longitud.setText(String.valueOf(mCurrentLocation.getLongitude()));
    tiempo.setText(mLastUpdateTime);

    LatLng mapCenter = new LatLng(mCurrentLocation.getLatitude(), mCurrentLocation.getLatitude());

    googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(mapCenter, 16));

    // Flat markers will rotate when the map is rotated,
    // and change perspective when the map is tilted.
    googleMap.addMarker(new MarkerOptions()
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.flecha))
            .position(mapCenter)
            .flat(true)
            .rotation(245));

    CameraPosition cameraPosition = CameraPosition.builder()
            .target(mapCenter)
            .zoom(16)
            .bearing(90)
            .build();

    // Animate the change in camera view over 2 seconds
    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition),
            2000, null);


}

But is giving me the following error:

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

Daniel Viaño
  • 485
  • 1
  • 9
  • 26
  • googleMap variable is null. Be sure you get GoogleMap object. – blackkara May 11 '16 at 22:19
  • I found the solution to my problem here: http://stackoverflow.com/questions/33739971/how-to-show-my-current-location-in-google-map-android-using-google-api-client – Daniel Viaño May 12 '16 at 22:02

0 Answers0