开发者

How to add a view to a specifc point on mapView

开发者 https://www.devze.com 2023-03-16 18:19 出处:网络
an When an onTouch event occurs on my MapView, i want layout i have specified to be drawn above the where the user has touched

an When an onTouch event occurs on my MapView, i want layout i have specified to be drawn above the where the user has touched

The below is where i have started, i do not know how to actually place the popup view on the x and y values retrieved from the event. Do you guys have any ideas?

@Override
public boolean onTouchEvent(MotionEvent ev, MapView mapView) {

int X = (int)ev.getX();          
int Y = (int)ev.getY();

ViewGroup parent=(ViewGroup)map.getPar开发者_如何转开发ent();
View popup=getLayoutInflater().inflate(R.id.popup, parent, false);

((ViewGroup)map.getParent()).addView(popup);


You can set the MapView.LayoutParams() with the values of x and y and then call the MapView's addView with the LayoutParams.

MapView.LayoutParams params = new MapView.LayoutParams(width, height, x, y, alignment);
mapView.addView(popup, params);


At first, your need one global Point with your X,Y coordinates

@Override
public boolean onTouchEvent(MotionEvent ev, MapView mapView) {
    int X = (int)ev.getX();          
    int Y = (int)ev.getY();

    selectedPoint = new Point(X,Y);
    return true;
}

Now, you should override the onDraw() method to draw you popup view. There are many solutions to do that, I've tried this one with StaticLayout:

@Override
public void draw (Canvas canvas, MapView mapView, boolean shadow){
    super.draw(canvas, mapView, false); 
    if ( selectedPoint != null) {

        // here you should define PopupText, textPaint and width variables

        StaticLayout sl = new StaticLayout(popupText, textPaint, width, 
                    Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);  

        // here you can draw a rectangle — border for your popup text. 
        // You can use sl.getWidth() and sl.getWidth() methods to get the popup dimensions  

        canvas.save();
        canvas.translate(selectedPoint.x, selectedPoint.y);
        sl.draw(canvas);
        canvas.restore()
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消