0

I am using google map V2. I want to use geocoderto find the location but getFromLocationName() it is returning me null value and I couldn't find why. Just see my code and suggest me, what should I do?

EditText sLocation = (EditText) v.findViewById(R.id.editText1);
                String location = sLocation.getText().toString();

                List<android.location.Address> addressList= null;

                if (location != null || !location.equals("")) {
                    Geocoder geocoder = new Geocoder(getActivity().getApplicationContext());
                    try {
                        addressList = geocoder.getFromLocationName(location, 1);

                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                    android.location.Address address = addressList.get(0);
                    LatLng latlng = new LatLng(address.getLatitude(), address.getLatitude());
                    gMap.addMarker(new MarkerOptions().position(latlng).title("Marker"));
                    gMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));

                }
nabia saroosh
  • 399
  • 1
  • 14
  • 36
  • 1
    Check this SO question [4567216](http://stackoverflow.com/questions/4567216/geocoder-getfromlocationname-returns-only-null) and [15182853](http://stackoverflow.com/questions/15182853/android-geocoder-getfromlocationname-always-returns-null) if it can help you. – KENdi Sep 12 '16 at 00:08
  • @KENdi, I have solved my problem. Just see my below answer. – nabia saroosh Sep 12 '16 at 18:08

1 Answers1

0

I was getting null value form getFromLocationName(). Because, my addressList was getting "null" value from location. When I declared my EditText in onClick method then getFromLocationName() return me correct value like this:

sButton =(Button) v.findViewById(R.id.generalId);
sButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                EditText sLocation = (EditText)getActivity().findViewById(R.id.editText1);
                location = sLocation.getText().toString();

                List<android.location.Address> addressList= null;

                if (location != null || location.length()>0) {
                    Geocoder geocoder = new Geocoder(getActivity().getApplicationContext());
                    try {
                        addressList = geocoder.getFromLocationName(location, 1);

                    } catch (IOException e) {

                            e.printStackTrace();
                    }

                    android.location.Address address = addressList.get(0);
                    String locality = address.getLocality();
                    Toast.makeText(getActivity(), locality, Toast.LENGTH_LONG).show();
                    double latitude = address.getLatitude();
                    double longitude = address.getLongitude();
                    LatLng latLng = new LatLng(latitude, longitude);
                    gMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
                    gMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));

                }

            }
        });
nabia saroosh
  • 399
  • 1
  • 14
  • 36