我一直在努力,直到我尝试将它与从我的网页解析的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;
}
}
答案 0 :(得分:0)
要检查的一件事:你的方法createAndShowMyItemizedOverlay()似乎只在创建ParsedExampleDataSet时被调用 - 这是在try / catch中创建的,你吞下任何异常。这可能是您在开始尝试解析xml时没有看到错误以及为什么它停止工作的原因。