0

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;
    }
}
Nurzhan Nogerbek
  • 4,806
  • 16
  • 87
  • 193
  • I would recommend logging the values you receive in your `PositionResponse` object. I just tested your `Arrays.toString(positionResponse.getPosition()).replaceAll("\\[|\\]", "")` statement, and it looks like it works as expected. – Daniel Nugent Apr 09 '15 at 23:35
  • Hello man. You mean that statement is not correct? I dont understand your idea exactly. I also used Log Log.d("w3w location: ", Arrays.toString(positionResponse.getPosition())); instead of Arrays.toString(positionResponse.getPosition()).replaceAll("\\[|\\]", "") just to see what will be in logcat. Unfortunaltly I have nothing in logcat too. Do you know the reason? – Nurzhan Nogerbek Apr 09 '15 at 23:56
  • The statement is correct. I tested it and it works. Log the values of `double[] position` inside `PositionResponse` to make sure they are getting populated in there. – Daniel Nugent Apr 09 '15 at 23:59
  • Hello man! I found my mistake. It was here return String.format("%f,%f", words); Stupid mistake. But I have one more question. As you see from JSON its latitude and longitude. I want to set marker in Google map but have red line here: .position(value) Can you check my post again. I updated it. What I did wrong again. – Nurzhan Nogerbek Apr 10 '15 at 07:43
  • I think I see the issue. Posting an answer now. – Daniel Nugent Apr 10 '15 at 07:59
  • Thank you for the accept! It would now probably be best to modify the title of your question to something like `Getting location result of w3w API into Google Map`, and add and edit to the question as well, so that future readers of the question don't get confused. – Daniel Nugent Apr 10 '15 at 17:40

2 Answers2

0

Try to use double. long it's for integer numbers. In your JSON numbers with a floating point.

Fevr
  • 1
  • 1
  • I used double in my PositionResponse class but anyway the result was the same as before. I mean nothing. Do you have any another ideas? – Nurzhan Nogerbek Apr 09 '15 at 23:04
0

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

Community
  • 1
  • 1
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137