【问题标题】:reverse geocoding is not working反向地理编码不起作用
【发布时间】:2012-02-20 07:25:31
【问题描述】:

我正在编写一个需要查找当前位置的应用程序。代码正确返回纬度和经度,但不返回真实地址(反向地理编码) 可能是什么问题。 有人请帮忙,我是android新手。 我正在使用 android 4.0 在模拟器上进行测试 updateWithNewLocation() 从 onLocationChanged(Location loc) 方法调用

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

                    }




            }

【问题讨论】:

    标签: android geocoding


    【解决方案1】:

    反向地理编码不适用于模拟器,在设备上测试。

    【讨论】:

    • 它确实可以在带有 google play 服务的标准模拟器上运行,可能帖子已经过时了。
    【解决方案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();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 2018-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多