6

在过去的两天里,当IllegalArgumentException我试图从地址中获取坐标时,我在 Android 代码中收到一个错误,甚至是反向,从经度和纬度中获取地址时,我都快疯了。这是代码,但我看不到错误。这是一个标准的代码片段,很容易在谷歌搜索中找到。

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 密钥:<uses-library android:name="com.google.android.maps" />

从上面的代码片段中,geocoderis not , ornull也不是,我在这里偶然发现:addressappContextgeocoder.getFromLocationName(strAddress, 5);

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

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

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

4

4 回答 4

14

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

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());
}
于 2011-02-09T20:36:55.097 回答
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
    }
于 2015-04-17T15:18:32.063 回答
2

有同样的问题。池化不起作用,所以我用它来获取位置:地理编码响应

使用此类获取位置的 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);
于 2017-03-16T13:55:50.177 回答
0

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

于 2010-12-31T04:35:50.380 回答