2013-05-03 130 views
26

右側我對Android的集成谷歌地圖API V2。我想在My Marker的Onclick上有一個自定義信息窗口。直到它沒事。我已經整合了它。Android的谷歌地圖API V2:打開自定義信息窗口上標記

我想要的是:我想在標記的右側顯示我的自定義信息窗口而不是標記的頂部。

下面是我使用的代碼:

public class MainActivity extends FragmentActivity { 

    private MainMapFragement mapFragment; 
    private HashMap<Marker, EventInfo> eventMarkerMap; 


    Marker ThirdMarker; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mapFragment = new MainMapFragement(); 
     // FragmentTransaction ft = getFragmentManager().beginTransaction(); 
     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
     ft.add(R.id.map, mapFragment); 
     ft.commit(); 

    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     setUpEventSpots(); 
    } 

    private void setUpEventSpots() { 
     // I'm going to make 2 EventInfo objects and place them on the map 
     EventInfo firstEventInfo = new EventInfo(new LatLng(50.154, 4.35), 
       "Right now - event", new Date(), "Party"); 
     EventInfo secondEventInfo = new EventInfo(new LatLng(51.25, 4.15), 
       "Future Event", new Date(1032, 5, 25), "Convention"); 

     EventInfo thirdEventInfo = new EventInfo(new LatLng(23.25, 72.15), 
       "Our Next Event", new Date(), "Ahmedabad-India"); 
     // this date constructor is deprecated but it's just to make a simple 
     // example 

     Marker firstMarker = mapFragment.placeMarker(firstEventInfo); 
     Marker secondMarker = mapFragment.placeMarker(secondEventInfo); 
     ThirdMarker = mapFragment.placeMarker(thirdEventInfo); 
     eventMarkerMap = new HashMap<Marker, EventInfo>(); 
     eventMarkerMap.put(firstMarker, firstEventInfo); 
     eventMarkerMap.put(secondMarker, secondEventInfo); 
     eventMarkerMap.put(ThirdMarker, thirdEventInfo); 

     // add the onClickInfoWindowListener 
     mapFragment.getMap().setOnInfoWindowClickListener(
       new OnInfoWindowClickListener() { 

        @Override 
        public void onInfoWindowClick(Marker marker) { 
         EventInfo eventInfo = eventMarkerMap.get(marker); 
         Toast.makeText(
           getBaseContext(), 
           "The date of " 
             + eventInfo.getName() 
             + " is " 
             + eventInfo.getSomeDate() 
               .toLocaleString(), 
           Toast.LENGTH_LONG).show(); 

        } 
       }); 



     // Custom Bhavesh 
     mapFragment.getMap().setInfoWindowAdapter(new InfoWindowAdapter() { 

      private final View window = getLayoutInflater().inflate(
        R.layout.ballonoverlly, null); 

      @Override 
      public View getInfoWindow(Marker marker) { 
       EventInfo eventInfo = eventMarkerMap.get(marker); 

       String title = marker.getTitle(); 
       TextView txtTitle = ((TextView) window 
         .findViewById(R.id.textview_name)); 
       if (title != null) { 
        // Spannable string allows us to edit the formatting of the 
        // text. 
        SpannableString titleText = new SpannableString(title); 
        titleText.setSpan(new ForegroundColorSpan(Color.RED), 0, 
          titleText.length(), 0); 
        txtTitle.setText(titleText); 
       } else { 
        txtTitle.setText(""); 
       } 

       // TextView txtType = ((TextView) window 
       // .findViewById(R.id.textview_aboutme)); 
       // if (eventInfo.getType() != null) 
       // txtType.setText(eventInfo.getType()); 

       return window; 
      } 

      @Override 
      public View getInfoContents(Marker marker) { 
       // this method is not called if getInfoWindow(Marker) does not 
       // return null 
       return null; 
      } 
     }); 
    } 

} 

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RlMain" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 


    <LinearLayout 
     android:id="@+id/imageview_comment" 
     android:layout_width="150dp" 
     android:layout_height="62dp" 
     android:background="@drawable/map_comment_back" 
     android:gravity="center_vertical" 
     android:orientation="vertical" 
     android:scaleType="fitXY" 
     android:visibility="visible" > 

     <TextView 
      android:id="@+id/textview_name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="10dp" 
      android:text="Bhavesh Patadiya" 
      android:textColor="#FFFFFF" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/textview_aboutme" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="10dp" 
      android:maxLines="2" 
      android:text="How&apos;s Going?" 
      android:textColor="#FFFFFF" 
      android:textStyle="normal" /> 
    </LinearLayout> 
</LinearLayout> 

下面是我所得到的屏幕截圖: enter image description here

現在,我想將我的自定義InfoWindow放在標記的右側而不是頂部。

所有幫助表示讚賞。

在此先感謝。

+0

試試這個:根據u - >創建一個圖像(背景),左半邊空白和右半邊,然後在右側添加內容(TextView)。我希望,因爲左側看起來空白 – Maddy 2013-06-05 11:20:01

回答

0

您可以通過簡單的轉換marker.getPosition()做screenPosition像下面的代碼

Projection projection = map.getProjection(); 
LatLng markerLocation = marker.getPosition(); 
Point screenPosition = projection.toScreenLocation(markerLocation); 

int x = screenPosition.x +marker. markerImage.getWidth(); 

您可以參考以下鏈接查看詳細信息(其轉化爲getPosition投影):

How to get screen coordinates from marker in google maps v2 android

+0

@BhaveshPatadiya,你這個做將需要標記的吧?我想嘗試將信息窗口放置在屏幕頂部。 – andrefadila 2013-08-26 00:56:34

+0

我如何在地圖上放置一個視圖?當我使用的代碼'inflater.inflate(R.id.overlay_layout,(ViewGroup中)myViewGroup)'它確實表明對標記的看法,但我也有一個問題,當佈局沿線兩側放置在地圖上的一個片段屏幕視圖獲取包梁也就是說,如果TextView的是有文字textview1然後一些文本是在一行和一些文字是在第2 line.Can有人能幫我解決這個 – JAPS 2014-08-21 06:50:07

6

編輯2:

從九月更新(V12或65年2月3日)開始Google地圖Android API v2 MarkerOptions.infoWindowAnchor已添加。有相當多的新功能,所以請release notes了。

編輯3:

可惜的是這除了沒有解決你的問題呢。爲此增加了new issue


這當前不可能。

如果不允許用戶移動地圖,試圖通過將View設置爲MapFragment來解決此問題可以是一個很好的解決方案。如果可以的話,你會使用或者「推理工科」 onCameraChange或輪詢Handlermap.getCameraPosition()當看到重新定位一些不好的滯後。

同樣的問題,如果您嘗試使用額外的標記與透明的圖標顯示在單擊時可見的信息窗口,但它會更有趣地看到它實際上執行。

如果能夠正常右邊信息窗口生活,現在,我會建議主演this feature already requested和等待。

編輯:

作爲3.1.36(REV。7)可以更改API v2的標記圖標。這意味着您可以製作兩個圖標:一個正常,另一個看起來像右側打開了一個信息窗口。禁用打開默認信息窗口並使用它可能適用於您。

相關問題