Thursday, 8 August 2013

How can I parse XML data to Google Map V2

How can I parse XML data to Google Map V2

I would like to know, whether how can I combine Google Map V2 together
with XMLPullParser.
I'm retrieving the Longitude and latitude from "
http://api.eventful.com/rest/events/search?app_key=42t54cX7RbrDFczc&location=singapore
" I would like it to be parse to google map, so that on select "title" it
will show me the respective location on the map.
Is there any tutorial or similiar sample that I could follow after?
Here is some of my code for XMLPullParser
public class MainActivity extends Activity {
ArrayList<String> title;
ArrayList<String> start_time;
ArrayList<String> latitude;
ArrayList<String> longitude;
ItemAdapter adapter1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.list);
title = new ArrayList<String>();
start_time = new ArrayList<String>();
latitude = new ArrayList<String>();
longitude = new ArrayList<String>();
try {
URL url = new URL(
"http://api.eventful.com/rest/events/search?app_key=42t54cX7RbrDFczc&location=singapore");
DocumentBuilderFactory dbf =
DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("event");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("title");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
title.add(""+ ((Node) nameList.item(0)).getNodeValue());
NodeList websiteList =
fstElmnt.getElementsByTagName("start_time");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
start_time.add(""+ ((Node)
websiteList.item(0)).getNodeValue());
NodeList websiteList1 =
fstElmnt.getElementsByTagName("latitude");
Element websiteElement1 = (Element) websiteList1.item(0);
websiteList1 = websiteElement1.getChildNodes();
latitude.add(""+ ((Node)
websiteList1.item(0)).getNodeValue());
NodeList websiteList2 =
fstElmnt.getElementsByTagName("longitude");
Element websiteElement2 = (Element) websiteList2.item(0);
websiteList2 = websiteElement2.getChildNodes();
longitude.add(""+ ((Node)
websiteList2.item(0)).getNodeValue());
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
adapter1 = new ItemAdapter(this);
list.setAdapter(adapter1);
}
class ItemAdapter extends BaseAdapter {
final LayoutInflater mInflater;
private class ViewHolder {
public TextView title_text;
public TextView des_text;
}
public ItemAdapter(Context context) {
// TODO Auto-generated constructor stub
super();
mInflater =
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//@Override
public int getCount() {
return title.size();
}
//@Override
public Object getItem(int position) {
return position;
}
//@Override
public long getItemId(int position) {
return position;
}
//@Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View view = convertView;
final ViewHolder holder;
if (convertView == null) {
view = mInflater.inflate(R.layout.mainpage_list,parent,
false);
holder = new ViewHolder();
holder.title_text = (TextView)
view.findViewById(R.id.title_text);
holder.des_text = (TextView)
view.findViewById(R.id.des_text);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.title_text.setText(""+title.get(position));
holder.des_text.setText(""+Html.fromHtml(start_time.get(position)));
return view;
}
}
}

No comments:

Post a Comment