How to get location data from a class in maps activity in android?

164 views Asked by At

I have been implementing FusedLocationProvider. However i did achieve and get the lat and long in an single activity, but what i am trying now is to implement it in separate non-activity class GetLocationActivity and get the location data in my Maps_activity class, when i click the Button, But i am unable to get data!

Is there anyway to get the data from the class and shows it in my maps activity? I also tried sharedPref for saving data in a class but no result.

P.S : I think the GetLocationActivity did not run or i am unable to instantiate it.

Here is the code

Map_activity.java

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private GoogleApiClient mGoogleApiClient;
    private FloatingActionButton floatingActionButton;
    private static final String LIST_FRAGMENT_TAG = "list_fragment";
    private static final String TAG = "list_fragment";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
        floatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        final GetLocationActivity obj = new GetLocationActivity();
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                obj.GetLocation();

            }
        });
    }

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

        // Add a marker in Karachi and move the camera
        LatLng sydney = new LatLng(24.9217531, 66.8943302);
        //mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Karachi"));
        mMap.animateCamera(CameraUpdateFactory.zoomTo(10.75f));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();

    }

GetLocationActivity.java

public class GetLocationActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener , com.google.android.gms.location.LocationListener {

    public final static String TAG = "Application";
    private GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;
    private LocationRequest mLocationRequest;

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

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


    }

    public void GetLocation()
    {
        try {

            mLocationRequest = LocationRequest.create();
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setInterval(10000); // Update location every second

            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

            mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                    mGoogleApiClient);
            if (mLastLocation != null) {


                Log.d(TAG, "onConnected: "+String.valueOf(mLastLocation.getLatitude()));
                Log.d(TAG, "onConnected: "+String.valueOf(mLastLocation.getLongitude()));

            }
        } catch (SecurityException e) {

        }

    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
            GetLocation();
    }

    @Override
    public void onConnectionSuspended(int i) {
        Toast.makeText(this, "onConnectionSuspended: " + String.valueOf(i),
                Toast.LENGTH_LONG).show();
    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
        Toast.makeText(this, "onConnectionFailed: " + connectionResult.toString(),
                Toast.LENGTH_LONG).show();
    }

    protected void onStart() {
        mGoogleApiClient.connect();
        super.onStart();
    }

    protected void onStop() {
        mGoogleApiClient.disconnect();
        super.onStop();
    }

    @Override
    public void onLocationChanged(Location location) {

    }
1

There are 1 answers

0
j.elmer On

try the GetLocation() using static

public static void getLocation() {
    ...
}

access that function like this

floatingActionButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        GetLocationActivity.getLocation();
    }
});

as far as I know, we don't have to access it using instance object, we can access it with static object. CMIIW.