开发者

How to show markers in Google map, latitude and lng obtained from XML URL after parsing

I had done sax XML parsing and now I want to display that lat and lng on map using marker. I am able to display one static marker. I mean I pass lat and lng value to geo point, but how to display all points which is obtained from XML parsing?

This question is very important to me but I am unable to do it during the last two days. I am new in Android and Java, so please help me.

public class XMLparsingExample extends MapActivity {
    String name;
    private String array_latitude[];
    private String array_longitude[];
    SitesList sitesList = null;
    private MapController mapController;
    private MapView mapView;
    private LocationManager locationManager;
    St开发者_Python百科ring value1;
    String value2;
    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);
        try {
            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>();
        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);
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new GeoUpdateHandler());
        /*
         * how to pass here latitude and longitude getting after xml parsing for
         * displaying all lat and long in map
         */
        p = new GeoPoint((int) (32.864 * 1E6), (int) (-117.2353 * 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) {
        }
    }
}

siteslist.java:

public class SitesList {
    /** Variables */
    private ArrayList<String> Latitude = new ArrayList<String>();
    private ArrayList<String> Longitude = new ArrayList<String>();

    /**
     * In Setter method default it will return arraylist change that to add
     */
    public ArrayList<String> getLatitude() {
        return Latitude;
    }

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

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

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


hope I don't missunderstand you, but you can add as many overlays as you want with

.getoverlays().add(geopoint);

edit:

public static ArrayList<MapRouteOverlay> postadded_overlays;

...

String[] lngLat = pairs[0].split(","); // lngLat[0]=longitude lngLat[1]=latitude   lngLat[2]=height
                // src
                GeoPoint startGP = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
                //mMapView01.getOverlays().add(overlayitem);
                GeoPoint gp1;
                GeoPoint gp2 = startGP;
                for(int i1=1;i1<pairs.length;i1++) {
                    lngLat = pairs[i1].split(",");
                    gp1 = gp2;
                    gp2 = new GeoPoint((int)(Double.parseDouble(lngLat[1])*1E6),(int)(Double.parseDouble(lngLat[0])*1E6));
                        postadded_overlays.add(new MapRouteOverlay(gp1,gp2,2,color));

                }`

...

        mapOverlays.addAll(postadded_overlays);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