从服务器加载具有限制和偏移量的地图标记

时间:2019-01-14 07:52:49

标签: java android google-maps limit offset

我正在根据用户的当前位置显示半径范围内的标记,并且我希望在从地图片段开始活动时从服务器加载预设数量的标记,并根据需要加载更多-理想情况下,加载20个标记/应用启动时的位置,然后在用户的命令下再加载20乘20。应该这样做,而不是从服务器返回所有可能会真正降低应用速度的位置。

我考虑过使用标记聚类,或者在用户滑动或单击地图时调用该方法,但是我不确定这是否是理想的解决方案。

在列表中,我可以使用类似RecyclerView.OnScrollListener()的东西轻松做到这一点:

listView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        int visibleItemCount = mLayoutManager.getChildCount();
        int totalItemCount = mLayoutManager.getItemCount();
        int pastVisibleItems = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
        if (pastVisibleItems + visibleItemCount >= totalItemCount) {
                //End of list
                fetchAllLocations(locationForDate, latitude, longitude);
            }
        }
});

上面的示例非常简单-我到达列表的底部,该方法再次被调用,它返回更多位置。

我找不到任何标准化的方法来调用地图标记的load more方法。

我将使用LocationListSize保留返回位置的数量,并将其用于返回位置的map方法:

void fetchAllLocations(String LocationForDate, String latitude, String longitude) {
    TokenManager tokenManager = TokenManager.getInstance(getContext().getSharedPreferences("prefs", MODE_PRIVATE));
    ApiInterface apiService = ApiClient.createServiceWithAuth(ApiInterface.class, tokenManager);
    CallLocationsFromSQLite(LocationForDate);

    SQLiteHandler db = new SQLiteHandler(Objects.requireNonNull(getActivity()).getApplicationContext());
    HashMap<String, String> settings = db.getSettings();
    String radius, receiveFrom;
    radius = settings.get("radius");
    receiveFrom = settings.get("invites_received_from");

    LocationListSize = LocationList.size();
    String offset = String.valueOf(LocationListSize);
    String count = String.valueOf(20);

    if (isNetworkAvailable()) {
        Call<LocationsResponse> call = apiService.getLocationsForConditions(LocationForDate, radius, latitude, longitude, receiveFrom, count, offset);
        call.enqueue(new Callback<LocationsResponse>() {
            @Override
            public void onResponse(Call<LocationsResponse> call, retrofit2.Response<LocationsResponse> response) {
                Log.d(TAG, "onResponse: " + response);

                if (response.isSuccessful()) {
                    List<Location> Locations = Objects.requireNonNull(response.body()).getResults();

                    if (Locations != null) {
                        for (Location Location : Locations) {
                            setMarker(Double.parseDouble(Location.getLat()), Double.parseDouble(Location.getLon()),
                                    Location.getId(), Location.getUserName(), Location.getDateTime());
                        }
                        // Adding number of locations retrieved to current count
                        Integer numberOfOhterLocations = Locations.size();
                        LocationListSize += numberOfOhterLocations;
                    }
                } else {
                    if (response.code() == 401) {

                        ApiError apiError = ErrorUtils.converErrors(response.errorBody());
                        Log.d(TAG, "There was an error: " + apiError);
                    } else {
                        Log.d(TAG, "There was an error: " + response);
                    }
                }
            }

            @Override
            public void onFailure(Call<LocationsResponse> call, Throwable t) {
                // Log error here since request failed
                Log.e(TAG, t.toString());
            }
        });
    } else {
        Log.e(TAG, "Network not available");
    }
}

因此,基本上,我想知道是否存在一种标准化的方法,如recyclerview中那样,通过限制和偏移量从服务器加载更多地图标记。

0 个答案:

没有答案