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

时间:2013-01-23 08:11:36

标签: android google-maps maps google-maps-android-api-2

我正在使用Google Map API V2,并且已在地图上为InfoWindow创建了自定义Marker。在此InfoWindow我有一个按钮。

我的问题是无法将Onclicklistener /功能设置为该Button(Dummy)。任何人都可以给我一些解决这个问题的想法:

enter image description here

以下是代码段:

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

5 个答案:

答案 0 :(得分:33)

请参阅此link

中的信息窗口点击事件

信息窗口不是实时视图,而是视图在地图上呈现为图像。因此,您在视图上设置的任何侦听器都将被忽略,并且您无法区分视图各个部分上的单击事件。建议您不要在自定义信息窗口中放置交互式组件(如按钮,复选框或文本输入)。

答案 1 :(得分:19)

您必须实现自定义标记;就像这样:

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

答案 2 :(得分:1)

我为这个问题构建了一个示例android工作室项目。

因此,您可以使用可点击按钮或ImageView等创建Google地图v2 Custom Infowindow,

输出屏幕截图: -

enter image description here

enter image description here

enter image description here

下载完整的项目源代码点击 here

请注意:您必须在Androidmanifest.xml中添加API密钥

答案 3 :(得分:0)

Google地图标记上显示的InfoWindow内的UI元素无法点击。所以最好使用自定义弹出窗口。

答案 4 :(得分:0)

由于the reason explained by @TamiL,您无法点击该按钮 但是,您可以点击InfoWindow,然后存储Marker(s)应该可点击的InfoWindow的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的点击监听器中。

如果您Marker所有Marker s&#39;你不必处理保存InfoWindow ID的问题。 $(document).ready(function () { var tInt = 0; $('.resizeDiv').each(function () { d = this.offsetHeight; if (d > tInt) { d = tInt; } }); $('.resizeDiv').each(function () { this.css('height', d + 'px'); this.css('min-height', d + 'px'); this.css('display', 'block'); }); 将是可点击的!