Shi Xuan
Shi Xuan

Reputation: 5

Geocoder is undefined?

I'm trying to get user location using GPS, but i need to convert the lat and long to address but Geocoder keep giving a error gc is undefined.

public class MyLocationListener implements LocationListener{

            public void onLocationChanged(Location loc){

                try{
                Geocoder gc = new Geocoder(this, Locale.ENGLISH);
                List <Address> addr = gc.getFromLocation(loc.getLatitude(),loc.getLongitude(),1);
                if (addr != null && addr.size() > 0){
                    Address address = addr.get(0);
                    String results = address.getAddressLine(0);
                    Toast.makeText(getApplicationContext(),results,Toast.LENGTH_LONG).show();
                }

                }catch(Exception e){

                }

            }

Upvotes: 0

Views: 472

Answers (1)

Shalini
Shalini

Reputation: 1733

Check your imports have this

import android.location.Geocoder;

And also,

this refer to your locationlistener class. So put your activityname.this instead of this

Geocoder gc = new Geocoder(youractivityname.this, Locale.ENGLISH);

Upvotes: 1

Related Questions