Reputation: 753
I have my following codes as below where I am putting 2 markers just for testing purpose into my codes. The problem all works fine and the markers appear the issue now is now to customised further so that the I can control to show all info window or close all info window.
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");
}
In my another function I already set for custom info window as below. Here I have already set that the info window will be customised according to another class as declared below.
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//mMap.setInfoWindowAdapter(new BalloonAdapter(getLayoutInflater(null)));
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
}
Here is my customised function to controlled the color and icon for my info window.
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;
}
}
Upvotes: 0
Views: 1750
Reputation: 787
Why dont you save the instances of the markers created? Probably in a list and call a function to show info windows on the markers (marker.showInfoWindow()) and the reverse for hiding them.
Code Snippet: Create a global variable to save the list of markers.
ArrayList<Marker> markersList = new ArrayList<>();
Now when you create a marker as per your code, add them to the list.
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);
Now a simple function will do
private void showOrHideInfoWindows(boolean shouldShow){
for(Marker marker:markersList){
if(shouldShow)
marker.showInfoWindow();
else
marker.hideInfoWindow();
}
}
A good way to use info window is (in map fragment)
View view = getLayoutInflater().inflate(R.layout.custom_info_window, null);
mMap.setInfoWindowAdapter(new CustomInfoWindow(view, this));
mMap.setOnInfoWindowClickListener(this);
And the custom class is like
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;
}
}
Upvotes: 3