Geocoder.getFromLocationName 不适用于路口地址

Geocoder.getFromLocationName does not work for intersection address

我尝试在我的应用程序中使用 Geocoder class,发现如果地址是两条街道的交叉点,Geocoder.getFromLocationName returns 为 null。前任。 "Quail Ave and Dunford Way, Sunnyvale, CA 94087"。但是,该方法适用于任何其他类型的地址。 在调试会话期间,我可以看到地址参数传递并正确反映,但 "geocodeMatches" returns size(0) 为零。 感谢任何帮助!!

public static List<Address> getGeoCode(@NonNull Context context, @NonNull String address){
    try {
        geocodeMatches = new Geocoder(context).getFromLocationName(address, 1);

        if (!geocodeMatches.isEmpty())
        {
            return geocodeMatches;
        }

    } catch (IOException e) {
        Log.e("ERROR", "Error to retrieve geocode" + e);
        e.printStackTrace();
    }
    return geocodeMatches;
}

GeoCoder 正在处理 NetworkLocationService。在某些情况下,NetworkLocationService 由于某种原因停止工作。我不知道确切的原因。但在这种情况下,您可以从 google api 获取地址。 希望下面的代码对您有所帮助!

public static String GetLocationAddressFromLatLong(Context context, double latitude, double longitude) {
    String finalAddress = "";
    Geocoder geocoder;
    List<Address> addresses = null;
    geocoder = new Geocoder(context.getApplicationContext(), Locale.getDefault());
    try {
        addresses = geocoder.getFromLocation(latitude, longitude, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
    if (addresses != null && addresses.size() > 0) {
        finalAddress = addresses.get(0).getAddressLine(0) + ", " + addresses.get(0).getAddressLine(1);
    } else {
        //Get address from Google api
        try {
            JSONObject jsonObj = getJSONfromURL("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + ","
                    + longitude + "&sensor=true");
            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();
        }
    }
    return finalAddress;
}