javaandroidjsonparsingandroid-parser

Parse double value and set in TextView


For parse data I use retrofit (gson) library. I have problems with parsing double value 'position'. The full JSON you can see here below. I parse String array 'words' great but with double value I have nothing in TextView. Can anyone say what exactly wrong and what I need to fix and what I missed?

Thanks for any help!

JSON

enter image description here

MainActivity.java

w3wNetworkFactory.w3wGetInstance().w3wGetApiCall().getPosition(w3w_API_KEY, new Words("gliders.inquest.hardly"), new Callback<PositionResponse>() {
            @Override
            public void success(PositionResponse positionResponse, Response response) {
                position_w3w.setText(Arrays.toString(positionResponse.getPosition()).replaceAll("\\[|\\]", ""));
                //Log.d("w3w location: ", Arrays.toString(positionResponse.getPosition()));
                String mLatLng = Arrays.toString(positionResponse.getPosition()).replaceAll("\\[|\\]", "");
                //Convert String to double
                double value = Double.parseDouble(mLatLng);
                //Set marker to map
                mGoogleMap.addMarker(new MarkerOptions().position(value)
                        .title("Geolocation system")
                        .snippet("Your last current location which was available!")
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_location)));
            }

            @Override
            public void failure(RetrofitError error) {
                error.toString();
            }
        });

PositionResponse.java

public class PositionResponse {
    private double[] position;

    public double[] getPosition() {
        return position;
    }
}

Solution

  • To answer your question about the Google Map, you need to separate out the lat and lon, and create a LatLng object. I think it should be something like this:

        @Override
                    public void success(PositionResponse positionResponse, Response response) {
                        position_w3w.setText(Arrays.toString(positionResponse.getPosition()).replaceAll("\\[|\\]", ""));
                        //Log.d("w3w location: ", Arrays.toString(positionResponse.getPosition()));
                        String mLatLng = Arrays.toString(positionResponse.getPosition()).replaceAll("\\[|\\]", "");
    
                        String[] arrLatLng = mLatLng.split(","); //separate out lat and lon
                        LatLng latLon = new LatLng(Double.parseDouble(arrLatLng[0]), Double.parseDouble(arrLatLng[1])); //create the LatLng object
    
                        //Convert String to double
                        //double value = Double.parseDouble(mLatLng); this wouldn't work, you need to do both values individually
                        //Set marker to map, use LatLng object latLon
                        mGoogleMap.addMarker(new MarkerOptions().position(latLon)
                                .title("Geolocation system")
                                .snippet("Your last current location which was available!")
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_location)));
    
                         CameraPosition cameraPosition = new CameraPosition.Builder()
                            .target(latLon).zoom(12).build();
    
    
                         mGoogleMap.animateCamera(CameraUpdateFactory
                            .newCameraPosition(cameraPosition));
                    }
    

    For more detailed info on getting Google Maps API fully set up, see my other answer here: Cannot add map in Android Studio as stated in Google "Getting Started" page; Android Studio gives error