title: Dynamically tracing a route in real time using Google Maps API for Android
body: <p>I am very new to Android development, and I am trying to create an app which will trace a route using a map overlay as I walk.  I have been able to get the map up and running, and can get it to show my movement, but I am lost as to how to show the route taken on the map.  I figure I need to use a polyline to do so, and I have tried to set up a List, and then to draw the line from that List, but I'm not sure if this is the appropriate logic to use.  Any help would definitely be appreciated. </p>
  
  <p>Here is the code I have so far:</p>
  
  <p><strong>ACTIVITY_MAIN_XML</strong>:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:map="http://schemas.android.com/apk/res-auto"
  android:id="@+id/LinearLayout1"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" &gt;
  
  &lt;fragment  
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.MapFragment"
        /&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>MAINACTIVITY.JAVA</strong>:</p>
  
  <pre><code>package com.example.google_maps_test;
  
  import java.util.List;
  
  import com.google.android.gms.common.ConnectionResult;
  import com.google.android.gms.common.GooglePlayServicesClient;
  import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
  import com.google.android.gms.location.LocationClient;
  import com.google.android.gms.location.LocationRequest;
  import com.google.android.gms.maps.CameraUpdate;
  import com.google.android.gms.maps.CameraUpdateFactory;
  import com.google.android.gms.maps.GoogleMap;
  import com.google.android.gms.maps.LocationSource.OnLocationChangedListener;
  import com.google.android.gms.maps.MapFragment;
  import com.google.android.gms.maps.model.LatLng;
  import com.google.android.gms.maps.model.Polyline;
  import com.google.android.gms.maps.model.PolylineOptions;
  
  import android.location.Location;
  import android.location.LocationListener;
  import android.os.Build;
  import android.os.Bundle;
  import android.annotation.SuppressLint;
  import android.annotation.TargetApi;
  import android.app.Activity;
  import android.support.v4.app.FragmentActivity;
  import android.view.Menu;
  import android.widget.Toast;
  
  public class MainActivity extends Activity implements    GooglePlayServicesClient.ConnectionCallbacks, 
  OnConnectionFailedListener, LocationListener{
  
  private OnLocationChangedListener mListener;
  GoogleMap mMap;
  LocationClient mLocationClient;
  Location mCurrentLocation;
  LocationRequest mLocationRequest;
  List&lt;LatLng&gt;routePoints;
  
  @TargetApi(Build.VERSION_CODES.HONEYCOMB)
  @SuppressLint("NewApi")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      mMap =((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
      mMap.setMyLocationEnabled(true);
      mLocationClient = new LocationClient(this,this, this);
      mLocationRequest = LocationRequest.create();
      mLocationRequest.setInterval(1000);
      mLocationRequest.setPriority(mLocationRequest.PRIORITY_HIGH_ACCURACY);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  @Override
  public void onConnected(Bundle dataBundle) {
  
      Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
      Location location = mLocationClient.getLastLocation();
      LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
      routePoints.add(latLng);
      CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17);
      mMap.animateCamera(cameraUpdate);
  
  }
  
  @Override
  public void onDisconnected() {
  
      Toast.makeText(this, "Disconnected. Please re-connect.",
              Toast.LENGTH_SHORT).show();
  
  }
  
  @Override
  public void onConnectionFailed(ConnectionResult connectionResult) {
  
  }
  
  public void onStart(){
      super.onStart();
      mLocationClient.connect();
  }
  
  public void onStop(){
      mLocationClient.disconnect();
      super.onStop();
  }
  
  public void activate(OnLocationChangedListener listener){
      mListener = listener;
  }
  
  public void deactivate(){
      mListener = null;
  }
  
  @Override
  public void onLocationChanged(Location location) {
  
      if(mListener != null){
          mListener.onLocationChanged(location);
      }
  
      LatLng mapPoint = new LatLng(location.getLatitude(), location.getLongitude());
      routePoints.add(mapPoint);
      Polyline route = mMap.addPolyline(new PolylineOptions());
      route.setPoints(routePoints);
  
  
  }   
  @Override
  public void onProviderDisabled(String arg0) {
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void onProviderEnabled(String arg0) {
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
      // TODO Auto-generated method stub
  
  }
  }
  </code></pre>
  
  <p><strong>ANDROIDMANIFEST_XML</strong>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.google_maps_test"
  android:versionCode="1"
  android:versionName="1.0" &gt;
  
  &lt;uses-permission android:name="android.permission.INTERNET"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&gt;
  &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
  &lt;uses-permission     android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/&gt;
  
  &lt;uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="17" /&gt;
  
  &lt;application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" &gt;
      &lt;activity
          android:name="com.example.google_maps_test.MainActivity"
          android:label="@string/app_name" &gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.MAIN" /&gt;
  
              &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
          &lt;/intent-filter&gt;
      &lt;/activity&gt;
      &lt;meta-data
          android:name="com.google.android.maps.v2.API_KEY"
          android:value="AIzaSyCOqSOYCyR-0bji10qcHa1WByfGoW-2ZsU"/&gt;
  &lt;/application&gt;
  
  &lt;uses-feature
      android:glEsVersion="0x00020000"
      android:required="true"/&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p>When I run this app, it crashes due to a Null Pointer Exception in the OnLocationChanged method.  </p>
  
  <p>Thanks in advance for any help!</p>