Android 应用程序中的反向地理编码

发布于 2024-09-28 04:37:19 字数 2344 浏览 5 评论 0原文

我正在尝试使用 Geocoder 类将当前的地理编码纬度/经度数据反转为管理区域和子管理区域。 我使用 NETWORK_PROVIDER 作为位置提供程序。这是我使用的代码并且它有效。问题是有时它不给我任何位置,有时却给我任何位置。

我正在使用的 Android 清单权限。

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>

这是代码的其余部分,它确实时不时地给我正确的位置,但其他时候却什么也没有。

            double latPoint = 0;
            double lngPoint = 0;
            List<Address> myList = null;
            getApplicationContext();
            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            Location recentLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (recentLoc != null){
            latPoint = recentLoc.getLatitude();
            lngPoint = recentLoc.getLongitude();

            Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
            if (myLocation != null){
            try {
                 myList = myLocation.getFromLocation(latPoint, lngPoint, 1);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            if (myList.size()> 0){
                        String AdminArea = myList.get(0).getAdminArea();
                        String SubAdminArea = myList.get(0).getSubAdminArea();
                        String Zipcode = myList.get(0).getPostalCode();

                        String urlocation = SubAdminArea + ", " + AdminArea + " " + Zipcode;

                        Context context1 = getApplicationContext(); 
                        int duration1 = Toast.LENGTH_LONG;
                        CharSequence text1 = urlocation;
                        Toast toast1 = Toast.makeText(context1, text1, duration1);
                        toast1.show();
            }
            }

网络提供商对您请求位置数据的频率是否有限制?

我如何确保每次运行都能为我提供有效的位置数据。

预先感谢您对此的任何见解。

PS:我已经在运行 Froyo 2.2 (Verizon Wireless) 的真实设备上运行了此代码。目标 API 级别 8。

这是我从调试中得到的结果。

纬度经度始终会被获取,但 getFromLocation 通常会为传递给它的纬度经度数据返回 null。那么解决方法应该是什么?可以将纬度/经度信息传递给 Google Maps API 进行反向地理编码。将很高兴收到任何意见。

I am trying to Reverse GeoCode current Lat/Long data to just Admin Area and Sub Admin Area using the Geocoder class.
I am using the NETWORK_PROVIDER as the Location provider. Here is the code that I use and it works. The problem is sometimes it does not give me any location sometimes it does.

Android Manifest permissions I am using.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>

Here is the rest of the code that does give me correct location from time to time but nothing at other times.

            double latPoint = 0;
            double lngPoint = 0;
            List<Address> myList = null;
            getApplicationContext();
            LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            Location recentLoc = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (recentLoc != null){
            latPoint = recentLoc.getLatitude();
            lngPoint = recentLoc.getLongitude();

            Geocoder myLocation = new Geocoder(getApplicationContext(), Locale.getDefault());
            if (myLocation != null){
            try {
                 myList = myLocation.getFromLocation(latPoint, lngPoint, 1);
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            if (myList.size()> 0){
                        String AdminArea = myList.get(0).getAdminArea();
                        String SubAdminArea = myList.get(0).getSubAdminArea();
                        String Zipcode = myList.get(0).getPostalCode();

                        String urlocation = SubAdminArea + ", " + AdminArea + " " + Zipcode;

                        Context context1 = getApplicationContext(); 
                        int duration1 = Toast.LENGTH_LONG;
                        CharSequence text1 = urlocation;
                        Toast toast1 = Toast.makeText(context1, text1, duration1);
                        toast1.show();
            }
            }

Are there restrictions with the network providers how frequently you can request location data?

How can I make sure that everytime its run this can give me valid location data.

Thanks in advance for any insight into this.

PS: I have run this code on my real device running Froyo 2.2 (Verizon Wireless). Target API Level 8.

This is what I got from my debugging.

Latitude Longitude is always fetched but the getFromLocation often returns null for the latitude longitude data passed to it. So what should be the workaround? May be pass the Lat/Long info to Google Maps API for reverse geocoding. Will be glad to receive any input.

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

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

发布评论

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

评论(2

征﹌骨岁月お 2024-10-05 04:37:19

嗯,这就是我解决它的方法。

由于 NETWORK_PROVIDER 每次都会提供纬度经度信息,因此我首先获取用户当前位置的纬度经度信息。

latPoint=recentLoc.getLatitude();
lngPoint = recentLoc.getLongitude();

我获取了 Google Maps API 密钥,然后用它对来自网络提供商的纬度、经度数据进行反向地理编码。这每次都会返回所需的地址和邮政编码。

Well this is how I solved it.

Since the NETWORK_PROVIDER provides the latitude longitude information without fail everytime, I fetch that for the user's current location first.

latPoint = recentLoc.getLatitude();
lngPoint = recentLoc.getLongitude();

I obtained the Google Maps API Key and then used it to Reverse Geocode the latitude, longitude data from the Network Provider. This returned the required address and the zipcode everytime without fail.

浮光之海 2024-10-05 04:37:19
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>

    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();
    }
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>

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