Reputation: 11
How to display marker infowindow when map loads? when im clicking the map name a new activity starts where the map loads.It shows the marker,the problem is i want to automatically display also the custom infowindow i created.Here's my code below: I will mark this answer for those who can fix this. thanks.
if (nameList.get(i).equals(selected)){
Marker marker = map.addMarker(new MarkerOptions()
.title(nameList.get(i))
.snippet(addressList.get(i))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_location))
.position(new LatLng(latList.get(i), lngList.get(i))));
marker.showInfoWindow();
//Set Custom InfoWindow
map.setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View myContentsView = getLayoutInflater().inflate(R.layout.map_info, null);
TextView tvTitle = ((TextView)myContentsView.findViewById(R.id.title));
tvTitle.setText(marker.getTitle());
TextView tvSnippet = ((TextView)myContentsView.findViewById(R.id.snippet));
tvSnippet.setText(marker.getSnippet());
TextView infoSnippet = (TextView)myContentsView.findViewById(R.id.moreinfo);
infoSnippet.setText("Click for more details.");
return myContentsView;
}
});
//Open New Activity
map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
Intent intent = new Intent(SelectedPlaceActivity.this, PlaceDescriptionActivity.class);
intent.putExtra("placetitle", marker.getTitle().toString());
intent.putExtra("snippet", marker.getSnippet().toString());
startActivity(intent);
}
});
}
Upvotes: 1
Views: 4610
Reputation: 549
It seems that you need an InfoWindow
adapter. See the following example:
map.setInfoWindowAdapter(new InfoWindowAdapter(){
// Use default InfoWindow frame
@Override
public View getInfoWindow(Marker marker) {
return null;
}
// Defines the contents of the InfoWindow
@Override
public View getInfoContents(Marker marker) {
// Getting view from the layout file info_window_layout
View v = getLayoutInflater().inflate(R.layout.custom_info_window, null);
// Getting reference to the TextView to set title
TextView note = (TextView) v.findViewById(R.id.note);
note.setText(marker.getTitle() );
return v;
}
});
As you can see, you need to create a custom layout
. In my example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:id="@+id/note"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#000000"/>
<ImageButton android:id="@+id/takeMeThere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/note"
android:contentDescription="@string/directions"
android:src="@drawable/ic_action_directions"
android:background="@null"/>
</RelativeLayout>
Hope this helps
Upvotes: 2
Reputation: 21531
According to the documents of Google Maps for Android V2:
An info window allows you to display information to the user when they tap on a marker on a map. By default, an info window is displayed when a user taps on a marker if the marker has a title set. Only one info window is displayed at a time. If a user clicks on another marker, the current window will be hidden and the new info window will be displayed. You can show an info window programmatically by calling showInfoWindow() on the target marker. An info window can be hidden by calling hideInfoWindow().
Upvotes: -1