我有以下代码,我将2个标记仅用于测试目的。问题一切正常,标记出现现在问题现在进一步定制,以便我可以控制显示所有信息窗口或关闭所有信息窗口。
protected void onPostExecute(String result) {
String s = result.trim();
//Toast.makeText(getApplicationContext(), "restult is"+s, Toast.LENGTH_LONG).show();
String stringSucess = "";
//markers = new Hashtable<String, String>();
int height = 50;
int width = 40;
BitmapDrawable bitmapdraw=(BitmapDrawable)getResources().getDrawable(R.drawable.basgray);
Bitmap b=bitmapdraw.getBitmap();
Bitmap smallMarker = Bitmap.createScaledBitmap(b, width, height, false);
LatLng sydney = new LatLng(-34, 151);
Marker mk = mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble("-34"),Double.parseDouble("151")))
.title("First")
.snippet("")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mk.showInfoWindow();
//mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney").icon(BitmapDescriptorFactory.fromBitmap(smallMarker))).showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
markers.put(mk.getId(), "RED");
Marker mk2 = mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble("-10"),Double.parseDouble("151")))
.title("Second")
.snippet("")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
mk2.showInfoWindow();
//setListAdapter(adapter);
markers.put(mk2.getId(), "Green");
}
在我的另一个功能中,我已经设置了自定义信息窗口,如下所示。在这里,我已经设置了信息窗口将根据下面声明的另一个类进行自定义。
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//mMap.setInfoWindowAdapter(new BalloonAdapter(getLayoutInflater(null)));
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
}
这是我自定义的功能,用于控制信息窗口的颜色和图标。
private class CustomInfoWindowAdapter implements InfoWindowAdapter {
private View view;
public CustomInfoWindowAdapter() {
//view = getLayoutInflater(null).inflate(R.layout.custom_info_window,
// null);
}
@Override
public View getInfoContents(Marker marker) {
if (marker != null
&& marker.isInfoWindowShown()) {
marker.hideInfoWindow();
marker.showInfoWindow();
}
return null;
}
@Override
public View getInfoWindow(final Marker marker) {
//MapActivityFragment1.this.marker = marker;
view = getLayoutInflater(null).inflate(R.layout.custom_info_window,
null);
String url = null;
if (marker.getId() != null && markers != null && markers.size() > 0) {
if ( markers.get(marker.getId()) != null &&
markers.get(marker.getId()) != null) {
url = markers.get(marker.getId());
}
}
final String title = marker.getTitle();
final TextView titleUi = ((TextView) view.findViewById(R.id.title));
if (title != null) {
titleUi.setText(title);
} else {
titleUi.setText("");
}
final String snippet = marker.getSnippet();
final TextView snippetUi = ((TextView) view
.findViewById(R.id.snippet));
if (snippet != null) {
snippetUi.setText(snippet);
} else {
snippetUi.setText("");
}
return view;
}
}
答案 0 :(得分:2)
为什么不保存创建的标记实例?可能在列表中调用一个函数来显示标记上的信息窗口(marker.showInfoWindow()),反之则隐藏它们。
代码片段:创建一个全局变量以保存标记列表。
ArrayList<Marker> markersList = new ArrayList<>();
现在,当您根据代码创建标记时,请将它们添加到列表中。
Marker mk = mMap.addMarker(new MarkerOptions()
.position(new LatLng(Double.parseDouble("-34"),Double.parseDouble("151")))
.title("First")
.snippet("")
.icon(BitmapDescriptorFactory.fromBitmap(smallMarker)));
markersList.add(mk);
现在一个简单的功能就可以了
private void showOrHideInfoWindows(boolean shouldShow){
for(Marker marker:markersList){
if(shouldShow)
marker.showInfoWindow();
else
marker.hideInfoWindow();
}
}
使用信息窗口的一种好方法是(在地图片段中)
View view = getLayoutInflater().inflate(R.layout.custom_info_window, null);
mMap.setInfoWindowAdapter(new CustomInfoWindow(view, this));
mMap.setOnInfoWindowClickListener(this);
自定义类就像
public class CustomInfoWindow implements GoogleMap.InfoWindowAdapter {
View infoWindow;
IMapDelegate mapDelegate;
public CustomInfoWindow(View view, IMapDelegate mapDelegate) {
infoWindow = view;
this.mapDelegate = mapDelegate;
}
@Override
public View getInfoContents(Marker marker) {
displayView(marker);
mapDelegate.onInfoWindowShown(infoWindow);
return infoWindow;
}
private void displayView(final Marker marker) {
((TextView)infoWindow.findViewById(R.id.markerTextView)).setText(marker.getTitle());
}
@Override
public View getInfoWindow(Marker marker) {
return null;
}
}