title: how to move marker on google map in android based on locations comes from api
body: <p>i want to put another marker which will display the position of person which is continuously moving. i want to update marker as the person moves on google map, i have tried from <a href="https://stackoverflow.com/a/33885379/5324829">https://stackoverflow.com/a/33885379/5324829</a> link but i'm unable to animate marker on predefined positions, can anyone help me please to solve this? thanks in advance, here is my code..</p>
  
  <pre><code>    GoogleMap mGoogleMap;
      SupportMapFragment mapFrag;
      LocationRequest mLocationRequest;
      GoogleApiClient mGoogleApiClient;
      Location mLastLocation;
      Marker mCurrLocationMarker;
      boolean isFirstTime = true;
      private ArrayList&lt;LatLng&gt; markerPoints;
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  //        getSupportActionBar().setTitle("Map Location Activity");
          //check for internet
  
          mapFrag = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
          mapFrag.getMapAsync(this);
  
          markerPoints = new ArrayList&lt;LatLng&gt;();
  
  
          //call web service to fetch location
           //on response call direction api
          //
      }
  
      @Override
      public void onPause() {
          super.onPause();
  
          //stop location updates when Activity is no longer active
          if (mGoogleApiClient != null) {
              LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
          }
      }
  
      @Override
      public void onMapReady(GoogleMap googleMap)
      {
          mGoogleMap=googleMap;
          mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
  
  
  
          //Initialize Google Play Services
          if (android.os.Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
              if (ContextCompat.checkSelfPermission(this,
                      Manifest.permission.ACCESS_FINE_LOCATION)
                      == PackageManager.PERMISSION_GRANTED) {
                  //Location Permission already granted
                  buildGoogleApiClient();
                  mGoogleMap.setMyLocationEnabled(true);
  
                  mGoogleMap.setOnMyLocationButtonClickListener(new GoogleMap.OnMyLocationButtonClickListener() {
                      @Override
                      public boolean onMyLocationButtonClick() {
                          LatLng latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
                          CameraUpdate center =
                                  CameraUpdateFactory.newLatLng(latLng);
                          CameraUpdate zoom = CameraUpdateFactory.zoomTo(15.0f);
  
                          mGoogleMap.moveCamera(center);
                          mGoogleMap.animateCamera(zoom);
                          return false;
                      }
                  });
  
              } else {
                  //Request Location Permission
                  checkLocationPermission();
              }
          }
          else {
              buildGoogleApiClient();
              mGoogleMap.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) {
          mLocationRequest = new LocationRequest();
          mLocationRequest.setInterval(1000);
          mLocationRequest.setFastestInterval(1000);
          mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
          if (ContextCompat.checkSelfPermission(this,
                  Manifest.permission.ACCESS_FINE_LOCATION)
                  == PackageManager.PERMISSION_GRANTED) {
              LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
          }
      }
  
      @Override
      public void onConnectionSuspended(int i) {}
  
      @Override
      public void onConnectionFailed(ConnectionResult connectionResult) {}
  
      @Override
      public void onLocationChanged(Location location)
      {
          mLastLocation = location;
          if (mCurrLocationMarker != null) {
              mCurrLocationMarker.remove();
          }
  
          /*this condition is to set the map to the current location only for the first time,
          after onLocationChanged() method is called every second so it will move us to current location every second*/
          if (isFirstTime) {
              //Place current location marker
              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 = mGoogleMap.addMarker(markerOptions);
              */
  
              CameraUpdate center =
                      CameraUpdateFactory.newLatLng(latLng);
              CameraUpdate zoom = CameraUpdateFactory.zoomTo(16.0f);
  
              mGoogleMap.moveCamera(center);
              mGoogleMap.animateCamera(zoom);
              //move map camera
              // mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,15.0f));
              isFirstTime = false;
              markerPoints.add(new LatLng(23.0515539,72.5158113));
              markerPoints.add(new LatLng(23.0504289,72.5153713));
              markerPoints.add(new LatLng(23.0491435,72.5159617));
              markerPoints.add(new LatLng(23.0482305,72.5155917));
              markerPoints.add(new LatLng(23.0473917,72.5154952));
  
              Bitmap Icon = BitmapFactory.decodeResource(getResources(), R.mipmap.bus);
              setAnimation(mGoogleMap,markerPoints,Icon);
          }
  
  
      }
  
      public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
      private void checkLocationPermission() {
          if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                  != PackageManager.PERMISSION_GRANTED) {
  
              // Should we show an explanation?
              if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                      Manifest.permission.ACCESS_FINE_LOCATION)) {
  
                  // Show an explanation to the user *asynchronously* -- don't block
                  // this thread waiting for the user's response! After the user
                  // sees the explanation, try again to request the permission.
                  new AlertDialog.Builder(this)
                          .setTitle("Location Permission Needed")
                          .setMessage("This app needs the Location permission, please accept to use location functionality")
                          .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialogInterface, int i) {
                                  //Prompt the user once explanation has been shown
                                  ActivityCompat.requestPermissions(MainActivity.this,
                                          new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                                          MY_PERMISSIONS_REQUEST_LOCATION );
                              }
                          })
                          .create()
                          .show();
  
  
              } else {
                  // No explanation needed, we can request the permission.
                  ActivityCompat.requestPermissions(this,
                          new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                          MY_PERMISSIONS_REQUEST_LOCATION );
              }
          }
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode,
                                             String permissions[], int[] grantResults) {
          switch (requestCode) {
              case MY_PERMISSIONS_REQUEST_LOCATION: {
                  // If request is cancelled, the result arrays are empty.
                  if (grantResults.length &gt; 0
                          &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  
                      // permission was granted, yay! Do the
                      // location-related task you need to do.
                      if (ContextCompat.checkSelfPermission(this,
                              Manifest.permission.ACCESS_FINE_LOCATION)
                              == PackageManager.PERMISSION_GRANTED) {
  
                          if (mGoogleApiClient == null) {
                              buildGoogleApiClient();
                          }
                          mGoogleMap.setMyLocationEnabled(true);
                      }
  
                  } else {
  
                      // permission denied, boo! Disable the
                      // functionality that depends on this permission.
                      Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
                  }
                  return;
              }
  
              // other 'case' lines to check for other
              // permissions this app might request
          }
      }
  
  
  
      public static void setAnimation(GoogleMap myMap, final List&lt;LatLng&gt; directionPoint, final Bitmap bitmap) {
  
  
          Marker marker = myMap.addMarker(new MarkerOptions()
                  .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
                  .position(directionPoint.get(0))
                  .flat(true));
  
          myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 16));
  
          animateMarker(myMap, marker, directionPoint, false);
      }
  
  
      private static void animateMarker(GoogleMap myMap, final Marker marker, final List&lt;LatLng&gt; directionPoint,
                                        final boolean hideMarker) {
          final Handler handler = new Handler();
          final long start = SystemClock.uptimeMillis();
          Projection proj = myMap.getProjection();
          final long duration = 30000;
  
          final Interpolator interpolator = new LinearInterpolator();
  
          handler.post(new Runnable() {
              int i = 0;
  
              @Override
              public void run() {
                  long elapsed = SystemClock.uptimeMillis() - start;
                  float t = interpolator.getInterpolation((float) elapsed
                          / duration);
                  if (i &lt; directionPoint.size())
                      marker.setPosition(directionPoint.get(i));
                  i++;
  
  
                  if (t &lt; 1.0) {
                      // Post again 16ms later.
                      handler.postDelayed(this, 5);
                  } else {
                      if (hideMarker) {
                          marker.setVisible(false);
                      } else {
                          marker.setVisible(true);
                      }
                  }
              }
          });
      }
  </code></pre>