在Google地图上添加视图 [英] Add View over google maps

查看:73
本文介绍了在Google地图上添加视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Google Maps片段中的标记上添加视图即使在旧的不赞成使用的函数中,我也到处搜索,但是无论我使用哪种方法,地图都将始终在我创建的视图上调用canvas.draw()并将其转换为位图图片",但我想要在包含textview的标记上显示气泡,请从用户输入文本以不显示某些文本

I'm trying to add a view over the marker in the google maps fragment I searched everywhere even in the old deprecated functions, but it seems no matter what method I'm using the maps will always call canvas.draw() on the view I'm creating converting it to a bitmap "Picture", but I want to show a bubble over the marker that contains a textview , input text from the user not to show some text

有什么可行的方法吗?

注意:我试图避免创建视图并将其放置在片段中,因为我试图避免计算标记在哪里(如果可能的话)

note: I'm trying to avoid creating view and putting it over the fragment in a realtivelayout because I'm trying to avoid calculating where the marker is if that's even possible

本来我会放一些代码,但是我不能确定这里哪一个是相关的!

I would have put some of my code but I can't decide which is relevant here!

推荐答案

不幸的是,使用当前版本的Maps v2 api的AFAIK只能通过监视标记位置来做到这一点.

Unfortunately, AFAIK with the current version of Maps v2 api there is no other way to do that than by monitoring marker position.

下面是示例代码,您如何执行此操作.

Below is sample code how you can do this.

我在自己的应用中使用了这种方法,效果很好.

I used this aproach in my own app and it works fine.

AbsoluteLayout.LayoutParams overlayLayoutParams;

ViewGroup infoWindowContainer; // your custom info window


private void showInfoWindow(Marker marker) {
    /*
        prepare & show your custom info window 
    */
    trackedPosition = marker.getPosition();
    startPositionUpdater();
}

private void hideInfoWindow() {
    /*
        hide your custom info window
    */
    stopPositionUpdater();
}


private void startPositionUpdater() {
    if (trackedPosition != null && infoWindowContainer.getVisibility() == VISIBLE && map != null) {
        positionUpdaterRunnable = new PositionUpdaterRunnable();
        handler.post(positionUpdaterRunnable);
    }
}

private void stopPositionUpdater() {
    if(positionUpdaterRunnable != null) {
        handler.removeCallbacks(positionUpdaterRunnable);
        positionUpdaterRunnable = null;
    }
}    


private class PositionUpdaterRunnable implements Runnable {
    private int lastXPosition = Integer.MIN_VALUE;
    private int lastYPosition = Integer.MIN_VALUE;

    @Override
    public void run() {
        if (trackedPosition != null && isInfoWindowShown() && map != null && handler != null) {

            Projection projection = map.getProjection();
            Point targetPosition = projection.toScreenLocation(trackedPosition);

            if (lastXPosition != targetPosition.x || lastYPosition != targetPosition.y) {
                overlayLayoutParams.x = targetPosition.x - popupXOffset;
                overlayLayoutParams.y = targetPosition.y - popupYOffset;
                infoWindowContainer.setLayoutParams(overlayLayoutParams);

                lastXPosition = targetPosition.x;
                lastYPosition = targetPosition.y;
            }

            handler.postDelayed(this, INFO_WINDOW_POSITION_REFRESH_INTERVAL);
        }
    }
}

这篇关于在Google地图上添加视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