1

The code works fine first time round, but when locationManager requests a update it doesn't redraw the markers or the polylines when the location changes but the little blue gps circle on it moves by it self and thats it. I want to know how you could get it to change the markers and polylines from the google maps every time the user moves to a new location

public class MyActivity extends FragmentActivity implements RoutingListener {
GoogleMap map;
LatLng User;
LatLng end;
Marker user;
Marker END;



/**
 * This activity loads a map and then displays the route and pushpins on it.
 */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
        if (map == null) {
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().
                    findFragmentById(R.id.map);
            map = fm.getMap();
            if (map != null) {
                setUpMap();
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void setUpMap() {
    map.setMyLocationEnabled(true);

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    Criteria criteria = new Criteria();

    String provider = locationManager.getBestProvider(criteria, true);

    Location myLocation = locationManager.getLastKnownLocation(provider);

    locationManager.requestLocationUpdates(provider, 5000, 0, (android.location.LocationListener) this);
    if(locationManager!=true){
        onLocationChanged(myLocation);
    }

}

private void onLocationChanged(Location myLocation) {
    user.remove();
    END.remove();
    double latitude = myLocation.getLatitude();

    double longitude = myLocation.getLongitude();

    end = new LatLng(51.493873, -3.18175);

    User = new LatLng(latitude, longitude);

    Marker user = map.addMarker(new MarkerOptions().
            position(new LatLng(latitude, longitude)).title("you").icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue)));

    Marker END = map.addMarker(new MarkerOptions().
            position(end).title("Other User").icon(BitmapDescriptorFactory.fromResource(R.drawable.end_green)));

    map.moveCamera((CameraUpdateFactory.newLatLng(User)));
    map.animateCamera(CameraUpdateFactory.zoomTo(15));
    Routing routing = new Routing(Routing.TravelMode.WALKING);
    routing.registerListener(this);
    routing.execute(User,end);
}



@Override
public void onRoutingFailure() {
    // The Routing request failed
}

@Override
public void onRoutingStart() {
    // The Routing Request starts
}

@Override
public void onRoutingSuccess(PolylineOptions mPolyOptions, Route route) {
    PolylineOptions polyOptions = new PolylineOptions();
    polyOptions.color(Color.BLUE);
    polyOptions.width(10);
    polyOptions.addAll(mPolyOptions.getPoints());
    map.addPolyline(polyOptions);
}

}

user2956865
  • 11
  • 1
  • 4
  • Your code looks fine to me, one thing I am wondering is.. if the onLocationChanged got called at all when you change your location? – kaho Apr 27 '15 at 17:52
  • does it not get called automatically when location changes – user2956865 Apr 28 '15 at 01:48
  • huh.. it sounds like a problem regarding to the `requestLocationUpdates`.. which [this](http://stackoverflow.com/questions/14700755/locationmanager-requestlocationupdates-not-working) might be the solution? – kaho Apr 28 '15 at 01:55
  • Sorry about this but how would I implement that then in my code because I'm not sure how that would work – user2956865 Apr 28 '15 at 02:03
  • I basically want the polylines and markers redrawn every 5 seconds when location changes but it's not happening with the code at the moment – user2956865 Apr 28 '15 at 02:11

1 Answers1

0

You are only listening when the user has changed location. If you want redraw it every five seconds regardless if the user has moved.

Using the new GoogleApiClient

private GoogleApiClient mGoogleApiClient;
private static final ScheduledExecutorService delayer = Executors.newSingleThreadScheduledExecutor();


@Override
public void onCreate(Bundle savedInstanceState) {
    mGoogleApiClient = new GoogleApiClient.Builder(this).addApi(LocationServices.API).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
    mGoogleApiClient.connect();
}

@Override
public void onConnected(Bundle bundle) {
    Location location;
    Runnable task = new Runnable() {
        public void run() {
           location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
            // do something with location

        }
    };
    delayer.schedule(task, 5, TimeUnit.SECONDS);
}
AC-OpenSource
  • 346
  • 1
  • 12