gpt4 book ai didi

java - 将 XML 数据 "location"解析为 Google map

转载 作者:行者123 更新时间:2023-11-30 11:29:41 25 4
gpt4 key购买 nike

我想将“lat”和“lng”标签解析到谷歌地图中。

目前,我正在从“http://api.eventful.com/rest/events/search?app_key=42t54cX7RbrDFczc&location=singapore ”,我能够在显示“标题”和“开始时间”的 ListView 中检索数据。接下来,我想做的就是显示“位置”。但是,我遇到了一些问题。

我希望在点击标题时该位置显示在谷歌地图上(下一个 Intent )。

我不知道我的代码有什么问题,我无法在 map 上检索到位置。

这是我的代码:

主.java

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;
}
}

public void onListItemClick(ListView parent, View v, int position,long id)
{;

Intent n = new Intent(MainActivity.this,map.class);
n.putExtra("lat",latitude.get(position).toString());
n.putExtra("lng",longitude.get(position).toString());
startActivity(n);
}
}

map .java

public class map extends  FragmentActivity  {

LatLng storeLocation;
private GoogleMap MAP;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_shown);
Intent n = getIntent();
String latitude = n.getStringExtra("lat");
String longitude= n.getStringExtra("lng");
SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
MAP = fm.getMap();

Double lat= Double.parseDouble(latitude);
Double lng= Double.parseDouble(longitude);


//MAP = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
android.support.v4.app.FragmentManager myFM = getActivity().getSupportFragmentManager();
SupportMapFragment myMAPF = (SupportMapFragment) myFM.findFragmentById(R.id.map);
MAP = myMAPF.getMap();
MAP.setMyLocationEnabled(true);
MAP.setMapType(GoogleMap.MAP_TYPE_NORMAL);
storeLocation = new LatLng(lat,
lng);

MAP.addMarker(new MarkerOptions().position(storeLocation).title(
storeLocation.toString()));



}

private FragmentActivity getActivity() {
// TODO Auto-generated method stub
return null;
}
}

最佳答案

您应该创建一个数据持有者类来持有多个 event TAG 条目的数据

将其替换为您的 for 循环代码

Vector objVector = new Vector(nodeList.getLength());

for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
xmldata objXmldata = new xmldata();
objXmldata.setValue(fstElmnt);
objVector.addElement(objXmldata);
}

并创建一个类来保存事件的数据,如下所示。

import java.util.Vector;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class xmldata {

String title = "";
String start_time = "";
String latitude = "";
String longitude = "";

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getStart_time() {
return start_time;
}

public void setStart_time(String start_time) {
this.start_time = start_time;
}

public String getLatitude() {
return latitude;
}

public void setLatitude(String latitude) {
this.latitude = latitude;
}

public String getLongitude() {
return longitude;
}

public void setLongitude(String longitude) {
this.longitude = longitude;
}

public void setValue(Element fstElmnt) {
setTitle(fstElmnt.getElementsByTagName("title").toString());
setStart_time(fstElmnt.getElementsByTagName("start_time").toString());
setLatitude(fstElmnt.getElementsByTagName("latitude").toString());
setLongitude(fstElmnt.getElementsByTagName("longitude").toString());
}

}

关于java - 将 XML 数据 "location"解析为 Google map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18178842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com