0

Here I am using this code in order to get the latitude and longitude of the location but all that I am getting is latitude 0.0 and longitude 0.0 the code to find the latitude and logitude that I am using is given as below , Also I am testing this code in Genymotion emulator and all that is printed in the log is 0.0 0.0 because the if(address.size()>0) is not getting satisfyied can anyone tell where am I going wrong or is it because of emulator.

Geocoder geo = new Geocoder(getActivity());
            try {
                List<Address> address = geo.getFromLocationName(str_ELocation,1);
                if (address.size() > 0) {
                    Log.d("List Size : ", "" + address.size());
                    latitude = address.get(0).getLatitude();
                    longitude = address.get(0).getLongitude();
                } else {
                    Log.d("In Else", "In Else");
                }
            } catch (IOException e) {
                // TODO: handle exception
                e.printStackTrace();
            }

            Log.d("LONGITUDE AND LATITUDE", "" + longitude + "" + latitude);

Also is there any other simpler solution to just find latitude and longitude. I am using this in Fragment and so I am using getActivity() here instead of myClassName.this.

Gaurav A Dubey
  • 641
  • 1
  • 6
  • 19

1 Answers1

0

I'm guessing you don't have a backend service you are using to obtain this information.

Note The Docs say

The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.

This should also be done on a background Thread if not already being done.

Alternative

You may be better off using LocationManager

Be sure to read those docs on it before beginning to have an understanding of how it works and should be implemented (That portion is short).

Here is an example that can get you started

Anything like this should be done on a background Thread and not on the UI Thread. And there are listeners which can return the values you need.

full disclosure: I haven't used GPS but I read through the docs ;)

Community
  • 1
  • 1
codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Sure not an issue at least you tried thanks for that and yes will try the same with location manager. Thanks codeMagic :).\ – Gaurav A Dubey Apr 16 '14 at 15:17
  • Which part is not an issue? I don't see anywhere that you are requesting results. The `Geocoder` class itself does not already have the info. It has to retrieve it from a backend service. – codeMagic Apr 16 '14 at 15:23
  • Yep and in the exception it was displaying that service not available when I was running it in the Samsung Star Duos – Gaurav A Dubey Apr 16 '14 at 17:59
  • @GauravADubey then that's the problem. Use `LocationManager` instead. – codeMagic Apr 16 '14 at 19:18
  • Yep Roger That.. Its getting too late so will be trying it tomorrow with another live device.. with LocationManager. Will update you soon. – Gaurav A Dubey Apr 16 '14 at 19:28