Joe Raynes
Joe Raynes

Reputation: 11

Android Google Map Location Updates NOT WORKING

My last known location is not updating but with the googlemap.setMyLocationEnable(true) my location is updating. Can anyone help? Here is my code:

private void setUpMap() {

    if(googlemap == null) {

        googlemap = ((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();

        if(googlemap != null) {         
            googlemap.setMyLocationEnabled(true);

            LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);

            String provider = lm.getBestProvider(new Criteria(), true);

            if (provider == null){
                onProviderDisabled(provider);
            }

            lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
            Location loc = lm.getLastKnownLocation(provider);

            if (loc != null) {
                onLocationChanged(loc);
            }

            googlemap.setOnMapLongClickListener(onLongClickMapSettings());
        }
    }
}

private OnMapLongClickListener onLongClickMapSettings() {
    // TODO Auto-generated method stub
    return new OnMapLongClickListener() {

        @Override
        public void onMapLongClick(LatLng arg0) {
            // TODO Auto-generated method stub
            Log.i(arg0.toString(), "User long clicked");
        }
    };
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    LatLng latlng = new LatLng(location.getLatitude(), location.getLongitude());

    googlemap.moveCamera(CameraUpdateFactory.newLatLng(latlng));
    googlemap.animateCamera(CameraUpdateFactory.zoomTo(15));

    Marker ME = googlemap.addMarker(new MarkerOptions()
    .position(latlng)
    .title("Current Location(You)")
    .snippet("Current")
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.user_marker)));

}

Upvotes: 0

Views: 2165

Answers (1)

fasteque
fasteque

Reputation: 4339

I think you're mixing the old and new way of using Google Maps on Android.

If you use the new way (v2, recommended), you don't have to instantiate a LocationManager and then request for location updates with that.

Please check here, on the official documentation, how to do things in the proper way: https://developer.android.com/training/location/receive-location-updates.html

You simply have to implement LocationListener and, of course, when you create your LocationRequest object set the correct parameters based on your needs.

If you're not using the v2 version of the API for Android, I kindly suggest you to do so starting from https://developer.android.com/google/play-services/location.html.

Upvotes: 1

Related Questions