反向地理编码不起作用返回 IOException 和 NullPointerException

Posted

技术标签:

【中文标题】反向地理编码不起作用返回 IOException 和 NullPointerException【英文标题】:Reverse geocoding not working returns IOException and NullPointerException 【发布时间】:2018-08-01 17:35:48 【问题描述】:

我得到了纬度和经度,但我无法进行反向地理编码,我看过很多教程,它们工作正常,但我的不是!

这是我的代码:

    if (mLastLocation != null) 
        double latitude = mLastLocation.getLatitude();
        double longitude = mLastLocation.getLongitude();
        txtCoordinates.setText(latitude + " / " + longitude);


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

            String strCompleteAddress= "";
            if (addresses.size() > 0)
            
                for (int i=0; i<addresses.get(0).getMaxAddressLineIndex();i++)
                    strCompleteAddress+= addresses.get(0).getAddressLine(i) + "\n";
            
            Log.i("MyLocTAG => ", strCompleteAddress);
            Toast.makeText(getBaseContext(), strCompleteAddress, Toast.LENGTH_LONG).show();
        
        catch (IOException e) 
            Log.i("MyLocTAG => ", "this is the exception part");
            e.printStackTrace();
        

这是我的 IOException!

W/System.err: java.io.IOException: Timed out waiting for response from server
W/System.err:     at android.location.Geocoder.getFromLocation(Geocoder.java:136)
W/System.err:     at com.example.beultimate.location.MainActivity.displayLocation(MainActivity.java:136)
W/System.err:     at com.example.beultimate.location.MainActivity.onLocationChanged(MainActivity.java:227)
W/System.err:     at com.google.android.gms.internal.zzart$zzb$1.zza(Unknown Source:2)
W/System.err:     at com.google.android.gms.internal.zzart$zzb$1.zzs(Unknown Source:2)
W/System.err:     at com.google.android.gms.internal.zzaaz.zzb(Unknown Source:8)
W/System.err:     at com.google.android.gms.internal.zzaaz$zza.handleMessage(Unknown Source:14)
W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
W/System.err:     at android.os.Looper.loop(Looper.java:164)
W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6494)
W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I/Choreographer: Skipped 300 frames!  The application may be doing too much work on its main thread.

我还有这个代码的另一个版本,其中我得到一个nullpointerexception,因为列表是空的!

【问题讨论】:

Android Geocoder behaves different on some devices的可能重复 【参考方案1】:

有时 Geocoder 类不能返回值,为了克服这个问题, 使用,

        //Get address from Google api
        //Log.e("Else","else");
        try 
            JSONObject jsonObj = getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + ","
                    + longitude + "&sensor=true");// to getting address form latitude and longitude
            String Status = jsonObj.getString("status");
            if (Status.equalsIgnoreCase("OK")) 
                JSONArray Results = jsonObj.getJSONArray("results");
                JSONObject location = Results.getJSONObject(0);
                finalAddress = location.getString("formatted_address");
            
         catch (Exception e) 
            e.printStackTrace();
        

此 URL 返回 JSON 结果。

【讨论】:

【参考方案2】:

试试这样的:

public String getAddress(double lat, double lng) 
    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    try 
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        return obj.getSubLocality();
     catch (Exception e) 
        e.printStackTrace();
        Toast.makeText(this, "Could not get address, please try again later", Toast.LENGTH_SHORT).show();
        return null;
    

希望对你有帮助!

【讨论】:

以上是关于反向地理编码不起作用返回 IOException 和 NullPointerException的主要内容,如果未能解决你的问题,请参考以下文章

反向地理编码不返回邮政编码

对交叉点进行反向地理编码

Android反向地理编码getLocality通常返回null

C#语言,从百度地图反向地理编码api返回的json数据中提取信息

Javascript 中的反向地理编码返回 MissingKeyMap 错误

谷歌地图反向地理编码不返回正确/准确的地址