title: Getting the location periodically and plotting markers to map
body: <p>I'm looking to create an app in android that basically tracks a phones location and periodically adds a marker to the google maps api so that a route can be displayed, the problem I have is that I don't know how to get the location periodically and in the background. </p>
  
  <p>Here is my code: </p>
  
  <pre><code>public class MapsActivity extends FragmentActivity implements LocationListener {
  
  private GoogleMap mMap; // Might be null if Google Play services APK is not available.
  Button btnCurrent, btnPrevious;
  ArrayList&lt;Double&gt; latitude = new ArrayList&lt;&gt;();
  ArrayList&lt;Double&gt; longitude = new ArrayList&lt;&gt;();
  LocationManager lm;
  LocationListener ll;
  Location networkLocation;
  Location gpsLocation;
  double LONG;
  double LAT;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_maps);
      lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
      networkLocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
      gpsLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  
  
      lm.requestLocationUpdates(lm.GPS_PROVIDER, 10, 0, new android.location.LocationListener() {
          @Override
          public void onLocationChanged(Location location) {
              LONG = networkLocation.getLongitude();
              LAT = networkLocation.getLatitude();
  
              latitude.add(LAT);
              longitude.add(LONG);
  
              Log.d("COORDS", LAT+" , "+LONG);
          }
  
          @Override
          public void onStatusChanged(String provider, int status, Bundle extras) {
  
          }
  
          @Override
          public void onProviderEnabled(String provider) {
  
          }
  
          @Override
          public void onProviderDisabled(String provider) {
  
          }
      });
  
  
      setUpMapIfNeeded();
  
  
  
  
      btnCurrent = (Button)findViewById(R.id.btn_currentLoc);
      btnPrevious = (Button) findViewById(R.id.btn_prevLoc);
  
      btnCurrent.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
              mMap.clear();
              double curLong, curLat;
              String test = lm.getAllProviders().toString();
              LatLng CurrentLoc;
  
              Log.d("PROVIDERS ", test);
  
              if((lm.isProviderEnabled("gps"))==true)
              {
                  curLong = gpsLocation.getLongitude();
                  curLat = gpsLocation.getLatitude();
                  CurrentLoc = new LatLng(curLat,curLong);
                  Log.d("GPS", "true");
              }else
              {
                  curLong = networkLocation.getLongitude();
                  curLat = networkLocation.getLatitude();
                  CurrentLoc = new LatLng(curLat,curLong);
                  Log.d("GPS", "false");
              }
  
  
              mMap.addMarker(new MarkerOptions().position(new LatLng(curLat,curLong)).title("LOCATION "+ curLat + ", " + curLong));
              mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(CurrentLoc,15));
              Log.d("LOCATION", curLat + "," + curLong);
  
  
  
  
          }
      });
  
      btnPrevious.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
  
  
              for(int i = 0;i&lt;latitude.size();i++)
              {
                  mMap.addMarker(new MarkerOptions().position(new LatLng(latitude.get(i),longitude.get(i))).title("LOCATION "+ latitude.get(i) + ", " + longitude.get(i)));
  
                  Log.d("LOCATION", latitude.get(i)+ "," + longitude.get(i));
  
  
              }
  
  
          }
      });
  
  }
  
  @Override
  protected void onResume() {
      super.onResume();
      setUpMapIfNeeded();
  }
  
  
  private void setUpMapIfNeeded() {
      // Do a null check to confirm that we have not already instantiated the map.
      if (mMap == null) {
          // Try to obtain the map from the SupportMapFragment.
          mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                  .getMap();
          // Check if we were successful in obtaining the map.
          if (mMap != null) {
              setUpMap();
          }
      }
  }
  
  
  private void setUpMap() {
  
  
  }
  
  private void getCurrentLocation(){
  
  
  
  
  }
  
  private void seeAllLocations(){
  
  
  
  
  }
  
  
  @Override
  public void onLocationChanged(Location location) {
  
  }
  }
  </code></pre>
  
  <p>Any help would be appreciated.</p>