1

我正在创建一个mapView从 XML 文件下载的坐标。GeoPoints我已经像我希望的那样解析了坐标,但是我知道我正在对它们进行硬编码,我有两个问题,第一个是向点提供“来自 XML 文件”数组的最佳方法是什么?第二个问题是我无法将多边形分开。每个多边形视图都有五个坐标。问题是它正在连接多边形,就好像它是一个多边形一样。有人可以告诉我我做错了什么还是我需要做的其他事情。以下是我目前有任何帮助将不胜感激。

好的,我对原始问题进行了更新,我使用Laire 的问题从 xml 中获取了数组部分。因此,所有坐标都从 xml 文件下载到地图,但问题是它们是连接的,就好像它是一组坐标一样。见带多边形的地图

这是我在 mainActivity.java 中的代码

public class MapViewActivity extends MapActivity {
ArrayList<HashMap<String, Object>> boslst;
MapView mapView; 
MapController mapcontrol;
GeoPoint p;
Polygon polygon;

private static final class LatLonPoints extends GeoPoint { 
    public LatLonPoints(double latitude, double longitude) { 
        super((int) (latitude * 1E6), (int) (longitude * 1E6)); 
    } 
} 
public static BigDecimal round(float d, int decimalPlace) {
    BigDecimal bd = new BigDecimal(Float.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);      
    return bd;

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

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



    String coordinates[] = {"35.20418", "-89.86862"};
    double lat = Double.parseDouble(coordinates[0]);
    double lng = Double.parseDouble(coordinates[1]);

    p = new GeoPoint(
            (int) (lat * 1E6), 
            (int) (lng * 1E6));

    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapcontrol = mapView.getController();
    mapcontrol.animateTo(p);
    mapcontrol.setZoom(10);
    mapView.setBuiltInZoomControls(true);

    ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
    try {

        URL url = new URL(
                "my url");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(url.openStream()));
        doc.getDocumentElement().normalize();

        NodeList nodeList = doc.getElementsByTagName("Section");


        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            Element locElement = (Element) node;
            NodeList nameList = locElement.getElementsByTagName("coords");


            int locationCount = nameList.getLength();

        for (int j = 0; j < nameList.getLength(); j++) {

            Node nodel = nameList.item(j);

            Element fstElement = (Element) nodel;
            NodeList nameL = fstElement.getElementsByTagName("coords");
            Element nameE = (Element) nameL.item(0);
            nameL = nameE.getChildNodes();

            String latit = ((Node) nameL.item(0)).getNodeValue();
            String[] latt = latit.split(",");
            BigDecimal latitude = round(Float.parseFloat(latt[0]),5);
            BigDecimal longitude = round(Float.parseFloat(latt[1]),5);


            double Lat = latitude.doubleValue();
            double Long =  longitude.doubleValue();


            points.add(new LatLonPoints(Lat,Long));
            polygon = new Polygon(points);
      }
            polygon = new Polygon(points);

        }
    }catch (Exception e) {
        Log.e("APP","Failed", e);
    }        

    mapView.getOverlays().clear();
    mapView.getOverlays().add(polygon);
    mapView.invalidate();
}

多边形.java

public class Polygon extends Overlay {
ArrayList<GeoPoint> geoPoints;

public Polygon(ArrayList<GeoPoint> points){
    geoPoints = points;
}

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow){
    //Set the color and style
    Paint paint = new Paint();
    paint.setColor(Color.parseColor("#88ff0000"));
    paint.setAlpha(50);
    paint.setStyle(Paint.Style.STROKE);

    //Create path and add points
    Path path = new Path();
    Point firstPoint = new Point();
    mapView.getProjection().toPixels(geoPoints.get(0), firstPoint);
    path.moveTo(firstPoint.x, firstPoint.y);

    for(int i = 1; i < geoPoints.size(); ++i){
        Point nextPoint = new Point();
        mapView.getProjection().toPixels(geoPoints.get(i), nextPoint);
        path.lineTo(nextPoint.x, nextPoint.y);
    }

    //Close polygon
    path.lineTo(firstPoint.x, firstPoint.y);
    path.setLastPoint(firstPoint.x, firstPoint.y);
    canvas.drawPath(path, paint);
    super.draw(canvas, mapView, shadow);

 }
 }

所以我的问题是我该怎么做才能让多边形一次构建一个而不是一个?

4

1 回答 1

0

我相信你需要改变这个:

//Close polygon
path.lineTo(firstPoint.x, firstPoint.y);
path.setLastPoint(firstPoint.x, firstPoint.y);
canvas.drawPath(path, paint);
super.draw(canvas, mapView, shadow);

对此:

    //Close polygon
    path.lineTo(firstPoint.x, firstPoint.y);
    path.setLastPoint(firstPoint.x, firstPoint.y);
    canvas.drawPath(path, paint);
    path.close();
    super.draw(canvas, mapView, shadow);

在最后关闭路径并将您返回到原始点。

于 2013-01-10T02:35:10.383 回答