如何使Google Maps Android标记标题处理多行?现在,它只显示一行
答案 0 :(得分:0)
1)一种简单的方法是在标记标题中使用“ \ n”:
driversMarker = mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.bike_top_left))
.title("Click To Call.\n again nextline")
.snippet("Driver ID: " + key)
.position(location));
2)另一种方法是使用自定义信息窗口适配器:
布局Xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:background="@android:color/white"
>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/txtPickupInfo"
android:text="Pickup Here"
android:textStyle="bold"
/>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/txtPickupSnippet"
android:text="Optional"
android:textStyle="bold"
/>
</LinearLayout>
Java:CustomInfoWindow:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.Marker;
public class CustomInfoWindow implements GoogleMap.InfoWindowAdapter {
View myView;
public CustomInfoWindow(Context context) {
myView=LayoutInflater.from(context).inflate(R.layout.custom_rider_info_window,null);
}
@Override
public View getInfoWindow(Marker marker) {
TextView textPickupTitle = ((TextView)myView.findViewById(R.id.txtPickupInfo));
TextView textPickupSnippet = ((TextView)myView.findViewById(R.id.txtPickupSnippet));
textPickupTitle.setText(marker.getTitle());
textPickupSnippet.setText(marker.getSnippet());
return myView;
}
@Override
public View getInfoContents(Marker marker) {
return null;
}
}
用途:
public class MapActivity extends AppCompatActivity
implements GoogleMap.OnInfoWindowClickListener {
@Override
public void onInfoWindowClick(Marker marker) {
// do something when click the info window.
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setInfoWindowAdapter(new CustomInfoWindow(this));
mMap.setOnInfoWindowClickListener(this);
}
}
设置标记时:
if(driversMarker!=null)
driversMarker.showInfoWindow();