34

Google Map API V2を使用していて、地図上のカスタムInfoWindowを作成しました。これにはボタンがあります。MarkerInfoWindow

私の問題はOnclicklistener/functioningをそのButton(Dummy)に設定することができません。誰もがこれを解決するためのいくつかのアイデアを私に与えます:

ここに画像の説明を入力してください

コードスニペットは次のとおりです。

public class MarkerView extends FragmentActivity implements OnMarkerClickListener,OnInfoWindowClickListener{

private GoogleMap mMap;
private Marker chennai;
private View infoWindow;
@Override
protected void onCreate(Bundle arg0) {
    super.onCreate(arg0);
    setContentView(R.layout.basic_demo);

    infoWindow=getLayoutInflater().inflate(R.layout.custom_info_contents, null);

    mMap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    chennai=mMap.addMarker(new MarkerOptions().position(new LatLng(13.0810, 80.274)).anchor(2, 1).title("Android").snippet("Snippet").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
    mMap.setInfoWindowAdapter(new CustomInfoAdapter());
    mMap.setOnInfoWindowClickListener(null);
    mMap.setOnMarkerClickListener(this);
    Button dummy=(Button) infoWindow.findViewById(R.id.dummy);
    dummy.setVisibility(View.VISIBLE);
    dummy.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(MarkerView.this, "Dummy Button", Toast.LENGTH_SHORT).show();

        }
    });
}


class CustomInfoAdapter implements InfoWindowAdapter{


    @Override
    public View getInfoContents(Marker arg0) {
        displayView(arg0);
        return infoWindow;
    }

    @Override
    public View getInfoWindow(Marker arg0) {

        return null;
    }


}


public void displayView(Marker arg0) {

    ((ImageView)infoWindow.findViewById(R.id.badge)).setImageResource(R.drawable.arrow);
    ((ImageView)infoWindow.findViewById(R.id.badge)).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(MarkerView.this, "Arrow Image", Toast.LENGTH_SHORT).show();

        }
    });
     ((TextView)infoWindow.findViewById(R.id.title)).setText(arg0.getTitle());
     ((TextView)infoWindow.findViewById(R.id.snippet)).setText(arg0.getTitle());

}


@Override
public boolean onMarkerClick(Marker arg0) {
    if(arg0.equals(chennai)){

        infoWindow.setClickable(false);

    }
    return false;
}


@Override
public void onInfoWindowClick(Marker arg0) {
    Toast.makeText(MarkerView.this, "Info window", Toast.LENGTH_SHORT).show();
}
4

5 に答える 5

35

このリンクの情報ウィンドウクリックイベントを参照してください

情報ウィンドウはライブビューではなく、ビューが画像としてマップ上にレンダリングされます。その結果、ビューに設定したリスナーは無視され、ビューのさまざまな部分のクリックイベントを区別できなくなります。カスタム情報ウィンドウ内に、ボタン、チェックボックス、テキスト入力などのインタラクティブなコンポーネントを配置しないことをお勧めします。

于 2013-01-23T10:19:43.630 に答える
20

カスタムマーカーを実装する必要があります。これは次のようなものです。

custommarker.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:background="#ADD8E6"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal" >

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="5dp"
    android:adjustViewBounds="true"
    android:contentDescription="@string/app_name"
    android:src="@drawable/sabarmati" />

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/snippet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="@android:color/black"
        android:textSize="15sp" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:textColor="@android:color/black"
        android:textSize="10sp"
        android:textStyle="bold" />
</LinearLayout>

</LinearLayout>

アクティビティ:

public class PlacesMapActivity extends android.support.v4.app.FragmentActivity
    implements OnClickListener, LocationListener {
/**
 * Note that this may be null if the Google Play services APK is not
 * available.
 */
ImageButton btn_home;
private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);

    SupportMapFragment fragment =   (SupportMapFragment)getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mMap = fragment.getMap();
    mMap.setMyLocationEnabled(true);

    // mMap = ((SupportMapFragment) getSupportFragmentManager()
    // .findFragmentById(R.id.map)).getMap();

    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.title("First Location");
    markerOptions.snippet("This Is Test Location");

    LatLng latlng = new LatLng(23.0333, 72.6167);

    markerOptions.position(latlng);
    // markerOptions.title("Ahmedabad Cordinat Found here");

    // Marker m = mMap.addMarker(markerOptions);

    ***mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
        @Override
        public View getInfoWindow(Marker arg0) {
            return null;
        }
        @Override
        public View getInfoContents(Marker marker) {
            View myContentView = getLayoutInflater().inflate(
                    R.layout.custommarker, null);
            TextView tvTitle = ((TextView) myContentView
                    .findViewById(R.id.title));
            tvTitle.setText(marker.getTitle());
            TextView tvSnippet = ((TextView) myContentView
                    .findViewById(R.id.snippet));
            tvSnippet.setText(marker.getSnippet());
            return myContentView;
        }
    });***

    mMap.addMarker(new MarkerOptions()
            .position(latlng)
            .title("This is Sabarmati Ashram")
            .snippet("Ahmedabad")
            .icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_RED)));

    mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(getBaseContext(),
                    DetailsOfPlacesActivity.class);
            startActivity(intent);
        }
    });

    btn_home = (ImageButton) findViewById(R.id.activity_map_ibtn_home);
    btn_home.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    });
}
}
于 2013-07-12T12:00:11.227 に答える
0

Googleマップのマーカーに表示されるInfoWindow内のUI要素はクリックできません。したがって、カスタムポップアップを使用することをお勧めします。

于 2015-01-21T10:37:29.277 に答える
0

@TamiLで説明されている理由により、ボタンをクリックすることはできません。
ただし、をクリックすることはできます。そのため、クリック可能にする必要がある( s InfoWindow)のIDを保存し、次のようにマップにaを追加します。MarkerInfoWindowGoogleMap.OnInfoWindowClickListener

map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener()
{
    @Override
    public void onInfoWindowClick(Marker marker)
    {
        // Called when ANY InfoWindow is clicked
    }
});

次に、onInfoWindowClickが呼び出されたら、クリックされたIDを以前に保存したIDと比較し、Markerそれらのいずれかが一致する場合は、Buttonクリックリスナーで実行する必要のあるコードを実行します。

すべてのがクリック可能になるMarker場合は、IDの保存に対処する必要はありません。MarkerInfoWindow

于 2015-05-17T16:36:19.247 に答える
0

この質問のためにサンプルのAndroidStudioプロジェクトをビルドしました。

したがって、クリック可能なボタンやImageViewなどを備えたGoogleマップv2カスタム情報ウィンドウを作成できます。

スクリーンショットを出力する:-

ここに画像の説明を入力してください

ここに画像の説明を入力してください

ここに画像の説明を入力してください

プロジェクトの完全なソースコードをダウンロードするには、ここをクリックし てください

注意: Androidmanifest.xmlにAPIキーを追加する必要があります

于 2016-09-30T11:56:12.660 に答える