2012-05-11 171 views
2

我想圍繞我的位置繪製一個圓圈。我不知道我在做什麼錯,但圓S沒有顯示:圍繞我的位置繪製圓圈

@Override 
public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
    Projection projection = mapView.getProjection(); 

    if (shadow == false && location != null) { 
     // Get the current location 
     Double latitude = location.getLatitude() * 1E6; 
     Double longitude = location.getLongitude() * 1E6; 
     GeoPoint geoPoint = new GeoPoint(latitude.intValue(), 
       longitude.intValue()); 

     int radius = metersToRadius(100, mapView, latitude); 

     // Convert the location to screen pixels 
     Point point = new Point(); 
     projection.toPixels(geoPoint, point); 

     // Setup the paint 
     Paint paint = new Paint(); 
     paint.setAntiAlias(true); 
     paint.setStrokeWidth(2.0f); 

     paint.setColor(0xff6666ff); 
     paint.setStyle(Style.STROKE); 
     canvas.drawCircle(point.x, point.y, radius, paint); 

     paint.setColor(0x186666ff); 
     paint.setStyle(Style.FILL); 
     canvas.drawCircle(point.x, point.y, radius, paint); 
    } 
    super.draw(canvas, mapView, shadow); 
} 

編輯:只是爲了說清楚我要去發佈我的課: CustomItemizedOverlay

public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> { 

protected final List<OverlayItem> mOverlays = new ArrayList<OverlayItem>(); 

protected final Context mContext; 

public CustomItemizedOverlay(Drawable defaultMarker, Context context) { 
    super(boundCenterBottom(defaultMarker)); 
    this.mContext = context; 
} 

public void addOverlay(OverlayItem overlay) { 
    mOverlays.add(overlay); 
    populate(); 
} 

@Override 
protected OverlayItem createItem(int i) { 
    // TODO Auto-generated method stub 
    return mOverlays.get(i); 
} 

public void removeOverlay(OverlayItem overlay) { 
    mOverlays.remove(overlay); 
    populate(); 
} 

public void clear() { 
    mOverlays.clear(); 
    populate(); 
} 

@Override 
public int size() { 
    // TODO Auto-generated method stub 
    return mOverlays.size(); 
} 

@Override 
protected boolean onTap(int i) { 
    OverlayItem itemClicked = this.mOverlays.get(i); 

    AlertDialog.Builder builder = new AlertDialog.Builder(this.mContext); 

    builder.setTitle(itemClicked.getTitle()); 
    builder.setMessage(itemClicked.getSnippet()); 
    builder.setCancelable(true); 

    AlertDialog alert = builder.create(); 
    alert.show(); 

    return true; 
} 

而且PcCustomizedOverlay

public class PcCustomItemizedOverlay extends CustomItemizedOverlay { 

public static int metersToRadius(float meters, MapView map, double latitude) { 
    return (int) (map.getProjection().metersToEquatorPixels(meters) * (1/Math 
      .cos(Math.toRadians(latitude)))); 
} 

private Location location; 

public PcCustomItemizedOverlay(Drawable defaultMarker, Context context) { 
    super(defaultMarker, context); 
} 

@Override 
public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
    Projection projection = mapView.getProjection(); 

    if (shadow == false && location != null) { 
     // Get the current location 
     Double latitude = location.getLatitude() * 1E6; 
     Double longitude = location.getLongitude() * 1E6; 
     GeoPoint geoPoint = new GeoPoint(latitude.intValue(), 
       longitude.intValue()); 

     int radius = metersToRadius(40, mapView, latitude); 

     // Convert the location to screen pixels 
     Point point = new Point(); 
     projection.toPixels(geoPoint, point); 

     // Setup the paint 
     Paint paint = new Paint(); 
     paint.setAntiAlias(true); 
     paint.setStrokeWidth(2.0f); 

     paint.setColor(0xff6666ff); 
     paint.setStyle(Style.STROKE); 
     canvas.drawCircle(point.x, point.y, radius, paint); 

     paint.setColor(0x186666ff); 
     paint.setStyle(Style.FILL); 
     canvas.drawCircle(point.x, point.y, radius, paint); 
    } 
    super.draw(canvas, mapView, shadow); 
} 

public Location getLocation() { 
    return location; 
} 

public void setLocation(Location location) { 
    this.location = location; 
} 

}

