0

I am trying to implement OnMapLongClickListener to my map application so that whenever a user long-clicks on the screen, a marker appears in the corresponding location.

My MainActivity class is as follows:

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMapLongClickListener {

    private GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
      mMap.setOnMapLongClickListener(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Hatfield and move the camera
        LatLng hatfield = new LatLng(51.7471060, -0.22978);
        mMap.addMarker(new MarkerOptions()
                .position(hatfield)
                .title("Hertfordshire Times")
                .snippet("Hertfordshire Lecturer wins Nobel Prize")
                .draggable(true));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(hatfield));

        mMap.addCircle(new CircleOptions()
                .center(hatfield)
                .fillColor(0x5500ff00)
                .strokeColor(Color.WHITE).radius(60));
    }

    @Override
    public void onMapLongClick(final LatLng latLng) {
        if(mMap != null) {
            Toast.makeText(this, "Long press", Toast.LENGTH_LONG).show();
            mMap.addMarker(new MarkerOptions()
            .position(latLng)
            .title("You placed a marker here")
            .icon(BitmapDescriptorFactory.defaultMarker()));
        }
    }
}

Once the OnMapLongClick method runs, it throws a NullPointerException (a snippet of the error is shown below):

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void     com.google.android.gms.maps.GoogleMap.setOnMapLongClickListener(com.google.android.gms.maps.GoogleMap$OnMapLongClickListener)' on a null object reference
                                                                                           at com.example.paulmbw.map_application_fyp.MapsActivity.onCreate(MapsActivity.java:29)

Any idea what I'm doing wrong? I have seen similar answers on Stack but cannot seem to find a solution for my problem. Any help will be greatly appreciated.

Thanks.

Rami
  • 7,879
  • 12
  • 36
  • 66
archon101
  • 1
  • 1
  • 2

3 Answers3

0

Call mMap.setOnMapLongClickListener(this); inside onMapReady().

mMap is null when you're calling that on it because the statement right before it is an asynchronous one. It doesn't wait for your callback onMapReady() to get complete or anything like that. It calls it when its ready instead.

razzledazzle
  • 6,900
  • 4
  • 26
  • 37
0

Because mMap is null (not yet ready).

Move your listener in onMapReady() method.

@Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Hatfield and move the camera
        LatLng hatfield = new LatLng(51.7471060, -0.22978);
        mMap.addMarker(new MarkerOptions()
                .position(hatfield)
                .title("Hertfordshire Times")
                .snippet("Hertfordshire Lecturer wins Nobel Prize")
                .draggable(true));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(hatfield));

        mMap.addCircle(new CircleOptions()
                .center(hatfield)
                .fillColor(0x5500ff00)
                .strokeColor(Color.WHITE).radius(60));

        mMap.setOnMapLongClickListener(MapsActivity.this);
    }
Rami
  • 7,879
  • 12
  • 36
  • 66
0

To Perform On Map Click

private GoogleMap map;
private Marker marker;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
ActionBarActivity activity = (ActionBarActivity) getActivity();
activity.getSupportActionBar().setTitle(R.string.select_location);
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.map, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
  Log.d("Map","On view created");
map = getMap();
        map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
map.setOnMapClickListener(new OnMapClickListener() {

    @Override
    public void onMapClick(LatLng point) {
        Log.d("Map","Map clicked");
        marker.remove();
        drawMarker(point);
    }
});

You can see the more in detail For Google Map and Location Manager Visit : http://www.studygtu.com/2016/02/google-map-implement-and-location.html

  • This is a **bad** idea, and might not solve the problem -- 1) *getMap()* doesn't guarantee to return a non-null object. -- 2) *getMap()* is deprecated. – Rami Feb 07 '16 at 16:32