0

Being very new to java, I am very confused about getting lastknownlocation and updated location.

Over last few days, I have managed to get the mark on a predefined location, but I am struggling very much on getting my current location using fusedlocationproviderclient.

I will be grateful if somebody kindly help me on getting this. I am using :

compile 'com.google.android.gms:play-services-maps:11.8.0'
compile 'com.google.android.gms:play-services-location:11.8.0'

Though not for the updated location, below is the code for marker at a fixed position

public class SecondFragment extends Fragment implements OnMapReadyCallback {

    private GoogleMap mMap;

    public GoogleMap getMap() {
    return mMap;
    }
    public static SecondFragment newInstance() {
        SecondFragment fragment = new SecondFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_maps, null, false);

        SupportMapFragment mapFragment = (SupportMapFragment) this.getChildFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        return view;
    }


    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        // Add a marker in Sydney and move the camera
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }
}
BaRud
  • 3,055
  • 7
  • 41
  • 89
  • 1
    Check [this](https://github.com/googlesamples/android-play-location/tree/master/LocationUpdates) and [this](https://developer.android.com/training/location/receive-location-updates.html). – Soham Feb 14 '18 at 10:07
  • @Soham: Thanks for your comment. That was my starting point. Unfortunately, I was unable to convert the activity to fragment. Thats the problem. I need to call the map to a pageradapter. – BaRud Feb 14 '18 at 10:11
  • @soham: can you kindly help me a bit in fixing this? – BaRud Feb 14 '18 at 10:16
  • Can you specify more in which part you are getting stuck.Are you not getting reference or what – Soham Feb 14 '18 at 11:45
  • I was basically trying out the https://github.com/googlesamples/android-play-location/tree/master/BasicLocationSample . But, I cant convert the activity to a fragment. I understand its silly beginner's problem, but thats where I stuck. – BaRud Feb 14 '18 at 11:51

1 Answers1

1

I am using this to get my location:

 LocationManager locationManager = (LocationManager)getActivity().getSystemService(getContext().LOCATION_SERVICE);
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
                Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

and listener for this:

 LocationListener locationListener = new LocationListener() {
                    @Override
                    public void onLocationChanged(Location location) {
                        Log.e("osm", "onLocationChanged: "+location.getLongitude() );
                    }

                    @Override
                    public void onStatusChanged(String s, int i, Bundle bundle) {

                    }

                    @Override
                    public void onProviderEnabled(String s) {

                    }

                    @Override
                    public void onProviderDisabled(String s) {

                    }
                };

Do not forget write to manifest this:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

and runtime permission for new version of android(6 and up) :

String[] permission = new String[]{Manifest.permission.CALL_PHONE,Manifest.permission.ACCESS_FINE_LOCATION};
        ActivityCompat.requestPermissions(this,permission,PERMISSON_CODE);`
Soham
  • 4,397
  • 11
  • 43
  • 71
Loki
  • 54
  • 6