有誰知道在哪裏 問題?

非常感謝您

+0

如果我正確的你想要實現mylocation圖像相同的谷歌地圖? – Akram

+0

@Akki是的,這就是我正在嘗試,但我沒有使用MyLocationOverlay,只是一個擴展ItemizedOverlay類 – pindleskin

+0

檢查我的答案下面 – Akram

回答

1

我認爲問題在你void draw

@Override 
public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
    super.draw(canvas, mapView, shadow); 
    Projection projection = mapView.getProjection(); 

    if (shadow == false && location != null) { 
     // Get the current location 
     Double latitude = location.getLatitude() * 1E6; 
     Double longitude = location.getLongitude() * 1E6; 
     GeoPoint geoPoint = new GeoPoint(latitude.intValue(), 
       longitude.intValue()); 

     int radius = metersToRadius(100, mapView, latitude); 

     // Convert the location to screen pixels 
     Point point = new Point(); 
     projection.toPixels(geoPoint, point); 

     // Setup the paint 
     Paint paint = new Paint(); 
     paint.setAntiAlias(true); 
     paint.setStrokeWidth(2.0f); 

     paint.setColor(0xff6666ff); 
     paint.setStyle(Style.STROKE); 
     canvas.drawCircle(point.x, point.y, radius, paint); 

     paint.setColor(0x186666ff); 
     paint.setStyle(Style.FILL); 
     canvas.drawCircle(point.x, point.y, radius, paint); 
    } 

} 

在這裏也可以看看把圖像放在你的地圖上。 drawing image as mapoverlay

+0

有沒有辦法實現它只是ItemizedOverlay? – pindleskin

3

試試這個代碼

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mapView = (MapView) findViewById(R.id.mapview); 
    MapController mc = mapView.getController(); 
    MyLocationOverlay myLocationOverlay = new MyLocationOverlay(MainMap.this, mapView); 
    mapView.getOverlays().add(myLocationOverlay); 
    mc.animateTo(new GeoPoint(lat, lng)); 
    mc.setZoom(15); 
    mapView.invalidate(); 
} 

不要忘記添加overlay.enableMyLocation();在onresume()和overlay.disableMyLocation();在暫停

,而不是上面的代碼,如果你想圍繞你點,你可以使用下面的示例代碼繪製圓:

Point screenPnts =new Point(); 
GeoPoint curr_geopoint = new GeoPoint((int)(location.getLatitude()*1E6),(int)(location.getLongitude()*1E6)); 
mapview.getProjection().toPixels(curr_geopoint, screenPnts); 
canvas.drawCircle(screenPnts.x, screenPnts.y, 15, paint); 

做一些試驗&錯誤通過操縱screenPnts繞過的點圈.x和screenPnts.y值。這裏油漆是Paint類的對象,可以將顏色賦予圓圈

+0

謝謝,但MyLocationOverlay類限制了我的應用太多,我想使用ItemizedOverlay。 – pindleskin

0

我有類似的問題。

解決它與重寫布爾繪製而不是無效的內部類擴展覆蓋。

它應該是這樣的:

//inner class MapOverlay 
class MapOverlay extends com.google.android.maps.Overlay 
{ 
    @Override 
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) 
    { 
     super.draw(canvas, mapView, shadow);     

     Projection projection = mapView.getProjection(); 

     //the rest of your code here................ 

     super.draw(canvas,mapView,shadow); 

     return true; 
    } 
} 

構建你的圈子與

MapOverlay mapOverlayCircle = new MapOverlay(); 

,並在你的MapView添加到您的疊加。而已。

+0

謝謝,但我想擴展ItemizedOverlay,而不是Overlay – pindleskin