0

我创建了示例应用程序,我想从字符串地址行获取纬度和经度。这是我用来获取纬度和经度的方法

private void getFromLocation(String address) {
    Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
    try {
        List<Address> addresses = geoCoder.getFromLocationName(address, 1);

        if (addresses.size() > 0) {
            GeoPoint p = new GeoPoint(
                    (int) (addresses.get(0).getLatitude() * 1E6),
                    (int) (addresses.get(0).getLongitude() * 1E6));

            latitude = p.getLatitudeE6() / 1E6;
            longtitude = p.getLongitudeE6() / 1E6;
            HAMBURG = new LatLng(latitude, longtitude);
            Log.d("latitide", "" + latitude);
            Log.d("longtitude", "" + longtitude);

        } else {
            Log.d("Addess", "Address not found");
        }
    } catch (Exception ee) {
        Log.d("ex", ee.toString());
    }
}

此方法抛出异常“ java.io.IOException: Unable to parse response from server ”。我犯了什么错误吗?有没有其他解决方案来获得纬度和经度?

4

2 回答 2

0

An IOException is likely to indicate that the remote request failed for some relatively low-level reason;

IOException: if the network is unavailable or any other I/O problem occurs


public List<Address> getFromLocationName (String locationName, int maxResults)

Added in API level 1 Returns an array of Addresses that are known to describe the named location, which may be a place name such as "Dalvik, Iceland", an address such as "1600 Amphitheatre Parkway, Mountain View, CA", an airport code such as "SFO", etc.. The returned addresses will be localized for the locale provided to this class's constructor.

The query will block and returned values will be obtained by means of a network lookup. The results are a best guess and are not guaranteed to be meaningful or correct. It may be useful to call this method from a thread separate from your primary UI thread.

Parameters

locationName: a user-supplied description of a location

maxResults: max number of results to return. Smaller numbers (1 to 5) are recommended

Returns:

a list of Address objects. Returns null or empty list if no matches were found or there is no backend service available.

Throws

IllegalArgumentException: if locationName is null

IOException: if the network is unavailable or any other I/O problem occurs


So, Do

Please try to avoid several hits for address in a minute. Run this code on different device.

for more detail check this artical.

于 2013-04-22T06:49:00.593 回答
0
     List<Address> foundGeocode = null;
   /* find the addresses  by using getFromLocationName() method with the given address*/
   try {
    foundGeocode = new Geocoder(MainActivity.this).getFromLocationName("Your location",1);
     Double s=foundGeocode.get(0).getLatitude(); //getting latitude
       Double n=foundGeocode.get(0).getLongitude();
        } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
        }
于 2013-04-22T06:57:03.157 回答