Android 地图自定义叠加层不可见
我正在尝试在 android 中的地图视图上显示多边形。我创建了一个自定义覆盖类(多边形)并覆盖了绘制方法。将多边形实例添加到地图视图的叠加列表后,“应该”显示多边形。但当显示地图时,却找不到叠加层。我缺少什么?这是创建多边形的主要地图活动:
public class GPSLocator extends MapActivity {
MapView mapView;
Polygon polygon;
@Override
protected boolean isRouteDisplayed() {
return false;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
points.add(new GeoPoint((int)(-86.63283601665323*1e6), (int)(34.6857467079488*1e6)));
points.add(new GeoPoint((int)(-86.63172145427183*1e6), (int)(34.68572865382659*1e6)));
points.add(new GeoPoint((int)(-86.63172141228351*1e6), (int)(34.68613493108094*1e6)));
points.add(new GeoPoint((int)(-86.63288804303616*1e6), (int)(34.68611093719812*1e6)));
polygon = new Polygon(points);
mapView.getOverlays().clear();
mapView.getOverlays().add(polygon);
mapView.invalidate();
}
这是自定义叠加多边形:
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.RED);
paint.setStyle(Paint.Style.FILL_AND_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);
}
}
I am trying to display a polygon on a mapview in android. I have created a custom overlay class(polygon) and overridden the draw method. After adding an instance of polygon to the mapview's overlay list a polygon "should" be displayed. But when the map is displayed, there is no overlay to be found. What am I missing? Here is the main map activity where the polygon is created:
public class GPSLocator extends MapActivity {
MapView mapView;
Polygon polygon;
@Override
protected boolean isRouteDisplayed() {
return false;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
ArrayList<GeoPoint> points = new ArrayList<GeoPoint>();
points.add(new GeoPoint((int)(-86.63283601665323*1e6), (int)(34.6857467079488*1e6)));
points.add(new GeoPoint((int)(-86.63172145427183*1e6), (int)(34.68572865382659*1e6)));
points.add(new GeoPoint((int)(-86.63172141228351*1e6), (int)(34.68613493108094*1e6)));
points.add(new GeoPoint((int)(-86.63288804303616*1e6), (int)(34.68611093719812*1e6)));
polygon = new Polygon(points);
mapView.getOverlays().clear();
mapView.getOverlays().add(polygon);
mapView.invalidate();
}
And here is the custom overlay Polygon:
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.RED);
paint.setStyle(Paint.Style.FILL_AND_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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须使用 path.moveTo(pixelPoint.x, PixelPoint.y);在你的绘图循环中。否则你会画出一个星形而不是多边形。这是我用于类似任务的(工作)代码:
这个类是如何实例化的:
You have to use path.moveTo(pixelPoint.x, pixelPoint.y); in your drawing loop. Otherwise you will draw a star not a polygon. Here is my (working) code for similar task:
and how is this this class instaniazed:
正如@piotrpo 在评论中所说:
您的纬度是-86.63283601665323;我在文档中读过这个:
所以你应该避免靠近极点的坐标。
但我不知道这是否是多边形未显示的原因,因为它可能应该自动“修复”坐标而不是让它变形。
As @piotrpo said in a comment:
Your latitude is -86.63283601665323; I have read this in documentation:
so you should avoid coordinates that close to a pole.
But I don't know if that is the reason of your polygon not showing up, since it should probably "fix" the coordinates automatically instead of letting it get malformed.