0

I am very new to android studio and I am trying to implement a simple mapping activity with high accuracy which tracks the users location. Currently whilst emulating the activity shows a location layer and if a location is pushed to the emulator the marker can be viewed by clicking the locations button but will not follow the marker when a new location is pushed to it. When run on a mobile device there is no locations layer or marker for the user, it simply displays a map.

Here is my code:

public class LiveMapping extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener, OnMapReadyCallback {

private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_live_mapping);
    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);


    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

    mapFragment.getMapAsync(this);
    setUpMap();

    createLocationRequest();
    buildLocationSettingsRequest();
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

protected void buildLocationSettingsRequest() {
    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
            .addLocationRequest(mLocationRequest);
}

@Override
protected void onResume() {
    super.onResume();
    setUpMap();
    mGoogleApiClient.connect();
}

@Override
protected void onPause() {
    super.onPause();
    if (mGoogleApiClient.isConnected()) {
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
        mGoogleApiClient.disconnect();
    }
}

private void setUpMap() {
    if (mMap != null) {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}

public void onConnected(Bundle bundle) {

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
        Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (location == null) {
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
        }
        handleNewLocation(location);
        mMap.setMyLocationEnabled(true);
}

private void handleNewLocation(Location location) {
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    double currentLatitude = location.getLatitude();
    double currentLongitude = location.getLongitude();
    LatLng latLng = new LatLng(currentLatitude, currentLongitude);

    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    MarkerOptions options = new MarkerOptions().position(latLng).title("I am here!");
    mMap.addMarker(options);
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onLocationChanged(Location location) {
    handleNewLocation(location);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
        mMap.setMyLocationEnabled(true);
        MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

}

}

R1994
  • 11
  • You are correct, the previous thread does fix my issue (to a degree). The only thing it doesn't fix is the camera does not follow the user as they move but this is likely a minor tweak. Is it better to delete this thread now as it is almost a repeat? – R1994 Mar 16 '16 at 00:52
  • You can delete if you want, but it would be better to just accept the duplicate proposal. Duplicates are good for making it easier to find answers, and they usually don't get deleted. Cheers! – Daniel Nugent Mar 16 '16 at 03:44
  • As for having the camera and Marker follow a user as they move, remove the call to `removeLocationUpdates()` from onLocationChanged(), so that it keeps the location listener registered. Just be sure to unregister for location updates as soon as possible, as it drains the battery at a faster rate when location listeners are registered. – Daniel Nugent Mar 16 '16 at 03:54
  • I have accepted it as a duplicate and you are correct removing the location updates fixed my other issue. Thanks Daniel – R1994 Mar 16 '16 at 13:13

0 Answers0