使用mapbox从地址获取纬度和经度

时间:2016-05-20 13:54:47

标签: android latitude-longitude mapbox

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

2 个答案:

答案 0 :(得分:2)

查看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();

答案 1 :(得分:2)

您必须使用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上尝试搜索字词并在地图上查看结果或查看原始JSON(这对于查看结果数据的形状以及如何遍历它)非常有用(注意:您必须登录)到mapbox帐户执行此操作,但如果您没有,则可以免费提供一个。)

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