2016-07-22 130 views
0

使用谷歌地圖api,對於我創建的每個標記,我想擁有一個自定義屬性,它將聲明關於該位置的更多信息。我可以添加一個自定義屬性嗎?還是我必須做一個單獨的課程?Android中的自定義標記屬性

也許這樣?

Marker m = mMap.addMarker(new MarkerOptions() 
      .position(new LatLng(lati, longi)) 
      .title(title) 
      .snippet(snippet) 
      .customProperty1(true)); 

如果我要創建一個新的類,代碼片斷將不勝感激!

+0

你真正想要哪種類型的屬性? –

回答

0

您可以創建自定義標記並在其中添加詳細信息。

您可以在標題傳遞信息或只是片斷,並在您的自定義標記使用

mapView.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 

      // Use default InfoWindow frame 
      @Override 
      public View getInfoWindow(Marker arg0) { 
       return null; 
      } 

      // Defines the contents of the InfoWindow 
      @Override 
      public View getInfoContents(Marker arg0) { 

       // Getting view from the layout file info_window_layout 
       View v = getActivity().getLayoutInflater().inflate(R.layout.layout_mapinfo, null); // my custom view 

       // Getting the position from the marker 
       // LatLng latLng = arg0.getPosition(); 

       // Getting reference to the TextView to set latitude 
       TextView tvLat = (TextView) v.findViewById(R.id.tv_lat); 

       // Getting reference to the TextView to set longitude 
       TextView tvLng = (TextView) v.findViewById(R.id.tv_lng); 

       // Setting the latitude 
       tvLat.setText(arg0.getTitle()); 

       // Setting the longitude 
       AppSession.setIconTypeface(tvLng); 
       tvLng.setText(Html.fromHtml(arg0.getSnippet())); 

       // Returning the view containing InfoWindow contents 
       return v; 

      } 
     }); 

layout_mapinfo.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/tv_lat" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textStyle="bold" 
     android:gravity="center_horizontal" 

     /> 

    <TextView 
     android:id="@+id/tv_lng" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textStyle="bold" 
     android:layout_gravity="center" 
     android:gravity="center" 

     /> 

</LinearLayout> 

如何在標題傳遞價值和摘要,以及如何interprete它在您的自定義視圖中取決於您的要求。

希望這會有所幫助。

注意:此代碼段僅用於示例目的。

+0

嗯我理解,但是如果代碼片段只接受一個字符串值,我該如何通過數組或其他東西在片段中添加多個屬性。你能提供一些樣品嗎? –

+0

是的,您可以在字符串中傳遞逗號分隔值並將其拆分爲適配器中的數組。檢查如何將字符串轉換爲數組@JTey – KDeogharkar