2011-05-17 114 views
10

我想在地图视图上画一个圆。我希望用户输入半径,对于该半径,我必须在地图上显示圆。之后,我必须在该圆上的某些位置显示标记。在地图视图中绘制一定半径的圆圈android

我知道如何在地图视图上显示标记。

请帮我画地图视图上的圆圈并在该圆圈边界上显示标记。

这对我来说很重要,我想找到在互联网的提示,但我不能做this.please帮我........

在此先感谢..

+0

这个库可以帮助 - https://github.com/i-schuetz/map_areas – Ixx 2014-10-14 21:43:36

回答

8

ItemizedOverlay的实施,从onDraw方法

protected void drawCircle(Canvas canvas, Point curScreenCoords) { 
    curScreenCoords = toScreenPoint(curScreenCoords); 
    int CIRCLE_RADIUS = 50; 
    // Draw inner info window 
    canvas.drawCircle((float) curScreenCoords.x, (float) curScreenCoords.y, CIRCLE_RADIUS, getInnerPaint()); 
    // if needed, draw a border for info window 
    canvas.drawCircle(curScreenCoords.x, curScreenCoordsy, CIRCLE_RADIUS, getBorderPaint()); 
} 

private Paint innerPaint, borderPaint; 

public Paint getInnerPaint() { 
    if (innerPaint == null) { 
     innerPaint = new Paint(); 
     innerPaint.setARGB(225, 68, 89, 82); // gray 
     innerPaint.setAntiAlias(true); 
    } 
    return innerPaint; 
} 

public Paint getBorderPaint() { 
    if (borderPaint == null) { 
     borderPaint = new Paint(); 
     borderPaint.setARGB(255, 68, 89, 82); 
     borderPaint.setAntiAlias(true); 
     borderPaint.setStyle(Style.STROKE); 
     borderPaint.setStrokeWidth(2); 
    } 
    return borderPaint; 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    Point p = new Point(); 
    for(OverlayItem item : items) { 
     drawCircle(canvas, getProjection().toPixels(item.getPoint(), p)); 
    } 
} 
+0

感谢ü,如果您使用的是itemizedoverlay,有平局方法太我会尝试 – 2011-05-17 11:06:22

+0

,因此同样适用 – NickT 2011-05-17 11:08:56

0

如果你把你覆盖的抽签方法,下面的代码,它会在你的MapView

@Override 
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, 
long when) { 
    .... 
    .... 

    Paint lp4; 
    lp4 = new Paint(); 
    lp4.setColor(Color.RED); 
    lp4.setAntiAlias(true); 
    lp4.setStyle(Style.STROKE); 
    canvas.drawCircle(mapView.getWidth()/2, mapView.getHeight()/2, 20, lp4); 

    .... 
    .... 
    mapView.invalidate(); 
} 

的中心画一个圆半径20像素,您应该能够适应它以满足您的需求

+0

可以请你解释我更清楚吗 – 2011-05-17 11:00:28

+0

我假设你有一个MapOverlay类,它扩展了com.google.android.maps.Overlay来绘制你的标记。这将有一个带有mapView和canvas参数的超类方法'draw'。按照描述重写此方法的绘制,然后在画布上绘制圆。 – NickT 2011-05-17 11:05:28

+0

我明白了。谢谢... – 2011-05-17 11:30:05

4

这是不完美的做类似的方法drawCircle,但这里的一些代码,我放在一起,放在一个圆圈在地图上。您可以轻松展开以设置圆圈的颜色等。我所看到的大多数其他代码示例未考虑使用缩放级别缩放圆圈大小,这是创建圆圈时的常见要求。圆半径以米为单位。

import com.google.android.maps.GeoPoint; 
import com.google.android.maps.MapView; 
import com.google.android.maps.Overlay; 
import com.google.android.maps.Projection; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Point; 
public class CircleOverlay extends Overlay { 

    Context context; 
    double mLat; 
    double mLon; 
    float mRadius; 

    public CircleOverlay(Context _context, double _lat, double _lon, float radius) { 
      context = _context; 
      mLat = _lat; 
      mLon = _lon; 
      mRadius = radius; 
    } 

