我正在创建一个mapView
,其坐标是从XML文件下载的。我已经将坐标解析出来,就像我希望它们一样,但是我知道我很难对它们进行编码,我有两个问题,第一个是什么是为GeoPoints
数组提供点的最佳方法“来自XML文件”?第二个问题是我无法将多边形分开。每个多边形视图我有五个坐标。问题是它将多边形连接成一个多边形。有人可以告诉我我做错了什么,或者我还需要做些什么。以下是我目前的任何帮助将不胜感激。
好的,我对原始问题进行了更新,我使用Laire's question从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();
}
Polygon.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);
}
}
所以我的问题是我需要做些什么来让多边形一次构建一个而不是作为一个?
答案 0 :(得分: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);
在结尾处关闭路径并返回原点。