0

I am using these two threads:

How to find postcode by latitude and logitude for UK

Get current location using GPS in Android

and have successfully managed to implement a GPStracker, however, when I try to recieve the postcode using the first thread it does not give me any post:

I have this as global variable:

public String receivePostCode = "";

and then in onCreate I have this:

Geocoder geoCoder = new Geocoder(getActivity().getApplicationContext(), Locale.getDefault());
        List<Address> address = null;

        if (geoCoder != null) {
            try {
                address = geoCoder.getFromLocation(latitude, longitude, 1);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            if (address.size() > 0) {
                receivePostCode = address.get(0).getPostalCode();

            }

        }

These are the global variables that I am using to recieve lat and long from my GPStracker class:

GPSTracker gps = new GPSTracker(getActivity());
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();

Any help would be nice.

Community
  • 1
  • 1
Henry
  • 1,042
  • 2
  • 17
  • 47

1 Answers1

1

You have to initialise the variables inside onCreate or onCreateView, they shouldn't be initialised globally, try this, in onCreate:

  gps = new GPSTracker(getActivity());
  latitude = gps.getLatitude();
  longitude = gps.getLongitude();

and put this as global:

GPSTracker gps;
double latitude;
double longitude;