2012-05-11 3 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

Google지도에서와 같이 mylocation 이미지를 구현하려는 경우에 맞습니까? – Akram

+0

@Akki 그래, 그게 내가 뭘 시도하는지,하지만 MyLocationOverlay, 그냥 ItemizedOverlay를 확장 클래스를 사용하지 오전 – pindleskin

+0

내 ans을 확인 – 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 값 여기에 페인트는 동그라미에 색상을 부여하는 페인트 클래스의 객체입니다

+0

감사합니다. MyLocationOverlay 클래스가 내 앱을 너무 많이 제한합니다. ItemizedOverlay를 사용하고 싶습니다. – pindleskin

0

나는 비슷한 문제가 있었어.

오버레이가 확장 된 내부 클래스의 void 1 대신 부울 값을 재정의하여 해결했습니다.

그 결과는 다음과 같습니다

//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(); 

으로 원을 구축하고지도보기에서 오버레이에 추가합니다. 그게 다야.

+0

감사합니다.하지만 오버레이가 아닌 ItemizedOverlay를 확장하고 싶습니다. – pindleskin

관련 문제