【问题标题】:geocoder.getFromLocationName returns only nullgeocoder.getFromLocationName 仅返回 null
【发布时间】:2011-06-01 19:40:09
【问题描述】:

在过去两天里,当我尝试从地址中获取坐标时,我在 Android 代码中收到 IllegalArgumentException 错误,甚至是反向,从经度和纬度中获取地址时,我一直在发疯。这是代码,但我看不到错误。这是一个标准代码 sn-p,在 Google 搜索中很容易找到。

public GeoPoint determineLatLngFromAddress(Context appContext, String strAddress) {
    Geocoder geocoder = new Geocoder(appContext, Locale.getDefault());
    GeoPoint g = null; 
    try {
        System.out.println("str addres: " + strAddress);
        List<Address> addresses = geocoder.getFromLocationName(strAddress, 5);
        if (addresses.size() > 0) {
            g = new GeoPoint(
               (int) (addresses.get(0).getLatitude() * 1E6),
               (int) (addresses.get(0).getLongitude() * 1E6)
            );
        }
    } catch (Exception e) {
        throw new IllegalArgumentException("locationName == null");
    }
    return g;
 }

这些是 manifest.xml 文件中的权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />

我也声明了 Google API 密钥:&lt;uses-library android:name="com.google.android.maps" /&gt;

从上面的代码sn-p,geocoder不是nulladdressappContext也不是,我在这里绊了一下:geocoder.getFromLocationName(strAddress, 5);

我做了很多谷歌搜索,没有找到任何有用的信息,我找到的最重要的信息是:

Geocoder 类需要一个不包含在核心 android 框架中的后端服务。

Sooo,我现在很困惑。我必须调用、导入、添加、在代码中使用什么......才能完成这项工作? 我正在使用 Google API 2.2,API 级别 8。 如果有人找到了解决方案或文档指针,我没有发现的东西,请告诉我们。

【问题讨论】:

    标签: android google-geocoder illegalargumentexception


    【解决方案1】:

    我遇到了类似的问题,发现在我得到结果之前轮询地理编码器是可行的。这是我的做法,到目前为止效果很好。

    try {
        List<Address> geoResults = geocoder.getFromLocationName("<address goes here>", 1);
        while (geoResults.size()==0) {
            geoResults = geocoder.getFromLocationName("<address goes here>", 1);
        }
        if (geoResults.size()>0) {
            Address addr = geoResults.get(0);
            myLocation.setLatitude(addr.getLatitude());
            myLocation.setLongitude(addr.getLongitude());
        }
    } catch (Exception e) {
        System.out.print(e.getMessage());
    }
    

    【讨论】:

    • 我能够修复它。我强制关闭了 Android 地图应用程序。一旦我重新启动了模拟器。效果很好
    • 很好的解决方案 - 我设置了 10 次尝试的阈值,这样它就不会陷入无限循环。如果一切都失败了,我就使用 Maps API:developers.google.com/maps/documentation/geocoding
    • Hay @chris,你的代码工作得很好......我正在将我的 actionBar 上的 SearchView 的地址传递给你上面提到的这个方法。我想知道的是如何像谷歌那样在搜索栏下方显示来自地址的建议。
    • 我尝试了上面的解决方案,出现了很多异常。如果我发布一个新问题,你有什么办法可以给我一些帮助吗?
    • @AliAnsari 如果您还没有找到解决方案,请查看以下链接:1. developer.android.com/reference/android/widget/… 2.developers.google.com/places/android-api
    【解决方案2】:
    public LatLng determineLatLngFromAddress(Context appContext, String strAddress) {
            LatLng latLng = null;
            Geocoder geocoder = new Geocoder(appContext, Locale.getDefault());
            List<Address> geoResults = null;
    
            try {
                geoResults = geocoder.getFromLocationName(strAddress, 1);
                while (geoResults.size()==0) {
                    geoResults = geocoder.getFromLocationName(strAddress, 1);
                }
                if (geoResults.size()>0) {
                    Address addr = geoResults.get(0);
                    latLng = new LatLng(addr.getLatitude(),addr.getLongitude());
                }
            } catch (Exception e) {
                System.out.print(e.getMessage());
            }
    
        return latLng; //LatLng value of address
        }
    

    【讨论】:

    • 第一次,如果没有给出结果,那么它是否会给出可能尝试的结果。?
    【解决方案3】:

    遇到了同样的问题。池化不起作用,所以我用它来获取位置:Geocoding Responses

    使用该类获取位置的JSONObject:

    public class GetLocationDownloadTask extends AsyncTask<String, Void, String> {
    
        @Override
        protected String doInBackground(String... strings) {
    
    
            String result = "";
            URL url;
            HttpURLConnection urlConnection;
            try {
                url = new URL(strings[0]);
                urlConnection = (HttpURLConnection) url.openConnection();
                InputStream is = urlConnection.getInputStream();
                InputStreamReader inputStreamReader = new InputStreamReader(is);
    
                int data = inputStreamReader.read();
                while(data != -1){
                    char curr = (char) data;
                    result += curr;
                    data = inputStreamReader.read();
                }
                return result;
    
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return null;
        }
    
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
    
            if(result != null) {
                try {
                    JSONObject locationObject = new JSONObject(result);
                    JSONObject locationGeo = locationObject.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");
    
    
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
    
        }
    

    您可以像这样创建和运行类实例:

    String link = "https://maps.googleapis.com/maps/api/geocode/json?address=" + addressString + "&key={YOUR_API_KEY}";
    
    GetLocationDownloadTask getLocation = new GetLocationDownloadTask();
    
    getLocation.execute(link);
    

    【讨论】:

      【解决方案4】:

      在 Android 2.0 上运行您的代码,它会工作。我对可能的代码有同样的问题。它在 2.2 中不起作用,不知道为什么

      【讨论】:

      • 我会用Android2.0试试,谢谢。希望它会起作用,如果是的话,会告诉你。新年快乐!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多