Question

I'm working on an Android app which gets your current location upon clicking a button, and then prints it if you click another button.

Now, I've got all of the code done, yet I'm stuck into how to retrieve the info of the longitude and latitude to print it, because I earn it on another function.

I'll post my code below to explain what I mean in a clearer manner:

package com.example.geolocation;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener, LocationListener {

    private LocationManager locationManager;
    public String provider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button start = (Button)findViewById(R.id.start);
        start.setOnClickListener(this);
        Button stop = (Button)findViewById(R.id.stop);
        stop.setOnClickListener(this);
        Button show = (Button)findViewById(R.id.show);
        show.setOnClickListener(this);
        TextView location = (TextView)findViewById(R.id.location);
    TextView providers = (TextView)findViewById(R.id.providers);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub


        switch(v.getId()){

        //funcions boto start
        case R.id.start:
        locationManager.requestLocationUpdates(
                provider,
                10000, //temps em ms (10s)
                500, //distancia (meters)
                this);
        //mostrar provider 
        TextView location = (TextView)findViewById(R.id.location);
        location.setText("Provider: " + provider);
        break;

        //funcio boto stop
        case R.id.stop:
        locationManager.removeUpdates(this);
        break;

        //funcio boto show
        case R.id.show:
        Location locations = locationManager.getLastKnownLocation(provider);
        locationManager.requestLocationUpdates(
                provider,
                10000, //temps em ms (10s)
                1000, //distancia (meters)
                this);
        //show long & lat
        TextView providers = (TextView)findViewById(R.id.providers);
        providers.setText("Latitude & longitude: " + ""); //com traslladar valor loc aqui?

        break;
        }

    }

    @Override
    public void onLocationChanged(Location loc) {
        // TODO Auto-generated method stub
        int lat = (int) (loc.getLatitude());
        int lng = (int) (loc.getLongitude());
     loc.toString();  
    }

    @Override
    public void onProviderDisabled(String np) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderEnabled(String p) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

}

The segment where I get the desired info is this one:

@Override
    public void onLocationChanged(Location loc) {
        // TODO Auto-generated method stub
        int lat = (int) (loc.getLatitude());
        int lng = (int) (loc.getLongitude());
     loc.toString();  
    }

As you can see, I turn the information into a String.

Now, my problem is: how do I call this info in the main function to print it? In this segment:

//funcio boto show
        case R.id.show:
        Location locations = locationManager.getLastKnownLocation(provider);
        locationManager.requestLocationUpdates(
                provider,
                10000, //temps em ms (10s)
                1000, //distancia (meters)
                this);
        //show long & lat
        TextView providers = (TextView)findViewById(R.id.providers);
        providers.setText("Latitude & longitude: " + ""); //com traslladar valor loc aqui?

        break;
Was it helpful?

Solution

what you have to do is the following :

1st - declare a String to store your lattitude and longitude :

private String  lattitude , longitude; 

2nd - in your get onLocationChanged do the following :

@Override
    public void onLocationChanged(Location loc) {
        // TODO Auto-generated method stub
        int lat = (int) (loc.getLatitude());
        int lng = (int) (loc.getLongitude());
        lattitude = "lattitude = "+ lat ;
        longitude = "longitude = "+ lng;
    }

3rd - use the lattitude and longitude to print them in any place you want .

Hope that helps .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top