0

I'm trying to get a polyline to be drawn on the map to track my location. For some reason, I'm not sure why my method onMyLocationChanged(location) is not being called and isn't drawing the polyline on the map. This is the method along with the lcoationToLatLng(loc) method that is suppose to help draw the polyline on the map that tracks where I go. Why isn't this polyline being drawn? Here is my code:

public class RouteTracking extends FragmentActivity implements View.OnClickListener, OnMyLocationChangeListener{
    private GoogleMap mMap = null;
    private Context context = this;
    private Location lastLocationloc = null;

    @Override
protected void onCreate(Bundle load) { 
    super.onCreate(load);

    setContentView(R.layout.fragment_ride_tracking);

    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.myMapView)).getMap();
    int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context);
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

     //Get the current location
    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, true);
    Location location = locationManager.getLastKnownLocation(provider);

    //Zooms into the current location when the activity is started
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    float zoom = (float) 10.0;
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude, longitude), zoom));      


    if (mMap != null) { 
        mMap.setMyLocationEnabled(true);
    }
   }

   @Override
   public void onMyLocationChange(Location location) { 
       if (lastLocationloc == null) { 
           lastLocationloc = location;
       }
       LatLng lastLatLng = locationToLatLng(lastLocationloc);
       LatLng thisLatLng = locationToLatLng(location);
       mMap.addPolyline(new PolylineOptions().add(lastLatLng).add(thisLatLng).width(3).color(Color.RED));
       lastLocationloc = location;
   }

   public static LatLng locationToLatLng(Location loc) { 
       if (loc != null) 
           return new LatLng(loc.getLatitude(), loc.getLongitude());
       return null;
   }
}
user268397
  • 1,917
  • 7
  • 38
  • 56

2 Answers2

0

I think you should implement LocationListener (implements LocationListener), and then modify the code into :

   @Override
   public void onLocationChanged(Location location) { 
       if (lastLocationloc == null) { 
           lastLocationloc = location;
       }
       LatLng lastLatLng = locationToLatLng(lastLocationloc);
       LatLng thisLatLng = locationToLatLng(location);
       mMap.addPolyline(new PolylineOptions().add(lastLatLng).add(thisLatLng).width(3).color(Color.RED));
       lastLocationloc = location;
   }

give it a try, I hope this solves your problem, Good Luck ^^

reidzeibel
  • 1,622
  • 1
  • 19
  • 24
0

You need to request (register) for updates from the location manager (or at least I am not seeing where you have done so).

    public void init(Context context)
    {
         this.context = context;

         locationManager = (LocationManager) this.context
            .getSystemService(Context.LOCATION_SERVICE);

         // exceptions will be thrown if gps provider is not permitted.
        try
        {
             gpsEnabled = locationManager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);

        } catch (Exception e)
        {
             Log.d("Fail", e.getMessage());
        }

                // ask for gps updates from the device if gps is enabled
        if (gpsEnabled)
        {
           locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,this);
        }
    }
Kaleb
  • 1,855
  • 1
  • 18
  • 24