2011-05-17 164 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