2013-02-26 82 views
39

我想在android中的地圖v2中製作自定義信息窗口適配器,如下所示。自定義信息窗口適配器與自定義數據在地圖v2

enter image description here

我看到下面的鏈接,但並沒有得到更多。下面

12,3

是我的內容佈局文件。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
> 
    <ImageView 
     android:id="@+id/infocontent_iv_image" 
     android:layout_width="150dp" 
     android:layout_height="150dp" 
     android:layout_alignParentTop="true" 
    /> 
    <RelativeLayout 
     android:id="@+id/infocontent_rl_middle" 
     android:layout_below="@id/infocontent_iv_image" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
    > 

    </RelativeLayout> 
    <TextView 
     android:id="@+id/infocontent_tv_name" 
     android:layout_below="@id/infocontent_rl_middle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textStyle="bold" 
     android:layout_margin="5dp" 
    /> 
    <TextView 
     android:id="@+id/infocontent_tv_type" 
     android:layout_below="@id/infocontent_tv_name" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#CCCCCC" 
     android:layout_margin="5dp" 
    /> 
    <TextView 
     android:id="@+id/infocontent_tv_desc" 
     android:layout_below="@id/infocontent_tv_type" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
    /> 
    <TextView 
     android:id="@+id/infocontent_tv_addr" 
     android:layout_below="@id/infocontent_tv_desc" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_margin="5dp" 
    /> 
</RelativeLayout> 

所以任何一個可以幫助我,我怎麼可以設置數據在信息窗口適配器的所有看法?

+0

只是'dialog.dismiss'在x按鈕上點擊,也關閉了設備上的後退按鈕。 – 2013-05-16 07:46:22

+0

嗨Girish!我也想像你一樣做。有沒有可能你的回答很少有解釋或參考。 ?? – TheLittleNaruto 2013-09-17 12:41:04

回答

18

我打開對話框onMarker()點擊。和setContentView(my layout)

我使用下面的代碼。

@Override 
    public boolean onMarkerClick(Marker arg0) { 
     if(arg0.getSnippet() == null){ 
      mMap.moveCamera(CameraUpdateFactory.zoomIn()); 
      return true; 
     } 
     //arg0.showInfoWindow(); 
     final DataClass data = myMapData.get(arg0); 
     final Dialog d = new Dialog(MainActivity.this); 
     d.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     //d.setTitle("Select"); 
     d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); 
     d.setContentView(R.layout.info_content); 
     ivPhoto = (ImageView)d.findViewById(R.id.infocontent_iv_image); 
     AddImageOnWindow executeDownload = new AddImageOnWindow(); 
     final LatLng l = arg0.getPosition(); 
     executeDownload.execute(l); 
     TextView tvName = (TextView)d.findViewById(R.id.infocontent_tv_name); 
     tvName.setText(data.getPlaceName()); 

     TextView tvType = (TextView)d.findViewById(R.id.infocontent_tv_type); 
     tvType.setText("("+data.getPlaceType()+")"); 

     TextView tvDesc = (TextView)d.findViewById(R.id.infocontent_tv_desc); 
     tvDesc.setText(data.getPlaceDesc()); 

     TextView tvAddr = (TextView)d.findViewById(R.id.infocontent_tv_addr); 
     tvAddr.setText(Html.fromHtml(data.getPlaceAddr())); 

d.show(); 
return true; 
+2

您能否詳細說明您的答案? – Sheharyar 2013-08-11 07:59:27

+0

我還沒有打開泡泡。我在標記點擊事件上打開對話框並將對話框中的按鈕。 – 2013-08-12 04:39:22

+2

@GirishBhutiya你如何讓對話框指向標記的頂部? (而不是隨意彈出),tks。 – 2013-10-02 18:11:32

66

試試這個

windowlayout.xml

<?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:orientation="vertical"> 

    <TextView 
     android:id="@+id/tv_lat" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

    <TextView 
     android:id="@+id/tv_lng" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 

MainActivity.java

public class MainActivity extends FragmentActivity { 


GoogleMap googleMap; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Getting reference to the SupportMapFragment of activity_main.xml 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 

    // Getting GoogleMap object from the fragment 
    googleMap = mapFragment.getMap(); 

    // Setting a custom info window adapter for the google map 
    googleMap.setInfoWindowAdapter(new 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 = getLayoutInflater().inflate(R.layout.windowlayout, null); 

      // 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("Latitude:" + latLng.latitude); 

      // Setting the longitude 
      tvLng.setText("Longitude:"+ latLng.longitude); 

      // Returning the view containing InfoWindow contents 
      return v; 

     } 
    }); 

    // Adding and showing marker while touching the GoogleMap 
    googleMap.setOnMapClickListener(new OnMapClickListener() { 

     @Override 
     public void onMapClick(LatLng arg0) { 
      // Clears any existing markers from the GoogleMap 
      googleMap.clear(); 

      // Creating an instance of MarkerOptions to set position 
      MarkerOptions markerOptions = new MarkerOptions(); 

      // Setting position on the MarkerOptions 
      markerOptions.position(arg0); 

      // Animating to the currently touched position 
      googleMap.animateCamera(CameraUpdateFactory.newLatLng(arg0)); 

      // Adding marker on the GoogleMap 
      Marker marker = googleMap.addMarker(markerOptions); 

      // Showing InfoWindow on the GoogleMap 
      marker.showInfoWindow(); 

     } 
    }); 

} 
} 

通過這種方式,你可以創建自定義佈局和實現你想要做的方式。

讓我知道它是否對你有幫助。

+0

我已經添加了我的infowindow適配器的佈局文件。那麼你能幫助我如何設置數據到所有視圖? – 2013-02-27 04:49:33

+0

你是什麼意思的所有意見?查看將是一個,數據會不同? – moDev 2013-02-27 07:50:21

+0

亞,但我有4個textview和一個imageview,所以我可以設置所有值。 – 2013-02-27 07:54:43