反向地理编码不起作用

发布于 2025-01-07 05:02:35 字数 2536 浏览 5 评论 0原文

我正在编写一个需要找到当前位置的应用程序。该代码正确返回纬度和经度,但不返回真实地址(反向地理编码) 可能有什么问题。 有人请帮忙,我是安卓新手。 我正在使用 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";    

                    }




            }

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

                    }




            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南薇 2025-01-14 05:02:36
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();
    }
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();
    }
咽泪装欢 2025-01-14 05:02:35

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文