    public void draw(Canvas canvas, MapView mapView, boolean shadow) { 

     super.draw(canvas, mapView, shadow); 

     Projection projection = mapView.getProjection(); 

     Point pt = new Point(); 

     GeoPoint geo = new GeoPoint((int) (mLat *1e6), (int)(mLon * 1e6)); 

     projection.toPixels(geo ,pt); 
     float circleRadius = projection.metersToEquatorPixels(mRadius); 

     Paint innerCirclePaint; 

     innerCirclePaint = new Paint(); 
     innerCirclePaint.setColor(Color.BLUE); 
     innerCirclePaint.setAlpha(25); 
     innerCirclePaint.setAntiAlias(true); 

     innerCirclePaint.setStyle(Paint.Style.FILL); 

     canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, innerCirclePaint); 
    } 
} 
10

如果有人在使用Google Maps API v2查找答案,这里是我所做的一个片段。 这实际上更像是一种地理方法。

public class MapDrawer { 

private GoogleMap map; 
private static int EARTH_RADIUS = 6371000; 

public MapDrawer(GoogleMap map) { 
    this.map = map; 
} 

private LatLng getPoint(LatLng center, int radius, double angle) { 
    // Get the coordinates of a circle point at the given angle 
    double east = radius * Math.cos(angle); 
    double north = radius * Math.sin(angle); 

    double cLat = center.latitude; 
    double cLng = center.longitude; 
    double latRadius = EARTH_RADIUS * Math.cos(cLat/180 * Math.PI); 

    double newLat = cLat + (north/EARTH_RADIUS/Math.PI * 180); 
    double newLng = cLng + (east/latRadius/Math.PI * 180); 

    return new LatLng(newLat, newLng); 
} 

public Polygon drawCircle(LatLng center, int radius) { 
    // Clear the map to remove the previous circle 
    map.clear(); 
    // Generate the points 
    List<LatLng> points = new ArrayList<LatLng>(); 
    int totalPonts = 30; // number of corners of the pseudo-circle 
    for (int i = 0; i < totalPonts; i++) { 
     points.add(getPoint(center, radius, i*2*Math.PI/totalPonts)); 
    } 
    // Create and return the polygon 
    return map.addPolygon(new PolygonOptions().addAll(points).strokeWidth(2).strokeColor(0x700a420b)); 
} 

}

关于这个的好处是,你不必缩放或平移地图后重绘任何东西 - 圆被调整大小并相应地移动。 的缺点是,如果你想在北极或南极一个圈,这并不工作 - 它会都去bezerk,但是,希望这不是案件的99%的时间:)

+0

嗨@Grisha S,我面对的,我要让我的左右标记一个圆圈一个问题,长而当用户放大或缩小缩小,我认为这个代码将工作,但我怎么能调用这个类,什么是发送到u制作函数的参数.....请,如果你看到这个帮助... ......感谢 – 2013-01-16 12:59:45

+1

2 @SalmanKhan:是的,代码应该在你的情况下工作。首先,使用要画上GoogleMap对象实例化类:MapDrawer mapDrawer =新MapDrawer(图)然后调用drawCircle方法给出它:1)一个LatLng对象,用于指定要绘制的圆的中心的地理坐标,2)一个整数,表示以米为单位的圆的半径 – 2013-01-16 14:05:17

+0

首先感谢回复Grisha它对我来说工作得很好,但是每当我将圆圈添加到我的地图中时,所有标记都变得不可见,当我评论对drawCircle方法的调用时,它再次向我展示所有标记,我不知道什么是这个问题,u能帮助我,请..... – 2013-01-17 06:22:03

17

只是为了随时更新...他们让Google Maps API v2的操作变得非常简单。

mMap.addCircle(new CircleOptions() 
     .center(center) 
     .radius(radius) 
     .strokeWidth(0f) 
     .fillColor(0x550000FF)); 

其中半径以米为单位。

至于边界上的标记,这应该是比较容易做到 - 只要按照在谷歌地图的示例代码这里的“圈子”演示:https://developers.google.com/maps/documentation/android/intro#sample_code

+0

罚款,Circle正在绘制中。但我想要绘制该圆的经纬度点,因为我想搜索一些属于该圆圈区域的新属性。任何人都可以帮我解决这个问题! – Mahe 2013-06-14 06:02:07

+0

这应该是被接受的答案人..很好的帮助。 @Mahe不知道。 – Noman 2014-11-24 10:13:52

+0

这对我有效。应该是被接受的答案! – Aci89 2017-07-19 05:21:09