3

I am using this code to get my location and print the coordinates on the screen.

package com.example.alpha;  

import android.app.Activity;  
import android.content.Context;  
import android.location.Location;  
import android.location.LocationListener; 
import android.location.LocationManager;  
import android.os.Bundle;  
import android.widget.Toast;  

public class alpha extends Activity   
{  
    private LocationManager lm;  
    private LocationListener locationListener;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 

        //---use the LocationManager class to obtain GPS locations---
        lm = (LocationManager) 
            getSystemService(Context.LOCATION_SERVICE);    

        locationListener = new MyLocationListener();

        lm.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            0, 
            0, 
            locationListener);   
    }

    public class MyLocationListener implements LocationListener 
    {
       public void onLocationChanged(Location loc) {


           double lat = loc.getLatitude();
           double lon = loc.getLongitude();
           Toast.makeText(getBaseContext(), "Location changed : alphaLat: " + lat + 
                   " alphaLng: " + lon, 
                   Toast.LENGTH_SHORT).show();

        }

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

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

        @Override
        public void onStatusChanged(String provider, int status, 
            Bundle extras) {
            // TODO Auto-generated method stub
        }

    }        

}

The problem is that i want to use the lat and lon values, in order to send them via a stream. Any advice on how to get the coordinates outside the onLocationChanged method?

MByD
  • 135,866
  • 28
  • 264
  • 277
kotsosh
  • 47
  • 1
  • 5

1 Answers1

2

Instead of declaring lat and lon as local variables, declare them as Class level variables. That way you can access these values outside onLocationChanged method.

public class alpha extends Activity
{
         private LocationManager lm;
         private LocationListener locationListener;
         double lat;
         double lon;

  public class MyLocationListener implements LocationListener 
  {
   ...

   lat = loc.getLatitude();
   lon = loc.getLongitude();

   ........
GSree
  • 2,890
  • 1
  • 23
  • 25
  • well wouldn't they still be taking their value inside the method and the value would not pass outside of it? – kotsosh Mar 21 '11 at 23:14
  • @kotsosh did not understand your question. If you want to access the variable outside the class, consider using sharedpreferences or a singleton object. I used singleton object to pass values between my service and activity. – GSree Mar 22 '11 at 00:02