28

Android用のGoogle MAP API V2を統合しました。My Marker の 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>

以下は私が得ているもののスクリーンショットです: ここに画像の説明を入力

ここで、カスタム InfoWindow をマーカーの上部ではなく右側に配置したいと考えています。

すべてのヘルプに感謝します。

前もって感謝します。

4

2 に答える 2

7

編集2:

Google Maps Android API v2 MarkerOptions.infoWindowAnchorの 9 月の更新 (v12 または 3.2.65) から追加されました。かなりの数の新機能があるので、リリースノートもチェックしてください。

編集3:

残念ながら、この追加はまだあなたの問題を解決していません. そのための新しい問題を追加しました。


これは現在不可能です。

ユーザーがマップを移動することを許可されていない場合は、Viewオーバーを配置して回避することをお勧めします。MapFragment可能であれば、「プッシュ技術」を使用するか、 および を使用してポーリングするときに、再配置時にいくつかの悪いラグが表示されonCameraChangeます。Handlermap.getCameraPosition()

透明なアイコンで追加のマーカーを使用して、表示がクリックされたときに情報ウィンドウを表示しようとすると、同じ問題が発生しますが、実際に実装されているのを見るのはもっと楽しいでしょう.

今のところ、右側の情報ウィンドウなしで生活できる場合は、この機能にスターを付けることをお勧めします.

編集:

API v2 の 3.1.36 (rev. 7) 以降、マーカーのアイコンを変更できます。これは、2 つのアイコンを作成できることを意味します。1 つは通常のアイコン、もう 1 つは右側に情報ウィンドウが開いているように見えるアイコンです。デフォルトの情報ウィンドウを開くことを無効にして、代わりにこれを使用するとうまくいく場合があります。

于 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 を投影に変換します)。

google maps v2 androidのマーカーから画面座標を取得する方法

于 2013-05-03T12:17:20.360 に答える