在从xml获取的谷歌地图位置上显示标记

时间:2011-05-11 11:02:11

标签: android

我想在解析后从xml获取谷歌地图位置上的标记。所以我将纬度和经度存储在arrylist中。我想展示,但它更接近

我从geopoint传递lat和long所以请你看看我的代码中的错误

public class XMLParsingExample1 extends MapActivity {
    /**
     * Create Object For SiteList Class
     */
    SitesList sitesList = null;
    private MapController mapController;
    private MapView mapView;
    private LocationManager locationManager;
    private String ss1;
    private String ss2;
    GeoPoint p;

    class MapOverlay extends com.google.android.maps.Overlay {
        @Override
        public boolean draw(Canvas canvas, MapView mapView,
                            boolean shadow, long when) {
            super.draw(canvas, mapView, shadow);

            //---translate the GeoPoint to screen pixels---
            Point screenPts = new Point();
            mapView.getProjection().toPixels(p, screenPts);

            //---add the marker---
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.marker);
            canvas.drawBitmap(bmp, screenPts.x, screenPts.y - 50, null);
            return true;
        }
    }

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /** Create a new layout to display the view */


        try {

            /** Handling XML */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            /** Send URL to parse XML Tags */
            URL sourceUrl = new URL(
                    "http://site4demo.com/artealdiaonline/output.php?lat=-34.6394879&lng=-58.3617837kkj");

            /** Create handler to handle XML Tags ( extends DefaultHandler ) */
            MyXMLHandler myXMLHandler = new MyXMLHandler();
            xr.setContentHandler(myXMLHandler);
            xr.parse(new InputSource(sourceUrl.openStream()));

        } catch (Exception e) {
            System.out.println("XML Pasing Excpetion = " + e);
        }

        /** Get result from MyXMLHandler SitlesList Object */
        sitesList = MyXMLHandler.sitesList;

        ArrayList<Integer> Latitude = new ArrayList<Integer>();
        ArrayList<Integer> Longtitude = new ArrayList<Integer>();
        for (int i = 0; i < sitesList.getName().size(); i++) {

        // **Is this correct??**
            ss1 = sitesList.getName().get(i);

        }
        for (int i = 0; i < sitesList.getWebsite().size(); i++) {
            ss2 = sitesList.getWebsite().get(i);
        }


        RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
        mapView = (MapView) findViewById(R.id.mapview);
        mapView.setBuiltInZoomControls(true);
        //mapView.setStreetView(true);
        mapView.setSatellite(true);
        mapController = mapView.getController();
        mapController.setZoom(14); // Zoon 1 is world view
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
                0, new GeoUpdateHandler());

        //Here i pass latitude and longitude value for displaying in map

        String coordinates[] = {ss1, ss2};
        double lat = Double.parseDouble(coordinates[0]);
        double lng = Double.parseDouble(coordinates[1]);
        p = new GeoPoint(
                (int) (lat * 1E6),
                (int) (lng * 1E6));

        mapController.animateTo(p);
        mapController.setZoom(17);

        //---Add a location marker---
        MapOverlay mapOverlay = new MapOverlay();
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();
    }

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

    public class GeoUpdateHandler implements LocationListener {
        @Override
        public void onLocationChanged(Location location) {
            int lat = (int) (location.getLatitude() * 1E6);
            int lng = (int) (location.getLongitude() * 1E6);
            GeoPoint point = new GeoPoint(lat, lng);
            mapController.animateTo(point); //  mapController.setCenter(point);
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    }
}

网站列表是

public class SitesList {

/** Variables */
private ArrayList<String> Latitude = new ArrayList<String>();
private ArrayList<String> Longitude = new ArrayList<String>();
private ArrayList<String> category = new ArrayList<String>();


/** In Setter method default it will return arraylist 
 *  change that to add  */

public ArrayList<String> getName() {
    return Latitude;
}

public void setName(String name) {
    this.Latitude.add(name);
}

public ArrayList<String> getWebsite() {
    return Longitude;
}

public void setWebsite(String website) {
    this.Longitude.add(website);
}

public ArrayList<String> getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category.add(category);
}

}

1 个答案:

答案 0 :(得分:0)

我至少可以看到一个案例,可以抛出异常。

如果Latitude类中的Longitude和/或SitesList列表中没有任何元素,或者这些列表中的String值无法解析为双倍,以下行将抛出NumberFormatException

double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);