Eric Belisle Giddings
Eric Belisle Giddings

Reputation: 471

Adding New Marker onLongMapClick Using Android Studio and Google Maps API

I have been trying for hours to add a new marker onLongMapClick in the code below. I've searched a lot but nothing seems to be working. I'm using Android Studio 1.5.1 and Google Maps API. I'm testing my app on a Nexus 6P emulator. API 23. I left it blank between the brackets at the bottom where I think the code is supposed to go. The code below is in public class BasicMapDemoActivity extends AppCompatActivity implements OnMapReadyCallback { and I can't seem to figure out how to do it. Can anyone please help me make this happen? I can provide what ever other information might be useful. Thank you!

GoogleMap mMap;
private UiSettings mUiSettings;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.basic_demo);
    SupportMapFragment mapFragment =
            (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle item selection
    if (item.getItemId() == R.id.menu_legal) {
        startActivity(new Intent(this, LegalInfoActivity.class));
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onMapReady(GoogleMap map) {

    mMap = map;
    mUiSettings = mMap.getUiSettings();
    mUiSettings.setZoomControlsEnabled(true);
    mUiSettings.setScrollGesturesEnabled(true);
    mUiSettings.setZoomGesturesEnabled(true);


mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng point) {
    }
});


mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
    @Override
    public void onMapLongClick(LatLng point) {
    }
});

mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
    @Override
    public void onCameraChange(CameraPosition position) {
        float maxZoom = 7.0f;
        if (position.zoom > maxZoom) {
            mMap.animateCamera(CameraUpdateFactory.zoomTo(maxZoom));
        }
    }
});
}

}

Upvotes: 0

Views: 4226

Answers (2)

Mohmed Ezz
Mohmed Ezz

Reputation: 11

you should add a marker in the onMapLongClick brackets like this

        mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
            @Override
            public void onMapLongClick(LatLng point) {
                MarkerOptions options = new MarkerOptions()
                        .position(point);
                Marker mMarker = mMap.addMarker(options);
            }
        });

Upvotes: 1

iSandeep
iSandeep

Reputation: 715

As you already have the reference of mMap, so try this code (Note: I haven't tried it.)

LatLng latlng= new LatLng(LATITUDE,LONGITUDE);
mMap.setMyLocationEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 13));
mMap.addMarker(new MarkerOptions()
   .title("NAME THE MARKER POINT")
   .snippet("DESCRIPTION FOR MARKER HERE.")
   .position(latlng));

Upvotes: 1

Related Questions