为什么标记的信息窗口没有显示?

问题描述 投票:8回答:6

当我点击地图的标记时,我陷入困境无法想象为什么信息窗口不显示。我在开发者安卓网站上看到,我应该只添加标记并给它们标题,片段等等。但结果却一无所获。

public class ClubMapActivity extends DefaultActivity implements GoogleMap.OnMarkerClickListener {
    private GoogleMap mMap;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clubmap);
        Bundle bundle = getIntent().getExtras();
        double lat = bundle.getDouble("latitude");
        double lng = bundle.getDouble("longitude");


        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
        mMap.addMarker(new MarkerOptions()
                .position(new LatLng(lat, lng))
                .title(bundle.getString("clubNmae")).snippet("AAA"));

        animateCameraTo(lat, lng);
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(20);

        mMap.animateCamera(zoom);


    }

    public void animateCameraTo(final double lat, final double lng)
    {
        CameraUpdate center=
                CameraUpdateFactory.newLatLng(new LatLng(lat, lng));
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(18);

        mMap.moveCamera(center);
        mMap.animateCamera(zoom);
    }


    @Override
    public boolean onMarkerClick(Marker marker) {
        if(marker.isInfoWindowShown()) {
            marker.hideInfoWindow();
        } else {
            marker.showInfoWindow();
        }
        return true;
    }
}
android google-maps-api-2
6个回答
17
投票

默认情况下,如果标记具有标题集,则当用户点击标记时会显示信息窗口。

确保您的.title(bundle.getString("clubNmae")返回非空值,否则在单击标记时无法看到信息窗口。


1
投票

它看起来不像你在调用setOnMarkerClickListener。

https://developers.google.com/maps/documentation/android/marker


1
投票

添加到@ fish40上面的答案:title必须不仅是NotNull而且NotEmpty,即使你传递“”作为标记标题,信息窗口也不会显示。


0
投票

试试这段代码,它对我有用....

XML文件

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <TextView
                android:id="@+id/showTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@android:color/black"
                android:textSize="20sp"
                android:textStyle="bold"
                android:hint="Current Location"
              />

            <TextView
                android:id="@+id/showLat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Latitude"/>

            <TextView
                android:id="@+id/showLong"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:hint="Longitued"/>
        </LinearLayout>


    </LinearLayout>

JAVA CODE

     if(mMap !=null) {
                mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
                    @Override
                    public View getInfoWindow(Marker marker) {
                        return null;
                    }

                    @Override
                    public View getInfoContents(Marker marker) {

                        View v = getLayoutInflater().inflate(R.layout.info_window,null);
                        TextView showTitle = (TextView)  v.findViewById(R.id.showSnappest);
                        TextView showLat   = (TextView) v.findViewById(R.id.showLat);
                        TextView showLong  = (TextView) v.findViewById(R.id.showLong);

                        LatLng latLng = marker.getPosition();
                        showTitle.setText(marker.getTitle());
                        showLat.setText("Latitude:"+latLng.latitude);
                        showLong.setText("Longitude:"+latLng.longitude);
                        return v;
                    }
                });
            }

0
投票

我还有另一种情况,请注意,如果你调用GoogleMap.clear(),setInfoWindowAdapter中设置的所有适配器都将消失。调用clear后需要再次设置它们


0
投票

用作:

GoogleMap mMap;
MarkerOptions options = new MarkerOptions();
Marker marker = mMap.addMarker(options.position(new LatLng(lat,lng)));
marker.showInfoWindow();

showInfoWindow()方法可以在Marker对象,而不是MarkerOptions对象。

© www.soinside.com 2019 - 2024. All rights reserved.