让 Google Maps Android 标记处理多条线

问题描述 投票:0回答:1

如何使 Google Maps Android 标记标题处理多行?现在,它只显示一行

请参阅此标记标题以供参考

java android google-maps google-maps-markers marker
1个回答
0
投票
  1. 一个简单的方法是使用“ “在标记标题中:
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));
  1. 另一种方法是使用自定义信息窗口适配器:

布局 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:自定义信息窗口:

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();
© www.soinside.com 2019 - 2024. All rights reserved.