2

I am working on application in which i need to show all the ATM. I am fetching the data from google api, but i am not able to fetch all the data. I want to fetch latitude and longitude but when I am fetch the data I am getting only one value.

String url = "https://maps.googleapis.com/maps/api/place/radarsearch/json?location=23.308003,81.3275914&radius=5000&type=atm&key=AIzaSyCYZoSkxHC_Exym4YBWvXZXwMyJA7dzEB4";
        try {
            JSONObject jObject = new JSONObject(request(url));
            String jj = jObject.getString("status");

            JSONArray data = jObject.getJSONArray("results");

            location = new ArrayList<HashMap<String, String>>();
            HashMap<String, String> map;

            for(int i = 0; i < data.length(); i++){
                JSONObject c = data.getJSONObject(i);
                map = new HashMap<String, String>();

            JSONObject geometry = c.getJSONObject("geometry");
            JSONObject locate = geometry.getJSONObject("location");
            String lat = locate.getString("lat");
            String lng = locate.getString("lng");
            Log.e("Data", lat+"--"+lng);
            //map.put("LocationID", c.getString("id"));
            //map.put("placeId",c.getString("place_id"));
            //location.add(map);


            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
2
  • you haven't put the value of lat lon which you are getting from Json in the the location(HashMap) Commented Jul 7, 2017 at 11:38
  • @user3810679 I recommend you to read about Retrofit and GSON/Jackson Commented Jul 7, 2017 at 11:48

1 Answer 1

7

I will suggest you to go through some parsing tutorial. Later you can us libraries like gson or jackson to parse you json. Now lot of people don't write json parsing code themselves.

Once you understand the json structure you should be able to parse it yourself.

Your json

"results": [ // array
    {        //  jsonobject
        "geometry": {  // geometry jsonobject
            "location": {   // location jsonobject
                // values
                "lat": 23.3085793, 
                "lng": 81.353116
            }
        },
        ... // the rest of the json

Then

// this is right   
JSONArray data = jObject.getJSONArray("results");

After that

   for(int i = 0; i < data.length(); i++){
       JSONObject c = data.getJSONObject(i);
       JSONObject geometry = c.getJSONObject("geometry"); 
       JSONObject location = geometry.getJSONObject("location");
       String lat = location.getString("lat");
       String lng = location.getString("lng"); 

       Log.d("Location->"," "+lat+" "+lng);
       // now do what you want.  
       // You probably meant to do
       map = new HashMap<String, String>();
       map.put("lat",lat);
       map.pt("lng",lng);
       location.add(map);

   }
Sign up to request clarification or add additional context in comments.

6 Comments

I am not able to fetch all latitude and longitude value. I am getting single latitude and longitude only
@user3810679 you will get all the values. Log.e("Location->"," "+lat+" "+lng); add this in the above for loop
I have used same thing but I am not getting how do i fetch all latitude and longitude value
@user3810679 show us your updated code if you are doing the same thing. You are doing nothing with the values and your hashmap is of no use
please check my updated code, Yeah right now I am not using hashmap. first I am checking that i am getting all the values or not
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.