Android:我是在进行客户端还是服务器端地理编码?

标签 android google-maps google-geocoder

所以在我的应用程序中,我正在使用 google map api,并且我正在使用地理编码来根据用户的当前位置确定地址。我使用的是 Geocoder Android 类,但我发现它确实非常有效。这只是不可靠。所以我使用了我在 SO 上看到的一篇文章来创建我自己的 Geocoder。问题是,我现在不知道我是在使用服务器端还是客户端地理编码。这一点很重要,因为一个有限制而另一个没有。不过我的所有代码都在 Android 中。

这是一些代码,在我的“MyGeocoder”类中:

public List<Address> getFromLocation(double latitude, double longitude,
        int maxResults) throws IOException, LimitExceededException {
    if (latitude < -90.0 || latitude > 90.0) {
        throw new IllegalArgumentException("latitude == " + latitude);
    }
    if (longitude < -180.0 || longitude > 180.0) {
        throw new IllegalArgumentException("longitude == " + longitude);
    }

    if (isLimitExceeded(context)) {
        throw new LimitExceededException();
    }

    final List<Address> results = new ArrayList<Address>();

    final StringBuilder url = new StringBuilder(
            "http://maps.googleapis.com/maps/api/geocode/json?sensor=true&latlng=");
    url.append(latitude);
    url.append(',');
    url.append(longitude);
    url.append("&language=");
    url.append(Locale.getDefault().getLanguage());

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(url.toString());

    try {
        HttpResponse response = httpclient.execute(httppost);
        String jsonResult = inputStreamToString(
                response.getEntity().getContent()).toString();

        Gson gson = new Gson();
        MyGeocodeResponse geocodeResponse = gson.fromJson(jsonResult, MyGeocodeResponse.class);
        final Address current = new Address(Locale.getDefault());
        if(geocodeResponse.getStatus().equals(STATUS_OK)) {
            MyGeocode locGeocode= geocodeResponse.getResults().get(0);
            String streetAddress = "";
            for(MyAddressComponent component : locGeocode.getAddress_components()) {
                for(String type : component.getTypes()) {
                    if(type.equals("locality")) {
                        current.setLocality(component.getLong_name());
                    }

                    if(type.equals("administrative_area_level_1")) {
                        current.setAdminArea(component.getLong_name());
                    }

                    if(type.equals("street_number")) {
                        if(streetAddress.length() != 0) {
                            current.setAddressLine(0, component.getLong_name() + " " + streetAddress);
                        } else {
                            streetAddress = component.getLong_name();
                        }
                    }

                    if(type.equals("route")) {
                        if(streetAddress.length() != 0) {
                            current.setAddressLine(0, streetAddress + " " + component.getShort_name());
                        } else {
                            streetAddress = component.getShort_name();
                        }
                    }
                }
            }   
            current.setLatitude(latitude);
            current.setLongitude(longitude);
            results.add(current);
        }


        Log.i("TEST", "Got it");

    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return results;
}

编辑: 我想还有一个问题是,如果这是服务器端地理编码,那么这段代码每天只能运行 2,500 次,还是每个应用程序用户每天可以运行 2,500 次?如果它是第一个选项,我仍然没问题,但如果它是第二个选项,我看不出任何想要拥有一半大用户群的应用程序如何在不达到该限制的情况下使用服务器端地理编码。

最佳答案

I now don't know if I'm using server side or client side geocoding

查看您的代码后,您编写了 http://maps.googleapis.com/maps/api/geocode/json?sensor=true&latlng=,因此它是服务器端反向地理编码通过进行额外的外部 http 调用来调用地理编码 api。

if this is server-side Geocoding, then can this code only be run 2,500 times per day period, or can it be run 2,500 times daily per user of the app?

每个 IP 地址有 2,500 个请求限制(基本上是指每天 2500 个请求),是的,这段代码每天只能为您的所有用户运行 2,500 次。您应该记住的一件事是您正在进行 http 调用geocoder api,因此从服务器或客户端进行此调用的位置无关紧要。

你应该看看这个谷歌link他们提到“何时使用客户端地理编码”和“何时使用服务器端地理编码”。

关于Android:我是在进行客户端还是服务器端地理编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23178819/

相关文章:

android - monkeyrunner 触摸事件被识别为长按

android - 适用于 Android Api 的亚马逊 AppStore?

android - 尝试从数据库中加载选定的(通过查询)数据

google-maps - 从Kotlin的LatLng列表中获取LatLngBound的最佳方法

java - 从字符串中提取地址 - 相当于 iOS 中的 NSDataDetector

google-maps - 如何获取某个位置各个方向的 5 公里点,地理编码 google api?

android - 我如何知道 Android 项目是用哪个 IDE 制作的?

php - 使用 MySQL 空间数据在 Google map 上获取最近的地点

JavaScript 将数组插入数组

java - 叠加层在 Google MapView 教程 Android 中不起作用