Geocoder.getFromLocationName() throws “Service not available” exception on Android 4 with Google Maps V2

后端 未结 2 1261
迷失自我
迷失自我 2021-01-16 12:17

The method Geocoder.getFromLocationName() throws the exception Service not available on Android 4.1, even if GooglePlayServicesUtil.isGoogleP

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-16 13:04

    try this .....

          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));
    

提交回复
热议问题