谷歌地图安卓v2中的自定义信息窗口

34

我正在使用Google Map API V2,并为地图上的标记创建了自定义InfoWindow。在这个InfoWindow里面,我有一个按钮。

我的问题是无法将Onclicklistener/功能设置到那个按钮(虚拟按钮)。请给我一些解决这个问题的思路:

输入图片说明

以下是代码片段:

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();
}

尝试一下,View view = (View) findViewById(R.id.your_custom_info_window_layout); Button bDummy = (Button) view .findViewById(R.id.button_dummy); bDummy.setOnclick... 这可能会对你有所帮助。 - TamiL
是的,已经尝试过了。没有用。 - SBK
你能把你的代码发出来吗?我会进行查看。 - TamiL
请看我的解决方法:https://dev59.com/DGYq5IYBdhLWcg3w4Edx#15040761 - chose007
你有没有得到任何结果? - hasan_shaikh
它能帮到你!我自己测试过了,这个方法可行。 - Dimon
5个回答

35
请参考此链接中的信息窗口点击事件(Info window click events).
信息窗口并不是一个实时的View,相反,该视图被渲染成地图上的一张图片。因此,您设置在视图上的任何监听器都将被忽略,并且您无法区分视图各部分的单击事件。建议您不要在自定义信息窗口中放置交互式组件,例如按钮、复选框或文本输入框。

9
啊,谷歌,你是要做我说的而不是我做的吗?Google Maps 应用程序的 InfoWindow 有两个触摸目标。搜索某些内容会提供一个分段的 InfoWindow,在其中您可以点击导航图标以获取方向或点击文本以获取有关兴趣点的更多信息。 - Greg
可惜,如果我们可以在旧的地图 API 中使用 MapViewBalloons 项目就好了。https://github.com/jgilfelt/android-mapviewballoons - Aaron Dancygier
这就解释了为什么我无法点击放置在InfoWindowAdapter中的AdMob横幅广告。 - Bevor

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();
        }
    });
}
}

秘密是 setOnInfoWindowClickListener。完美。 - SolArabehety
2
这个程序是如何确定信息窗口的哪一部分被点击了呢?我看不出它是如何工作的。 - Barodapride

0

你无法点击按钮,因为@TamiL解释的原因
但是,你可以点击InfoWindow,所以,请存储应该可点击InfoWindowMarker的ID,并向地图添加一个GoogleMap.OnInfoWindowClickListener,如下所示:

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

然后,当调用onInfoWindowClick时,将点击的Marker的ID与之前存储的ID进行比较,如果有任何匹配项,则执行在Button的单击监听器中应该执行的任何代码。

如果您所有的MarkerInfoWindow都是可点击的,则无需保存Marker ID:s!


0

我已经为这个问题建立了一个示例的Android Studio项目。

因此,您可以创建一个带有可点击按钮或ImageView等的Google地图v2自定义信息窗口。

输出屏幕截图:

enter image description here

enter image description here

enter image description here

下载完整项目源代码请点击 这里

请注意:您需要在Androidmanifest.xml中添加您的API密钥


1
也许可以将其上传到GitHub或其他存储库中。 - Hampel Előd

0
Google Maps上标记中出现的InfoWindow内的UI元素不可点击。因此最好使用自定义弹出窗口。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接