google map in android

Apr 3 2018 8:03 AM
how to add multiple markers by touching or by tapping in googlemap in android studio
here is my main activity
  1. import android.Manifest;  
  2. import android.annotation.SuppressLint;  
  3. import android.content.SharedPreferences;  
  4. import android.content.pm.PackageManager;  
  5. import android.location.Location;  
  6. import android.os.Build;  
  7. import android.support.v4.app.ActivityCompat;  
  8. import android.support.v4.app.FragmentActivity;  
  9. import android.os.Bundle;  
  10. import android.support.v4.content.ContextCompat;  
  11. import android.widget.Toast;  
  12.   
  13. import com.google.android.gms.common.ConnectionResult;  
  14. import com.google.android.gms.common.api.GoogleApiClient;  
  15. import com.google.android.gms.location.LocationListener;  
  16. import com.google.android.gms.location.LocationRequest;  
  17. import com.google.android.gms.location.LocationServices;  
  18. import com.google.android.gms.maps.CameraUpdateFactory;  
  19. import com.google.android.gms.maps.GoogleMap;  
  20. import com.google.android.gms.maps.MapFragment;  
  21. import com.google.android.gms.maps.OnMapReadyCallback;  
  22. import com.google.android.gms.maps.SupportMapFragment;  
  23. import com.google.android.gms.maps.model.BitmapDescriptorFactory;  
  24. import com.google.android.gms.maps.model.LatLng;  
  25. import com.google.android.gms.maps.model.Marker;  
  26. import com.google.android.gms.maps.model.MarkerOptions;  
  27.   
  28. public class MainActivity extends FragmentActivity implements OnMapReadyCallback,  
  29.         GoogleApiClient.ConnectionCallbacks,  
  30.         GoogleApiClient.OnConnectionFailedListener,  
  31.         LocationListener {  
  32.   
  33.  private GoogleMap mMap;  
  34.     GoogleApiClient mGoogleApiClient;  
  35.     Location mLastLocation;  
  36.     Marker mCurrLocationMarker;  
  37.     SharedPreferences sharedPreferences;  
  38.     MapFragment mapFragment;  
  39.  int touch;  
  40.  @Override  
  41.  protected void onCreate(Bundle savedInstanceState) {  
  42.  super.onCreate(savedInstanceState);  
  43.         setContentView(R.layout.activity_main);  
  44.   
  45.   
  46.         SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()  
  47.                 .findFragmentById(R.id.map);  
  48.         mapFragment.getMapAsync(this);  
  49.   
  50.     }  
  51.  @Override  
  52.  public void onMapReady(GoogleMap googleMap) {  
  53.  mMap = googleMap;  
  54.  mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);  
  55.  mMap.getUiSettings().setRotateGesturesEnabled(false);  
  56.   
  57.   
  58.  if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
  59.  if (ContextCompat.checkSelfPermission(this,  
  60.                     Manifest.permission.ACCESS_FINE_LOCATION)  
  61.                     == PackageManager.PERMISSION_GRANTED) {  
  62.                 buildGoogleApiClient();  
  63.  mMap.setMyLocationEnabled(true);  
  64.             }  
  65.         }  
  66.  else {  
  67.             buildGoogleApiClient();  
  68.  mMap.setMyLocationEnabled(true);  
  69.         }  
  70.     }  
  71.   
  72.  protected synchronized void buildGoogleApiClient() {  
  73.  mGoogleApiClient = new GoogleApiClient.Builder(this)  
  74.                 .addConnectionCallbacks(this)  
  75.                 .addOnConnectionFailedListener(this)  
  76.                 .addApi(LocationServices.API)  
  77.                 .build();  
  78.  mGoogleApiClient.connect();  
  79.     }  
  80.   
  81.  @Override  
  82.  public void onConnected(Bundle bundle) {  
  83.   
  84.  @SuppressLint("RestrictedApi") LocationRequest locationRequest = new LocationRequest();  
  85.         locationRequest.setInterval(1000);  
  86.         locationRequest.setFastestInterval(1000);  
  87.         locationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);  
  88.  if (ContextCompat.checkSelfPermission(this,  
  89.                 Manifest.permission.ACCESS_FINE_LOCATION)  
  90.                 == PackageManager.PERMISSION_GRANTED) {  
  91.             LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);  
  92.         }  
  93.     }  
  94.  @Override  
  95.  public void onConnectionSuspended(int i) {  
  96.     }  
  97.  @Override  
  98.  public void onLocationChanged(Location location) {  
  99.   
  100.  mLastLocation = location;  
  101.  if (mCurrLocationMarker != null) {  
  102.  mCurrLocationMarker.remove();  
  103.         }  
  104.             LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());  
  105.             MarkerOptions markerOptions = new MarkerOptions();  
  106.             markerOptions.position(latLng);  
  107.             markerOptions.title("Current Position");  
  108.             markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));  
  109.  mCurrLocationMarker = mMap.addMarker(markerOptions);  
  110.   
  111.  mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));  
  112.  mMap.animateCamera(CameraUpdateFactory.zoomTo(11));  
  113.  if (mGoogleApiClient != null) {  
  114.  LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);  
  115.             }  
  116.         }  
  117.  @Override  
  118.  public void onConnectionFailed(ConnectionResult connectionResult) {  
  119.   
  120.     }  
  121. }

Answers (1)