38

説明:を使用しています。タッチした場所Google maps API V2に実装しました。Android Reverse Geocoding

問題: 例外がスローされる

 try {
     addresses = geocoder.getFromLocation(latitude, longitude,1);}
 catch (IOException e)
     {
     e.printStackTrace();
     if(AppConstants.DEBUG)Log.v(AppConstants.DEBUG_TAG," e.printStackTrace(): "+e.getMessage());
     }

私は受け取ってlatitudeおり、longitude値は正しいですが、なぜそれがスローされるのか理解できず、exceptionGoogle検索も行いましたが、役に立ちませんでした.

誰か詳しく説明してくれませんか??

4

3 に答える 3

86

このAndroid issue 38009 - Geocoder throwing exception: IOException: Service not Availableに記載されているように 、デバイスを再起動すると問題が解決します

于 2013-04-20T11:30:12.577 に答える
16

デバイスを再起動したくない場合は、これを使用します

     public  JSONObject getLocationFormGoogle(String placesName) {

    HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +placesName+"&ka&sensor=false");
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {

        e.printStackTrace();
    }

    return jsonObject;
}

public  LatLng getLatLng(JSONObject jsonObject) {

    Double lon = new Double(0);
    Double lat = new Double(0);

    try {

        lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lng");

        lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
            .getJSONObject("geometry").getJSONObject("location")
            .getDouble("lat");

    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return new LatLng(lat,lon);

}



LatLng Source =getLatLng(getLocationFormGoogle(placesName));
于 2014-02-27T07:17:05.420 に答える
10

@Ani ソリューションを変更して、緯度経度パラメーターから都市名を取得しました。

public static String getLocationCityName( double lat, double lon ){
    JSONObject result = getLocationFormGoogle(lat + "," + lon );
    return getCityAddress(result);
}

protected static JSONObject getLocationFormGoogle(String placesName) {

    String apiRequest = "https://maps.googleapis.com/maps/api/geocode/json?latlng=" + placesName; //+ "&ka&sensor=false"
    HttpGet httpGet = new HttpGet(apiRequest);
    HttpClient client = new DefaultHttpClient();
    HttpResponse response;
    StringBuilder stringBuilder = new StringBuilder();

    try {
        response = client.execute(httpGet);
        HttpEntity entity = response.getEntity();
        InputStream stream = entity.getContent();
        int b;
        while ((b = stream.read()) != -1) {
            stringBuilder.append((char) b);
        }
    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject = new JSONObject(stringBuilder.toString());
    } catch (JSONException e) {

        e.printStackTrace();
    }

    return jsonObject;
}

protected static String getCityAddress( JSONObject result ){
    if( result.has("results") ){
        try {
            JSONArray array = result.getJSONArray("results");
            if( array.length() > 0 ){
                JSONObject place = array.getJSONObject(0);
                JSONArray components = place.getJSONArray("address_components");
                for( int i = 0 ; i < components.length() ; i++ ){
                    JSONObject component = components.getJSONObject(i);
                    JSONArray types = component.getJSONArray("types");
                    for( int j = 0 ; j < types.length() ; j ++ ){
                        if( types.getString(j).equals("locality") ){
                            return component.getString("long_name");
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    return null;
}
于 2014-09-22T12:11:43.793 に答える