首页 > 解决方案 > Mapbox 反向地理编码并不总是返回结果

问题描述

我在 mapbox 上尝试反向地理编码示例

一切正常。但问题是当我替换我当前的位置时,它不会返回结果。我认为 mapbox 没有太多关于我所在地区的数据(我在越南)。假设我有一个坐标和地址数据集,是否可以添加我自己的地方

我的代码:

mapboxGeocoding = MapboxGeocoding.builder()
                .accessToken(getString(R.string.mapbox_access_token))
                .query(Point.fromLngLat(106.799142,10.875838))
                .geocodingTypes(GeocodingCriteria.TYPE_ADDRESS)
                .build();;

        mapboxGeocoding.enqueueCall(new Callback<GeocodingResponse>() {
            @Override
            public void onResponse(Call<GeocodingResponse> call, Response<GeocodingResponse> response) {

                List<CarmenFeature> results = response.body().features();

                if (results.size() > 0) {

                    // Log the first results Point.
                    //Point firstResultPoint = results.get(0).center();
                    String country=results.get(0).placeName();
                    Log.d("Geocoding",country);
                } else {

                    // No result for your request were found.

                    Toast.makeText(getApplicationContext(), "onResponse: No result found", Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<GeocodingResponse> call, Throwable throwable) {
                throwable.printStackTrace();
            }
        });
    }
    @Override
    protected void onDestroy(){
        super.onDestroy();
        mapboxGeocoding.cancelCall();
    }

标签: androidmapboxreverse-geocoding

解决方案


目前无法将自定义 POI 添加到支持 Geocoding API 的数据中,但解决方法是使用Tilequery API。此 API 根据与特定地理坐标的接近程度从矢量切片中检索特征。您可以通过以下工作流程使用自定义 POI 模拟反向地理编码功能:

  1. 创建一个包含自定义 POI 的图块集。也就是说,将您的数据上传为 tileset
  2. 检索要用于反向地理编码的点的地理坐标。
  3. 调用 Tilequery API,其中 radius 为 0,tileset_id 是您在步骤 1 中创建的图块集的 id,{lon},{lat} 是在步骤 2 中检索到的经度和纬度。有关每个参数的更多详细信息可以在这里找到。

我建议通过本教程学习使用 Tilequery API 制作健康食品查找器,以获得更多关于如何使用 API 进行自定义 POI 反向地理编码的直觉。Tilequery API 游乐场也是交互式实验的绝佳资源。

如果有不清楚的地方,请告诉我!


推荐阅读