在谷歌地图上的位置显示标记从XML获得图上、标记、位置、XML

2023-09-04 06:08:43 作者:@情域

我要显示在分析后从XML获得的谷歌地图的位置标记。所以,我存储在一个arrylist经度和纬度。我想说明,但它fo​​rse接近

I want to show a marker on google map position obtained from xml after parsing. So I store latitude and longitude in an arrylist. I want to show but it forse close

我通过经纬度和长距离的GeoPoint所以请你能看看哪里是错误的,我code

I pass lat and long from geopoint so please can you take a look where is wrong in my code

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) {
        }
    }
}

站点列表

公共类SitesList {

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);
}

}

推荐答案

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

I can see at least one case, where exception can be thrown.

如果您在经纬度没有任何元素 SitesList和/或经度名单类,这些列表或字符串值不能被解析一倍,以下行会抛出 NumberFormatException的

If you have no any elements in Latitude and/or Longitude lists in SitesList class, or String values in those lists can't be parsed to double, the following lines will throw NumberFormatException

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