当我点击地图的标记时,我陷入困境无法想象为什么信息窗口不显示。我在开发者安卓网站上看到,我应该只添加标记并给它们标题,片段等等。但结果却一无所获。
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;
}
}
默认情况下,如果标记具有标题集,则当用户点击标记时会显示信息窗口。
确保您的.title(bundle.getString("clubNmae")
返回非空值,否则在单击标记时无法看到信息窗口。
它看起来不像你在调用setOnMarkerClickListener。
https://developers.google.com/maps/documentation/android/marker
添加到@ fish40上面的答案:title必须不仅是NotNull而且NotEmpty,即使你传递“”作为标记标题,信息窗口也不会显示。
试试这段代码,它对我有用....
<?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>
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;
}
});
}
我还有另一种情况,请注意,如果你调用GoogleMap.clear(),setInfoWindowAdapter中设置的所有适配器都将消失。调用clear后需要再次设置它们
用作:
GoogleMap mMap;
MarkerOptions options = new MarkerOptions();
Marker marker = mMap.addMarker(options.position(new LatLng(lat,lng)));
marker.showInfoWindow();
showInfoWindow()
方法可以在Marker
对象,而不是MarkerOptions
对象。