在我的应用程序中,我想要当前位置。我成功地获得了纬度和经度。现在我想从那个地方完成地址。这是我的代码:
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);
}
现在我想要的是,如果假设我没有获得完整的地址,那么我想再尝试 5 次以获得完整的地址,如果也没有获得位置,那么我只想发送纬度和经度。
任何建议将不胜感激。
这是我的日志:
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)