解析XML文件以在google-map应用程序上添加标记

时间:2012-04-01 08:14:23

标签: android google-maps xml-parsing google-maps-markers

我在从XML文件中添加标记时遇到问题

这是我的XML文件

<PA>
<node>
    <id>1</id>
    <nama>SMP 8  Cimahi</nama>
    <longitude>-6.894581</longitude>
    <latitude>107.536519</latitude>
    <kategori>sekolah</kategori>
</node>

我想用DOM解析器

解析经度和纬度的值

这是我在活动文件

上的一段代码
 try {

         DocumentBuilderFactory DBF;
            DocumentBuilder DB;
            Document dom;
            Element elt;

            DBF = DocumentBuilderFactory.newInstance();
            DB = DBF.newDocumentBuilder();
            dom = DB.parse(new InputSource(getAssets().open("database.xml")));
            elt = dom.getDocumentElement(); 

        NodeList nodeList = elt.getElementsByTagName("node");
        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            Element fstElmnt = (Element) node;
                            NodeList longList = fstElmnt.getElementsByTagName("longitude");
            Element longElement = (Element) longList.item(0);
            longList = longElement.getChildNodes();

            int lng = ((int) (Double.parseDouble(longList.item(0).getNodeValue()) *1E6));


            NodeList latList = fstElmnt.getElementsByTagName("latitude");
            Element latElement = (Element) latList.item(0);
            latList = latElement.getChildNodes();

            int lat = ((int) (Double.parseDouble(latList.item(0).getNodeValue()) *1E6));

            List<Overlay> mapOverlays = mapView.getOverlays();
            Drawable drawable = this.getResources().getDrawable(R.drawable.school);
            markerpeta itemizedoverlay = new markerpeta(drawable,this);

            GeoPoint point = new GeoPoint(lat,lng);
            OverlayItem overlayitem = new OverlayItem(point, "", null);

            itemizedoverlay.addOverlay(overlayitem);
            mapOverlays.add(itemizedoverlay);

这是我的markerpeta课程

public class markerpeta extends ItemizedOverlay {
private ArrayList<OverlayItem> mOverlay = new ArrayList<OverlayItem>();
private Context mContext;
//private peta peta;

public markerpeta(Drawable defaultMarker) {
    super(boundCenterBottom(defaultMarker));
    // TODO Auto-generated constructor stub
}

public markerpeta(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    mContext = context;
    // TODO Auto-generated constructor stub
}

public void addOverlay(OverlayItem overlay){
    mOverlay.add(overlay);
    populate();

}

@Override
protected OverlayItem createItem(int i) {
    // TODO Auto-generated method stub
    return mOverlay.get(i);
}

@Override
public int size() {
    // TODO Auto-generated method stub
    return mOverlay.size();
}

 @Override
        protected boolean onTap(int index) {
          OverlayItem item = mOverlay.get(index);
          AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
          dialog.setTitle(item.getTitle());
          dialog.setMessage(item.getSnippet());
          dialog.setPositiveButton("Close", new OnClickListener() {

                public void onClick(DialogInterface arg0, int arg1) {
                    arg0.dismiss();
             }
            });
          dialog.show();
          return true;
        }

}

此代码中没有错误,但应用程序上没有标记,任何人都可以帮助我吗? 谢谢

1 个答案:

答案 0 :(得分:0)

在您的markerpeta类中添加以下方法

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
 // TODO Auto-generated method stub
 super.draw(canvas, mapView, shadow);           
}

也尝试替换以下两行

Drawable drawable = this.getResources().getDrawable(R.drawable.school);
markerpeta itemizedoverlay = new markerpeta(drawable,this);

Drawable drawable = getApplicationContext()。getResources()。getDrawable(R.drawable.school);   markerpeta itemizedoverlay = new markerpeta(drawable,getApplicationContext());