On Map Long Click listener is not placing a marker on my G Map, can you help me? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of On Map Long Click listener is not placing a marker on my G Map, can you help me? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

Help when I do the long click nothing happens. Also, I get these messages when I fire up the emulator, could it be connected with my problem? Everything else seems to be working in the app.

Here is my code:

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

@Override
public void onMapLongClick(LatLng latLng) {
    Log.i("Map", "CLICKED");
    mMap.addMarker(new MarkerOptions().position(latLng).title("Your new Memorable Place").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));

    Toast.makeText(getApplicationContext(),
            "New marker added@" + latLng.toString(), Toast.LENGTH_LONG)
            .show();
}

LocationManager locationManager;
LocationListener locationListener;
private GoogleMap mMap;

public void centerMapOnLocation (Location location, String title) {

    if (location != null) {
        LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());

        mMap.clear();
        mMap.addMarker(new MarkerOptions().position(userLocation).title(title));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 12));
    }
}

@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);

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, locationListener);
            Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            centerMapOnLocation(lastKnownLocation, "Your Location");
        }
    }
}

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



    Intent intent = getIntent();
    if (intent.getIntExtra("placeNumber", 0) == 0){
        //Zoom in on user location

        locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        locationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                centerMapOnLocation(location, "Your Location");
            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {

            }
        };

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION ) == PackageManager.PERMISSION_GRANTED){
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1, locationListener);
            Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            centerMapOnLocation(lastKnownLocation, "Your Location");
        }else {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }
    }

}

}

Answer

I think you have missed adding the listener to the map.

@Override
public void onMapReady(GoogleMap googleMap){
        mMap=googleMap;
        mMap.setOnMapLongClickListener(this);
        //rest of your code.
}
We are here to answer your question about On Map Long Click listener is not placing a marker on my G Map, can you help me? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji