簡體   English   中英

如何為谷歌地圖設置自定義標記標題

[英]How to set a custom marker title for google maps

如何設置包含信息和按鈕的樣式自定義標記標題/代碼段? 我已經有一個自定義標記圖標圖像集。 現在我需要一個自定義彈出窗口,當用戶點擊標記時,該窗口將包含某些信息和一個按鈕。

這是接近我想要實現的目標。

自定義標題/片段示例

自定義標題/片段示例

 LatLng huduma_gpo = new LatLng(-1.280694, 36.818277);
            googleMap.addMarker(new MarkerOptions().position(huduma_gpo).title("Huduma center GPO")).setIcon(BitmapDescriptorFactory.fromBitmap(marker));

            // For zooming automatically to the location of the marker
            googleMap.moveCamera( CameraUpdateFactory.newLatLngZoom(huduma_gpo , 12.0f) );

使用自定義布局設計並將其添加到您的 Google 地圖。 在這里參考下面的示例代碼並自定義您的設計。

窗戶設計:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/custom_info_bubble"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="40dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical">

    <ImageView
        android:id="@+id/badge"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/text_lay"
        android:background="@drawable/icn_right_arrow" />

    <LinearLayout
        android:id="@+id/text_lay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:padding="3dp">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif"
            android:padding="3dp"
            android:singleLine="true"
            android:text="title"
            android:textColor="#ff000000"
            android:textSize="@dimen/text_size_small" />

        <TextView
            android:id="@+id/snippet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:fontFamily="sans-serif"
            android:padding="3dp"
            android:text="45 bays  available"
            android:textColor="@color/green"
            android:textSize="14dp" />
    </LinearLayout>

</RelativeLayout>

窗口適配器的 Java 類:

public class InfoWindowAdapter implements GoogleMap.InfoWindowAdapter, CommonValues, BundleTags {

private View view;

private FragmentActivity myContext;

public InfoWindowAdapter(FragmentActivity aContext) {
    this.myContext = aContext;

    LayoutInflater inflater = (LayoutInflater) myContext.getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    view = inflater.inflate(R.layout.layout_inflate_parking_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) {

    final String title = marker.getTitle();
    final TextView titleUi = ((TextView) view.findViewById(R.id.title));
    if (title != null) {
        titleUi.setText(title);
    } else {
        titleUi.setText("");
        titleUi.setVisibility(View.GONE);
    }

    final String snippet = marker.getSnippet();
    final TextView snippetUi = ((TextView) view
            .findViewById(R.id.snippet));

    if (snippet != null) {

        String[] SnippetArray = snippet.split(SEPARATOR);


        snippetUi.setText(SnippetArray[0]);
    } else {
        snippetUi.setText("");
    }

    return view;
  }
}

這是僅使用 Title 和 Snipped 的簡單答案的鏈接:查看https://stackoverflow.com/a/31629308/7877442

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM