Search code examples
androidandroid-studiomapboxmapbox-android

MapboxGeocoding returning invalid lat and long (Android)


I'm basically trying to get Lat and Long from entered address, but the MapboxGeocoding method returns invalid co ordinates which does not range between -90 to 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");
                }
            }

This part here :

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

returns co ordinates of :

Lat : -122.396457
long : 37.791239

Which are completely wrong, my log:

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)

Why is this happening, is it because I'm using a public mapbox key?


Solution

  • I believe

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

    should be

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

    as the name LatLng suggests.