- Find lat and long from address Json Pasing
- {
- "results" : [
- {
- "address_components" : [
- {
- "long_name" : "Pune",
- "short_name" : "Pune",
- "types" : [ "locality", "political" ]
- },
- {
- "long_name" : "Pune",
- "short_name" : "Pune",
- "types" : [ "administrative_area_level_2", "political" ]
- },
- {
- "long_name" : "Maharashtra",
- "short_name" : "Maharashtra",
- "types" : [ "administrative_area_level_1", "political" ]
- },
- {
- "long_name" : "India",
- "short_name" : "IN",
- "types" : [ "country", "political" ]
- }
- ],
- "formatted_address" : "Pune, Maharashtra, India",
- "geometry" : {
- "bounds" : {
- "northeast" : {
- "lat" : 18.62719520,
- "lng" : 73.98963809999999
- },
- "southwest" : {
- "lat" : 18.41367390,
- "lng" : 73.75319080
- }
- },
- "location" : {
- "lat" : 18.52043030,
- "lng" : 73.85674370
- },
- "location_type" : "APPROXIMATE",
- "viewport" : {
- "northeast" : {
- "lat" : 18.60505030,
- "lng" : 73.98480309999999
- },
- "southwest" : {
- "lat" : 18.43576840,
- "lng" : 73.72868430
- }
- }
- },
- "types" : [ "locality", "political" ]
- }
- ],
- "status" : "OK"
- }
- import java.util.ArrayList;
- import java.util.HashMap;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import org.w3c.dom.Document;
- import org.w3c.dom.Element;
- import org.w3c.dom.NodeList;
- import com.pxr.tutorial.xmltest.R;
- import android.app.ListActivity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ListAdapter;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.Toast;
- public class Main extends ListActivity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.listplaceholder);
- ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
- JSONObject json = JSONfunctions.getJSONfromURL("http://maps.google.com/maps/api/geocode/json?address=pune+India&sensor=false");
- try{
- JSONArray earthquakes = json.getJSONArray("location");
- for(int i=0;i<earthquakes.length();i++){
- HashMap<String, String> map = new HashMap<String, String>();
- JSONObject e = earthquakes.getJSONObject(i);
- //map.put("id", String.valueOf(i));
- map.put("name", "Earthquake name:" + e.getString("lat"));
- map.put("magnitude", "Magnitude: " + e.getString("lng"));
- mylist.add(map);
- }
- }catch(JSONException e) {
- Log.e("log_tag", "Error parsing data "+e.toString());
- }
- ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
- new String[] { "name", "magnitude" },
- new int[] { R.id.item_title, R.id.item_subtitle });
- setListAdapter(adapter);
- final ListView lv = getListView();
- lv.setTextFilterEnabled(true);
- lv.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- @SuppressWarnings("unchecked")
- HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
- Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
- JSONObject mainObj= new JSONObject(response);
- JSONArray jArray = mainObj.optJSONArray("results");
- for(int i=0;i<jArray.length();i++)
- {
- JSONObject temp = jArray.optJSONObject(i);
- JSONObject loc = temp.optJSONObject("geomatery").optJSONObject("location");
- loc.getString("lat");
- loc.getString("lng");
- }