how to add multiple markers by touching or by tapping in googlemap in android studio
here is my main activity
- import android.Manifest;
- import android.annotation.SuppressLint;
- import android.content.SharedPreferences;
- import android.content.pm.PackageManager;
- import android.location.Location;
- import android.os.Build;
- import android.support.v4.app.ActivityCompat;
- import android.support.v4.app.FragmentActivity;
- import android.os.Bundle;
- import android.support.v4.content.ContextCompat;
- import android.widget.Toast;
- import com.google.android.gms.common.ConnectionResult;
- import com.google.android.gms.common.api.GoogleApiClient;
- import com.google.android.gms.location.LocationListener;
- import com.google.android.gms.location.LocationRequest;
- import com.google.android.gms.location.LocationServices;
- import com.google.android.gms.maps.CameraUpdateFactory;
- import com.google.android.gms.maps.GoogleMap;
- import com.google.android.gms.maps.MapFragment;
- 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.LatLng;
- import com.google.android.gms.maps.model.Marker;
- import com.google.android.gms.maps.model.MarkerOptions;
- public class MainActivity extends FragmentActivity implements OnMapReadyCallback,
- GoogleApiClient.ConnectionCallbacks,
- GoogleApiClient.OnConnectionFailedListener,
- LocationListener {
- private GoogleMap mMap;
- GoogleApiClient mGoogleApiClient;
- Location mLastLocation;
- Marker mCurrLocationMarker;
- SharedPreferences sharedPreferences;
- MapFragment mapFragment;
- int touch;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
- .findFragmentById(R.id.map);
- mapFragment.getMapAsync(this);
- }
- @Override
- public void onMapReady(GoogleMap googleMap) {
- mMap = googleMap;
- mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
- mMap.getUiSettings().setRotateGesturesEnabled(false);
- if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- if (ContextCompat.checkSelfPermission(this,
- Manifest.permission.ACCESS_FINE_LOCATION)
- == PackageManager.PERMISSION_GRANTED) {
- buildGoogleApiClient();
- mMap.setMyLocationEnabled(true);
- }
- }
- else {
- buildGoogleApiClient();
- mMap.setMyLocationEnabled(true);
- }
- }
- protected synchronized void buildGoogleApiClient() {
- mGoogleApiClient = new GoogleApiClient.Builder(this)
- .addConnectionCallbacks(this)
- .addOnConnectionFailedListener(this)
- .addApi(LocationServices.API)
- .build();
- mGoogleApiClient.connect();
- }
- @Override
- public void onConnected(Bundle bundle) {
- @SuppressLint("RestrictedApi") LocationRequest locationRequest = new LocationRequest();
- locationRequest.setInterval(1000);
- locationRequest.setFastestInterval(1000);
- locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
- if (ContextCompat.checkSelfPermission(this,
- Manifest.permission.ACCESS_FINE_LOCATION)
- == PackageManager.PERMISSION_GRANTED) {
- LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
- }
- }
- @Override
- public void onConnectionSuspended(int i) {
- }
- @Override
- public void onLocationChanged(Location location) {
- mLastLocation = location;
- if (mCurrLocationMarker != null) {
- mCurrLocationMarker.remove();
- }
- LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
- MarkerOptions markerOptions = new MarkerOptions();
- markerOptions.position(latLng);
- markerOptions.title("Current Position");
- markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
- mCurrLocationMarker = mMap.addMarker(markerOptions);
- mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
- mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
- if (mGoogleApiClient != null) {
- LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
- }
- }
- @Override
- public void onConnectionFailed(ConnectionResult connectionResult) {
- }
- }
Hari MurugesanPosted Oct 18, 2019, 12:07 AM