私の計画は、ユーザーが領域をマークして後で保存するために、マップ上に長方形を描画できるようにすることです。私はそれを行う方法を見つけましたが、それは何もしません。これまでのコードは次のようになります。
public class Map extends MapActivity{
private MapView mvMap;
MapController kontrol;
float xs, ys, xe, ye;
GeoPoint start, end;
private Paint paint = new Paint();
private boolean up = false;
@Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.map);
mvMap = (MapView) findViewById(R.id.mvMap);
mvMap.setBuiltInZoomControls(true);
paint.setStrokeWidth(2.0f);
//
// DrawOverlay t = new DrawOverlay();
// List<Overlay> olList = mvMap.getOverlays();
// olList.add(t);
mvMap.getOverlays().add(new EmptyOverlay());
mvMap.postInvalidate();
kontrol = mvMap.getController();
GeoPoint ja = new GeoPoint(52172722, 21071987);
kontrol.animateTo(ja);
kontrol.setZoom(11);
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
class DrawOverlay extends Overlay{
public boolean onTouchEvent(MotionEvent e, MapView m){
if(up = false){
if(e.getAction() == MotionEvent.ACTION_DOWN){
xs = ys = 0;
xs = e.getX();
ys = e.getY();
start = mvMap.getProjection().fromPixels((int)xs,(int)ys);
//draw(null, m, up);
}
if(e.getAction() == MotionEvent.ACTION_MOVE){
xe = e.getX();
ye = e.getY();
end = mvMap.getProjection().fromPixels((int)xe,(int)ye);
}
if(e.getAction() == MotionEvent.ACTION_UP){
up = true;
}
}
return true;
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
if(start != null && end != null){
//get the 2 geopoints defining the area and transform them to pixels
//this way if we move or zoom the map rectangle will follow accordingly
Point screenPts1 = new Point();
mapView.getProjection().toPixels(start, screenPts1);
Point screenPts2 = new Point();
mapView.getProjection().toPixels(end, screenPts2);
//draw inner rectangle
paint.setColor(0x4435EF56);
paint.setStyle(Style.FILL);
canvas.drawRect(screenPts1.x, screenPts1.y, screenPts2.x, screenPts2.y, paint);
//draw outline rectangle
paint.setColor(0x88158923);
paint.setStyle(Style.STROKE);
canvas.drawRect(screenPts1.x, screenPts1.y, screenPts2.x, screenPts2.y, paint);
}
return true;
}
}
public class EmptyOverlay extends Overlay {
private float x1,y1;
private Overlay overlay = null;
public EmptyOverlay(){
}
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
long when) {
// TODO Auto-generated method stub
return super.draw(canvas, mapView, shadow, when);
}
@Override
public boolean onTouchEvent(MotionEvent e, MapView mapView) {
// if(mv.isEditMode()){
if(e.getAction() == MotionEvent.ACTION_DOWN){
//when user presses the map add a new overlay to the map
//move events will be catched by newly created overlay
x1 = y1 = 0;
x1 = e.getX();
y1 = e.getY();
overlay = new DrawOverlay();
mapView.getOverlays().add(overlay);
}
if(e.getAction() == MotionEvent.ACTION_MOVE){
}
//---when user lifts his finger---
if (e.getAction() == MotionEvent.ACTION_UP) {
}
// return true;
// }
return false;
}
}
どんな助けにも感謝します。