首页 > 解决方案 > MapboxGeocoding 返回无效的经纬度(Android)

问题描述

我基本上是想从输入的地址中获取纬度和经度,但是 MapboxGeocoding 方法返回的坐标无效,其范围不在 -90 到 90 之间。

MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
                .accessToken(getString(R.string.access_token))
                .query("50 Beale St, San Francisco, CA")
                .build();

        Log.d(TAG, "THe mapbox geocode: " + mapboxGeocoding.toString());

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

                if (response.body() != null) {


                    List<CarmenFeature> results = response.body().features();
                    Log.d(TAG, "carmen response : " + results.toString());

                    if (results.size() > 0) {

                        // Log the first results Point.
                        Point firstResultPoint = results.get(0).center();
                        Log.d(TAG, "Lat : " + firstResultPoint.longitude());
                        Log.d(TAG, "long : " + firstResultPoint.latitude());

                        mapboxMap.animateCamera(CameraUpdateFactory.newCameraPosition(
                                new CameraPosition.Builder()
                                        .target(new LatLng(firstResultPoint.longitude(), firstResultPoint.latitude()))
                                        .zoom(17)
                                        .build()), 4000);
                        Log.d(TAG, "onResponse: " + firstResultPoint.toString());

                    } else {

                        // No result for your request were found.
                        Log.d(TAG, "onResponse: No result found");

                    }
                } else {
                    Log.d(TAG, "onResponse: Nothing found");
                }
            }

这部分在这里:

       MapboxGeocoding mapboxGeocoding = MapboxGeocoding.builder()
                .accessToken(getString(R.string.access_token))
                .query("50 Beale St, San Francisco, CA")
                .build();

返回 的坐标:

Lat : -122.396457
long : 37.791239

这是完全错误的,我的日志:

12-04 10:52:21.531 11351-11351/io.apptizer.business.clover E/AndroidRuntime: FATAL EXCEPTION: main
    Process: io.apptizer.business.clover, PID: 11351
    java.lang.IllegalArgumentException: latitude must be between -90 and 90
        at com.mapbox.mapboxsdk.geometry.LatLng.setLatitude(LatLng.java:132)
        at com.mapbox.mapboxsdk.geometry.LatLng.<init>(LatLng.java:67)
        at io.apptizer.pos.activity.SearchLocationActivity$2.onResponse(SearchLocationActivity.java:222)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)

为什么会发生这种情况,是因为我使用的是公共 mapbox 密钥吗?

标签: androidandroid-studiomapboxmapbox-android

解决方案


我相信

.target(new LatLng(firstResultPoint.longitude(), firstResultPoint.latitude()))

应该

.target(new LatLng(firstResultPoint.latitude(), firstResultPoint.longitude()))

正如 LatLng 的名字所暗示的那样。


推荐阅读