Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 4.64 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Find lat and long from address Json Pasing
  2. {
  3.    "results" : [
  4.       {
  5.          "address_components" : [
  6.             {
  7.                "long_name" : "Pune",
  8.                "short_name" : "Pune",
  9.                "types" : [ "locality", "political" ]
  10.             },
  11.             {
  12.                "long_name" : "Pune",
  13.                "short_name" : "Pune",
  14.                "types" : [ "administrative_area_level_2", "political" ]
  15.             },
  16.             {
  17.                "long_name" : "Maharashtra",
  18.                "short_name" : "Maharashtra",
  19.                "types" : [ "administrative_area_level_1", "political" ]
  20.             },
  21.             {
  22.                "long_name" : "India",
  23.                "short_name" : "IN",
  24.                "types" : [ "country", "political" ]
  25.             }
  26.          ],
  27.          "formatted_address" : "Pune, Maharashtra, India",
  28.          "geometry" : {
  29.             "bounds" : {
  30.                "northeast" : {
  31.                   "lat" : 18.62719520,
  32.                   "lng" : 73.98963809999999
  33.                },
  34.                "southwest" : {
  35.                   "lat" : 18.41367390,
  36.                   "lng" : 73.75319080
  37.                }
  38.             },
  39.             "location" : {
  40.                "lat" : 18.52043030,
  41.                "lng" : 73.85674370
  42.             },
  43.             "location_type" : "APPROXIMATE",
  44.             "viewport" : {
  45.                "northeast" : {
  46.                   "lat" : 18.60505030,
  47.                   "lng" : 73.98480309999999
  48.                },
  49.                "southwest" : {
  50.                   "lat" : 18.43576840,
  51.                   "lng" : 73.72868430
  52.                }
  53.             }
  54.          },
  55.          "types" : [ "locality", "political" ]
  56.       }
  57.    ],
  58.    "status" : "OK"
  59. }
  60.        
  61. import java.util.ArrayList;
  62. import java.util.HashMap;
  63.  
  64. import org.json.JSONArray;
  65. import org.json.JSONException;
  66. import org.json.JSONObject;
  67. import org.w3c.dom.Document;
  68. import org.w3c.dom.Element;
  69. import org.w3c.dom.NodeList;
  70.  
  71. import com.pxr.tutorial.xmltest.R;
  72.  
  73. import android.app.ListActivity;
  74. import android.content.Intent;
  75. import android.os.Bundle;
  76. import android.util.Log;
  77. import android.view.View;
  78. import android.widget.AdapterView;
  79. import android.widget.AdapterView.OnItemClickListener;
  80. import android.widget.ListAdapter;
  81. import android.widget.ListView;
  82. import android.widget.SimpleAdapter;
  83. import android.widget.Toast;
  84.  
  85. public class Main extends ListActivity {
  86.     /** Called when the activity is first created. */
  87.     @Override
  88.     public void onCreate(Bundle savedInstanceState) {
  89.         super.onCreate(savedInstanceState);
  90.         setContentView(R.layout.listplaceholder);
  91.  
  92.         ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
  93.  
  94.  
  95.         JSONObject json = JSONfunctions.getJSONfromURL("http://maps.google.com/maps/api/geocode/json?address=pune+India&sensor=false");
  96.  
  97.         try{
  98.  
  99.             JSONArray  earthquakes = json.getJSONArray("location");
  100.  
  101.             for(int i=0;i<earthquakes.length();i++){                        
  102.                 HashMap<String, String> map = new HashMap<String, String>();    
  103.                 JSONObject e = earthquakes.getJSONObject(i);
  104.  
  105.                 //map.put("id",  String.valueOf(i));
  106.                 map.put("name", "Earthquake name:" + e.getString("lat"));
  107.                 map.put("magnitude", "Magnitude: " +  e.getString("lng"));
  108.                 mylist.add(map);            
  109.             }      
  110.         }catch(JSONException e)        {
  111.              Log.e("log_tag", "Error parsing data "+e.toString());
  112.         }
  113.  
  114.         ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
  115.                         new String[] { "name", "magnitude" },
  116.                         new int[] { R.id.item_title, R.id.item_subtitle });
  117.  
  118.         setListAdapter(adapter);
  119.  
  120.         final ListView lv = getListView();
  121.         lv.setTextFilterEnabled(true);  
  122.         lv.setOnItemClickListener(new OnItemClickListener() {
  123.             public void onItemClick(AdapterView<?> parent, View view, int position, long id) {              
  124.                 @SuppressWarnings("unchecked")
  125.                 HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);                  
  126.                 Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
  127.  
  128.             }
  129.         });
  130.     }
  131. }
  132.        
  133. JSONObject mainObj= new JSONObject(response);
  134.         JSONArray jArray = mainObj.optJSONArray("results");
  135.         for(int i=0;i<jArray.length();i++)
  136.         {
  137.             JSONObject temp = jArray.optJSONObject(i);
  138.             JSONObject loc = temp.optJSONObject("geomatery").optJSONObject("location");
  139.             loc.getString("lat");
  140.             loc.getString("lng");
  141.         }