reverse geocoding is not working

﹥>﹥吖頭↗ 提交于 2019-12-13 05:19:36

问题


I am writting an application which needs to find the current location. The code is returning lattitude and longitude correctly but doesnt return the real address(reverse geocoding) what could be problem. Someone please help, i am new to android. I am testing on emulator with android 4.0 updateWithNewLocation() is called from onLocationChanged(Location loc) method

         void updateWithNewLocation(Location location)
          {
                    if (location != null) 
                    {
                            double lat = 29.00;//location.getLatitude();
                            double lng = 77.0;//location.getLongitude();
                            longitudeLattitudeString="Lattitude :"+lat+" Longitude :"+lng;


                            Geocoder gc = new Geocoder(this, Locale.getDefault());

                            try 
                            {
                                    List<Address> addresses = gc.getFromLocation(lat, lng, 1);
                                    StringBuilder sb = new StringBuilder();
                                    //Toast.makeText(this, "Problem1", 2000).show();
                                    if (addresses.size() > 0)
                                    {
                                            Address address = addresses.get(0);
                                            for (int i = 0; i < address.getMaxAddressLineIndex(); i++)
                                                sb.append(address.getAddressLine(i)).append("\n");
                                            sb.append(address.getLocality()).append("\n");
                                            Toast.makeText(this, "Problem2", 2000).show();
                                            sb.append(address.getPostalCode()).append("\n");
                                            sb.append(address.getCountryName());
                                    }
                                    else
                                    {
                                        addressString="  No Location";
                                        //Toast.makeText(this, "Problem3", 2000).show();
                                    }
                                    addressString = sb.toString();
                            } 
                            catch (IOException e) 
                            {
                                //Toast.makeText(thisContext, "Problem : InCatch", 2000).show();
                            }
                    }


                    else 
                    {
                            longitudeLattitudeString = "No location found";    

                    }




            }

回答1:


Reverse Geocoding does not work with Emulator, test on device.




回答2:


public static String getUserLocation(String lat, String lon) {
        String userlocation = null;
        String readUserFeed = readUserLocationFeed(lat.trim() + ","+ lon.trim());
        try {
            JSONObject Strjson = new JSONObject(readUserFeed);
            JSONArray jsonArray = new JSONArray(Strjson.getString("results"));
            userlocation = jsonArray.getJSONObject(1)
                    .getString("formatted_address").toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.i("User Location ", userlocation);
        return userlocation;
    }

    public static String readUserLocationFeed(String address) {
        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+ address + "&sensor=false");
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    builder.append(line);
                }
            } else {
                Log.e(ReverseGeocode.class.toString(), "Failed to download file");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }


来源:https://stackoverflow.com/questions/9357555/reverse-geocoding-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!