1

我正在尝试使用 Geocoder 类将 GeoCode 当前的 Lat/Long 数据反转为 Admin Area 和 Sub Admin Area。我使用 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 以进行反向地理编码。很高兴收到任何意见。

4

2 回答 2

1
<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();
    }
于 2012-05-09T12:11:02.600 回答
1

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

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

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

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

于 2010-10-20T15:35:20.440 回答