使用 mapbox 从地址获取经纬度

Get latitude and longitude from the address with mapbox

有没有人举例说明如何使用 mapbox 获取地址的纬度和经度?

查看 Mapbox Android SDK 指南,执行以下操作:

不要在主 UI 线程上执行任何示例代码。

示例 1:

请注意,position 参数是可选的,但可以更快地获得结果。

// position used for proximity
Position position = Position.fromCoordinates(-73.98572, 40.74843);

MapboxGeocoding client = new MapboxGeocoding.Builder()
            .setAccessToken("<your access token here>")
            .setLocation("Empire State Building")
            .setProximity(position)
            .build();

Response<GeocodingResponse> response = client.execute();

示例 2:

MapboxGeocoding client = new MapboxGeocoding.Builder()
            .setAccessToken("<your access token here>")
            .setLocation("Empire State Building")
            .build();

Response<GeocodingResponse> response = client.execute();

您必须使用 Mapbox.GeocodingBuilder,获取 Response,并从 Response 访问您想要的字段。

这是我在网上找到的一个简单示例,并稍作编辑。

private void simpleSample() {
    MapboxGeocoder client = new MapboxGeocoder.Builder()
            .setAccessToken(MAPBOX_ACCESS_TOKEN)
            .setLocation("The White House")
            .build();

    client.enqueue(new Callback<GeocoderResponse>() {
        @Override
        public void onResponse(Response<GeocoderResponse> response, Retrofit retrofit) {
            // Features, basically, is a list of search results
            // get the first one
            // get latitude and longitude
            String latitude = response.body().getFeatures().get(0).getLatitude();
            String longitude = response.body().getFeatures().get(0).getLongitude();
        }

        @Override
        public void onFailure(Throwable t) {
            // log t.getMessage()
        }
    });
}

您可以在 API 101 webpage.

上阅读有关 mapbox 地理编码 API 的一些基础知识

你可以在mapbox Geocoding API documentation page

上看到一些官方例子

您可以在 API forward geocoding playground page (注意:您必须登录 mapbox 帐户才能执行此操作,但如果您没有,则可以免费制作一个)。

(完全披露:我在 SmartyStreets 工作,这是一家地址验证和自动完成公司,competing products。)