0

In my application I want current location. I am getting latitude and longitude succesfully. Now I want to complete address from that lat long. Here is my code:

double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();

        // \n is for new line
        Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();    

        System.out.println("$$$$$$"+latitude+":::"+longitude);

        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(this, Locale.getDefault());
        try {
            addresses = geocoder.getFromLocation(latitude, longitude, 1);

            String address = addresses.get(0).getAddressLine(0);
            String city = addresses.get(0).getAddressLine(1);
            String country = addresses.get(0).getAddressLine(2);                
            String fullAdd = address+":"+city;      

            sendSMS(IncomingCallReceiver.phonenumber,fullAdd);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            String fullAdd = "Latitude:"+latitude+",Longitude:"+longitude;              

            sendSMS(IncomingCallReceiver.phonenumber,fullAdd);
        }

Now what I want is if suppose I am not get complete address then I want to try 5 more times to get aomplete address and if then also not get location then only I want to send latitude and longitude.

Any suggestion will be appreciated.

Here is my logcat:

04-26 14:54:35.359: W/System.err(11976): java.io.IOException: Unable to parse response from    server
04-26 14:54:35.359: W/System.err(11976):    at     android.location.Geocoder.getFromLocation(Geocoder.java:136)
04-26 14:54:35.359: W/System.err(11976):    at com.example.locationtracker.LocationService.getLocation(LocationService.java:88)
04-26 14:54:35.359: W/System.err(11976):    at com.example.locationtracker.LocationService.onStart(LocationService.java:45)
04-26 14:54:35.359: W/System.err(11976):    at android.app.Service.onStartCommand(Service.java:428)
04-26 14:54:35.359: W/System.err(11976):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2043)
04-26 14:54:35.359: W/System.err(11976):    at android.app.ActivityThread.access$2800(ActivityThread.java:117)
04-26 14:54:35.359: W/System.err(11976):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998)
04-26 14:54:35.359: W/System.err(11976):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 14:54:35.359: W/System.err(11976):    at android.os.Looper.loop(Looper.java:130)
04-26 14:54:35.359: W/System.err(11976):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-26 14:54:35.359: W/System.err(11976):    at java.lang.reflect.Method.invokeNative(Native Method)
04-26 14:54:35.359: W/System.err(11976):    at java.lang.reflect.Method.invoke(Method.java:507)
04-26 14:54:35.359: W/System.err(11976):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-26 14:54:35.359: W/System.err(11976):    at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-26 14:54:35.359: W/System.err(11976):    at dalvik.system.NativeStart.main(Native Method)
PPD
  • 5,420
  • 11
  • 48
  • 86
  • post stack trace. and logs – njzk2 Apr 26 '13 at 09:23
  • @njzk2, actually what I want is I want to call getFromLocation 10 times if I got exception. In my question I had added logcat. – PPD Apr 26 '13 at 09:33
  • if you get an exception once, i'd guess you'll have an exception the second time as well, if you call the same service with the same parameters – njzk2 Apr 26 '13 at 09:37
  • I thought exception is because of network problem. So it may possible after some seconds network is available and I will get proper location.in this post accepted ans is something like I am looking for- http://stackoverflow.com/questions/5222164/geocoder-getfromlocation-throws-exception – PPD Apr 26 '13 at 09:42

0 Answers0