0

我让它工作,直到我尝试将它与从我的网页解析的 xml 结合起来。没有添加标记,但我没有错误。下面是新代码。任何帮助是极大的赞赏!

LocationBasedServicesV2.java:

private MapView myMap;
private TextView tv;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv = (TextView) findViewById(R.id.tv);
    try {
           URL url = new URL("MySiteGoesHere");
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();
            ExampleHandler myExampleHandler = new ExampleHandler();
            xr.setContentHandler(myExampleHandler);
            xr.parse(new InputSource(url.openStream()));
            ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();
            tv.setText(parsedExampleDataSet.toString());
    } catch (Exception e) {
        new AlertDialog.Builder(this)   
        .setMessage(e.toString())   
        .setTitle("error")   
        .setCancelable(true)   
        .setNeutralButton(android.R.string.cancel,   
           new DialogInterface.OnClickListener() {   
           public void onClick(DialogInterface dialog, int whichButton){}   
           })   
        .show(); 
    }


    initMap();
    //initLocationManager();
}
private void initMap() {
    myMap = (MapView) findViewById(R.id.mymap);

    View zoomView = myMap.getZoomControls();
    LinearLayout myzoom = (LinearLayout) findViewById(R.id.myzoom);
    myzoom.addView(zoomView);
    myMap.displayZoomControls(true);

}
/*private void initLocationManager() {
    locManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locListener = new LocationListener() {
        public void onLocationChanged(Location newLocation) {
            createAndShowMyItemizedOverlay(newLocation);
        }
        public void onProviderDisabled(String arg0) {
        }
        public void onProviderEnabled(String arg0) {
        }
        public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        }
    };
    locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            locListener);
}*/
public void createAndShowMyItemizedOverlay(int Lat,int Long) {
    //int Lat2;
    //int Long2;
    //Lat2 = Integer.parseInt(Lat);
    //Long2 = Integer.parseInt(Long);
    List overlays = myMap.getOverlays();
    GeoPoint geopoint = new GeoPoint(Lat,Long);
    Drawable icon = getResources().getDrawable(R.drawable.icon);
    icon.setBounds(0, 0, icon.getIntrinsicWidth(), icon
            .getIntrinsicHeight());
    MyItemizedOverlay overlay = new MyItemizedOverlay(icon);
    OverlayItem item = new OverlayItem(geopoint, "My Location", null);
    overlay.addItem(item);
    myMap.getOverlays().add(overlay);
    myMap.getController().animateTo(geopoint);

        }
@Override
protected boolean isRouteDisplayed() {
    return false;
}
 }

MyItemizedOverlay.java:

 public class MyItemizedOverlay extends ItemizedOverlay {
private List items;
private Drawable marker;
public MyItemizedOverlay(Drawable defaultMarker) {
    super(defaultMarker);
    items = new ArrayList();
    marker = defaultMarker;
}
@Override
protected OverlayItem createItem(int index) {
    return (OverlayItem)items.get(index);
}
@Override
public int size() {
    return items.size();
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
    super.draw(canvas, mapView, shadow);
    boundCenterBottom(marker);
}
public void addItem(OverlayItem item) {
    items.add(item);
    populate();
}
 }

ExampleHandler.java:

public class ExampleHandler extends DefaultHandler{

    private boolean in_lat = false;
    private boolean in_long = false;
    private boolean in_retailer = false;

    private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();


    public ParsedExampleDataSet getParsedData() {
            return this.myParsedExampleDataSet;
    }

    @Override
    public void startDocument() throws SAXException {
            this.myParsedExampleDataSet = new ParsedExampleDataSet();
    }

    @Override
    public void endDocument() throws SAXException {
            // Nothing to do
    }


    @Override
    public void startElement(String namespaceURI, String localName,
                    String qName, Attributes atts) throws SAXException {
            if (localName.equals("Lat")) {
                    this.in_lat = true;
            }else if (localName.equals("Long")) {
                this.in_long = true;
            }

    }


    @Override
    public void endElement(String namespaceURI, String localName, String qName)
                    throws SAXException {
           if (localName.equals("Lat")) {
                    this.in_lat = false;
            }else if (localName.equals("Long")) {
                this.in_long = false;}
            else if (localName.equals("Retailer")) {
                this.in_retailer = false;}
    }

    @Override
    public void characters(char ch[], int start, int length) {
            if(this.in_lat){
            myParsedExampleDataSet.setExtractedString(new String(ch, start, length));}
            else if(this.in_long){
                myParsedExampleDataSet.setExtractedString2(new String(ch, start,     length));}

}
}

ParsedExampleDataSet.Java:

public class ParsedExampleDataSet extends LocationBasedServicesV2 {
private String extractedString = null;
private String OLDextractedString = null;
String Long;
String Lat;
Double Long2;
Double Lat2;
int Lat3;
int Long3;

public void setExtractedString(String extractedString) {
        Long = extractedString;
        Long2 = Double.parseDouble(Long);
        Long3 =  (int) (Long2 * 1E6);
        /*if (OLDextractedString != null){
        this.extractedString = OLDextractedString + extractedString + ',' ; 
    }
    else
    {
        this.extractedString = extractedString + ',' ;
    }
        OLDextractedString = this.extractedString;*/
}
public void setExtractedString2(String extractedString) {
   Lat = extractedString;
   Lat2 = Double.parseDouble(Lat);
   Lat3 =  (int) (Lat2 * 1E6);
   createAndShowMyItemizedOverlay(Lat3,Long3);
   /*if (OLDextractedString != null){
     * 
        this.extractedString = OLDextractedString + extractedString + '\n' ;    
    }
    else
    {
        this.extractedString = OLDextractedString + '\n' + extractedString  ;
    }
    OLDextractedString = this.extractedString;*/
}
   public int parseInt(){

       return Lat3;

}
}
4

1 回答 1

0

要检查的一件事:您的方法 createAndShowMyItemizedOverlay() 似乎仅在创建 ParsedExampleDataSet 时才被调用 - 这是在您吞下任何异常的 try/catch 中创建的。这可能是您没有看到错误以及当您开始尝试解析 xml 时它停止工作的原因。

于 2011-01-08T13:56:24.793 回答