SaPires
SaPires

Reputation: 153

Convert coordinates to address with geocoder

I've been trying to convert the value of coordinates from a TextView to Toast with an address, but get an error in the line marked with ->. The code:

public void test() throws IOException {
    TextView inicioLat = (TextView) findViewById(R.id.eInicioLat);
    double tmp1 = Double.parseDouble(inicioLat.getText().toString());
    TextView inicioLong = (TextView) findViewById(R.id.eInicioLong);
    double tmp2 = Double.parseDouble(inicioLong.getText().toString());
 -> Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> addresses;
    addresses = geocoder.getFromLocation(tmp1, tmp2, 1);
    Toast.makeText(this, (CharSequence) addresses,Toast.LENGTH_LONG).show();
}

Stack trace:

04-19 09:41:26.303  31020-31020/? E/Zygote﹕ MountEmulatedStorage()
04-19 09:41:26.303  31020-31020/? E/Zygote﹕ v2
04-19 09:41:26.323  31020-31020/? E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
04-19 09:41:30.493  31020-31020/greetrack.estg.ipvc.greentrack E/ViewRootImpl﹕ sendUserActionEvent() mView == null
04-19 09:41:35.003  31020-31020/greetrack.estg.ipvc.greentrack E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: greetrack.estg.ipvc.greentrack, PID: 31020
    java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:4221)
            at android.view.View.performClick(View.java:5155)
            at android.view.View$PerformClick.run(View.java:20747)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4216)
            at android.view.View.performClick(View.java:5155)
            at android.view.View$PerformClick.run(View.java:20747)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
     Caused by: java.lang.IllegalArgumentException: latitude == 225.0
            at android.location.Geocoder.getFromLocation(Geocoder.java:126)
            at greetrack.estg.ipvc.greentrack.Adicionar.test(Adicionar.java:96)
            at greetrack.estg.ipvc.greentrack.Adicionar.locInicio(Adicionar.java:60)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at android.view.View$1.onClick(View.java:4216)
            at android.view.View.performClick(View.java:5155)
            at android.view.View$PerformClick.run(View.java:20747)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:5832)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)

I have the permissions in the manifest already created.

Upvotes: 0

Views: 663

Answers (1)

Exception
Exception

Reputation: 2323

You are getting

java.lang.IllegalArgumentException: latitude == 225.0

It is because getFromLocation() does not take latitude and longitude below -90 or over 90 and below -180 or over 180 respectively.

Take a look at the google documentation here.

Divide your value 10^6(1M) or anything higher based on the precision you want.

Upvotes: 3

Related Questions