-5

I've been looking at the right way to work with the following code which is in my MainActivity and pull the result in to another activity so I can call a URL of 'http://www.website.com?lat:lng:speed:bearing' which will be based on the values pulled through.

locationCheck() sits in my Mainactivity how ever I have a JSONParse activity which I want to use the location found to construct the url. What I do not is how to share the result.

In HTML or PHP I would use a global value.

Although I can call and toast on create I have not found the best way to share the result.

public void locationCheck() {
        MyLocation myLocation = new MyLocation();       
        // this is the result
        myLocation.getLocation(this, locationResult);
    }

    public LocationResult locationResult = new LocationResult() {
        public void gotLocation(final Location location) {
            try {
                double lat = location.getLatitude();
                double lng = location.getLongitude();
                if (lat != 0.0 && lng != 0.0) {

                    String sLat;
                    String sLng;
                    sLat = Double.toString(lat);
                    sLng = Double.toString(lng);
                    String gps_location = sLat + " : " + sLng;
                    Toast.makeText(getBaseContext(),
                            "We got gps location! " + gps_location + "",
                            Toast.LENGTH_LONG).show();
                }
            } catch (Exception e) {

            }
        }
    };
Terran Brown
  • 509
  • 10
  • 25

1 Answers1

0

The following allows me to have a location activity run and post its results as strings in to a globally accessible form which then allows me to use in the format of creating a context aware URL through another activity...

As you can see the toast uses the global value which allowed me to test they where accessible.

In the Mainactivity....

public void locationCheck() {
    MyLocation myLocation = new MyLocation();       
    // this is the result
    myLocation.getLocation(this, locationResult);
}

public LocationResult locationResult = new LocationResult() {
    public void gotLocation(final Location location) {
        try {
            double lat = location.getLatitude();
            double lng = location.getLongitude();
            double bearing = location.getBearing();
            double speed = location.getSpeed();
            if (lat != 0.0 && lng != 0.0) {

                String sLat;
                String sLng;
                String sBearing;
                String sSpeed;  

                sLat = Double.toString(lat);
                sLng = Double.toString(lng);
                sBearing = Double.toString(bearing);
                sSpeed = Double.toString(speed);

                // send to global vars

                Globals.glat = sLat;
                Globals.glng = sLng;
                Globals.gBearing = sBearing;
                Globals.gSpeed = sSpeed;    

                String gps_location = Globals.glat + " : " + Globals.glng;

                @SuppressWarnings("unused")
                String gps_location2 = sBearing + " : " + sSpeed;

                Toast.makeText(getBaseContext(),
                        "We got gps location! " + gps_location + "",
                        Toast.LENGTH_LONG).show();
            }
        } catch (Exception e) {

        }
    }
};

In the Globals Activity

// Global Values
// http://stackoverflow.com/questions/3100726/sharing-global-variables-across-activities-problem-when-using-a-webview
// http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables?rq=1

   public class Globals {

   static String glat; 
   static String glng;
   static String gBearing;
   static String gSpeed;

}
Terran Brown
  • 509
  • 10
  • 25