28

我已经为 Android 集成了 Google MAP API V2。我想在我的标记的 Onclick 上有一个自定义信息窗口。到此为止还好。我已经整合了它。

我想要什么:我想在标记的右侧而不是标记的顶部显示我的自定义信息窗口。

以下是我正在使用的代码:

public class MainActivity extends FragmentActivity {

    private MainMapFragement mapFragment;
    private HashMap<Marker, EventInfo> eventMarkerMap;


    Marker ThirdMarker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mapFragment = new MainMapFragement();
        // FragmentTransaction ft = getFragmentManager().beginTransaction();
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(R.id.map, mapFragment);
        ft.commit();

    }

    @Override
    protected void onStart() {
        super.onStart();
        setUpEventSpots();
    }

    private void setUpEventSpots() {
        // I'm going to make 2 EventInfo objects and place them on the map
        EventInfo firstEventInfo = new EventInfo(new LatLng(50.154, 4.35),
                "Right now - event", new Date(), "Party");
        EventInfo secondEventInfo = new EventInfo(new LatLng(51.25, 4.15),
                "Future Event", new Date(1032, 5, 25), "Convention");

        EventInfo thirdEventInfo = new EventInfo(new LatLng(23.25, 72.15),
                "Our Next Event", new Date(), "Ahmedabad-India");
        // this date constructor is deprecated but it's just to make a simple
        // example

        Marker firstMarker = mapFragment.placeMarker(firstEventInfo);
        Marker secondMarker = mapFragment.placeMarker(secondEventInfo);
        ThirdMarker = mapFragment.placeMarker(thirdEventInfo);
        eventMarkerMap = new HashMap<Marker, EventInfo>();
        eventMarkerMap.put(firstMarker, firstEventInfo);
        eventMarkerMap.put(secondMarker, secondEventInfo);
        eventMarkerMap.put(ThirdMarker, thirdEventInfo);

        // add the onClickInfoWindowListener
        mapFragment.getMap().setOnInfoWindowClickListener(
                new OnInfoWindowClickListener() {

                    @Override
                    public void onInfoWindowClick(Marker marker) {
                        EventInfo eventInfo = eventMarkerMap.get(marker);
                        Toast.makeText(
                                getBaseContext(),
                                "The date of "
                                        + eventInfo.getName()
                                        + " is "
                                        + eventInfo.getSomeDate()
                                                .toLocaleString(),
                                Toast.LENGTH_LONG).show();

                    }
                });



        // Custom Bhavesh
        mapFragment.getMap().setInfoWindowAdapter(new InfoWindowAdapter() {

            private final View window = getLayoutInflater().inflate(
                    R.layout.ballonoverlly, null);

            @Override
            public View getInfoWindow(Marker marker) {
                EventInfo eventInfo = eventMarkerMap.get(marker);

                String title = marker.getTitle();
                TextView txtTitle = ((TextView) window
                        .findViewById(R.id.textview_name));
                if (title != null) {
                    // Spannable string allows us to edit the formatting of the
                    // text.
                    SpannableString titleText = new SpannableString(title);
                    titleText.setSpan(new ForegroundColorSpan(Color.RED), 0,
                            titleText.length(), 0);
                    txtTitle.setText(titleText);
                } else {
                    txtTitle.setText("");
                }

                // TextView txtType = ((TextView) window
                // .findViewById(R.id.textview_aboutme));
                // if (eventInfo.getType() != null)
                // txtType.setText(eventInfo.getType());

                return window;
            }

            @Override
            public View getInfoContents(Marker marker) {
                // this method is not called if getInfoWindow(Marker) does not
                // return null
                return null;
            }
        });
    }

}

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RlMain"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >


    <LinearLayout
        android:id="@+id/imageview_comment"
        android:layout_width="150dp"
        android:layout_height="62dp"
        android:background="@drawable/map_comment_back"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:scaleType="fitXY"
        android:visibility="visible" >

        <TextView
            android:id="@+id/textview_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:text="Bhavesh Patadiya"
            android:textColor="#FFFFFF"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textview_aboutme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:maxLines="2"
            android:text="How&apos;s Going?"
            android:textColor="#FFFFFF"
            android:textStyle="normal" />
    </LinearLayout>
</LinearLayout>

下面是我得到的屏幕截图: 在此处输入图像描述

现在,我想让我的自定义信息窗口位于标记的右侧而不是顶部。

感谢所有帮助。

提前致谢。

4

2 回答 2

7

编辑2:

从 9 月开始,添加了 Google Maps Android API v2 MarkerOptions.infoWindowAnchor的更新(v12 或 3.2.65) 。有很多新功能,所以也要查看发行说明

编辑3:

不幸的是,这个添加还没有解决你的问题。为此添加了新问题


目前这是不可能的。

如果不允许用户移动地图,尝试通过View覆盖来解决它可能是一个很好的解决方案。MapFragment如果可以的话,在使用“推送技术”或使用 and 进行轮询时,您会看到重新定位的一些严重onCameraChange滞后。Handlermap.getCameraPosition()

如果您尝试使用带有透明图标的附加标记来在单击可见时显示信息窗口,则会出现同样的问题,但看到它实际实现会更有趣。

如果您现在可以在没有右侧信息窗口的情况下生活,我建议您将已请求的此功能加注并等待。

编辑:

从 API v2 的 3.1.36(修订版 7)开始,您可以更改标记的图标。这意味着您可以制作两个图标:一个是普通图标,另一个看起来像是在右侧打开了一个信息窗口。禁用打开默认信息窗口并改用它可能对您有用。

于 2013-05-03T14:37:26.443 回答
0

您可以通过简单地将 marker.getPosition() 转换为 screenPosition ,如下面的代码

Projection projection = map.getProjection();
LatLng markerLocation = marker.getPosition();
Point screenPosition = projection.toScreenLocation(markerLocation);

int x = screenPosition.x +marker. markerImage.getWidth();

您可以参考以下链接以获取更多信息(将 getPosition 转换为投影):

如何从谷歌地图v2 android中的标记获取屏幕坐标

于 2013-05-03T12:17:20.360 回答