2013-09-02 33 views
16

enter image description hereGoogle bản đồ v2 Tuỳ chỉnh Infowindow với hai nút có thể nhấp hoặc ImageView

Tôi cần cửa sổ thông tin tùy chỉnh với hai nút bấm như above.In nó khi hơn sau đó đánh dấu và nhấp vào bất kỳ một trong số họ sau đó cửa sổ sẽ được mở và khi bấm vào một điểm đánh dấu khác cửa sổ sẽ mở và đóng cửa sổ trước cũng như trong nhấp chuột duy nhất. có lý do cụ thể nào khiến bản đồ google v2 không hỗ trợ thành phần trực tiếp như nút, hộp kiểm không?

+0

Sử dụng tùy chỉnh cửa sổ bật lên cửa sổ sử dụng bộ chuyển đổi, map.setInfoWindowAdapter (PopupAdapterTodays mới (getLayoutInflater (bó), bối cảnh)); –

+2

có điều này là đúng nhưng làm thế nào tôi có thể có được hai hành động khác nhau cho ví dụ hiển thị firstactivity trên nút đầu tiên và secondactivity trên nút thứ hai trong infowindow. –

+0

bạn có thể tạo bố cục tùy chỉnh trong bộ điều hợp ở đó bạn có thể có hai nút với người nghe –

Trả lời

3

Điều bạn đang cố gắng đạt được là không thể. Ngay cả khi bạn sẽ tạo bố cục xml cho bạn thông tin-cửa sổ. Nội dung của cửa sổ thông tin được hiển thị và trình bày dưới dạng hình ảnh trên bản đồ. Vì vậy, nó chỉ có thể chấp nhận một Trình nghe nhấp chuột cho toàn bộ cửa sổ. bạn không thể chỉ định một số nhấp chuột khác nhau cho cửa sổ này.

UPDATE: Từ Docs:

Lưu ý: Cửa sổ thông tin được rút ra không phải là một live view. Chế độ xem được hiển thị dưới dạng hình ảnh (bằng cách sử dụng View.draw (Canvas)) tại thời điểm nó được trả về. Điều này có nghĩa là mọi thay đổi tiếp theo đối với chế độ xem sẽ không được phản ánh bởi cửa sổ thông tin trên bản đồ. Để cập nhật cửa sổ thông tin sau (ví dụ: sau khi hình ảnh đã được tải), hãy gọi showInfoWindow(). Hơn nữa, cửa sổ thông tin sẽ không tôn trọng bất kỳ tương tác nào điển hình cho một chế độ xem thông thường như các sự kiện chạm hoặc cử chỉ. Tuy nhiên, bạn có thể nghe sự kiện nhấp chuột chung trên toàn bộ cửa sổ thông tin như được mô tả trong phần bên dưới.

+0

trong bản đồ google api v1 tôi đã sử dụng lớp phủ tùy chỉnh nhưng trong v2 này không thể –

+0

bạn có thể chỉnh sửa từ tài liệu, những gì bạn đang cố gắng làm là không thể cho V2 –

4

MainActivity.java

@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
public class MainActivity extends Activity { 

    private ViewGroup infoWindow; 
    private TextView infoTitle; 
    private TextView infoSnippet; 
    private Button infoButton1, infoButton2; 
    private OnInfoWindowElemTouchListener infoButtonListener; 

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

    final MapFragment mapFragment = 
     (MapFragment) getFragmentManager().findFragmentById(R.id.map); 
    final MapWrapperLayout mapWrapperLayout = 
     (MapWrapperLayout) findViewById(R.id.map_relative_layout); 
    final GoogleMap map = mapFragment.getMap(); 


    mapWrapperLayout.init(map, getPixelsFromDp(this, 39 + 20)); 

    final Marker ki = map.addMarker(new MarkerOptions() 
     .position(new LatLng(50.08, 14.43)) 
     .icon(BitmapDescriptorFactory 
      .fromResource(R.drawable.circles))); 

    infoWindow = (ViewGroup) getLayoutInflater() 
     .inflate(R.layout.activity_main, null); 
    infoButton1 = (Button) infoWindow.findViewById(R.id.b1); 
    infoButton2 = (Button) infoWindow.findViewById(R.id.b2); 

    infoButtonListener = new OnInfoWindowElemTouchListener(infoButton1, 
     getResources().getDrawable(R.drawable.ic_launcher), 
     getResources().getDrawable(R.drawable.ic_launcher)) { 
     @Override 
     protected void onClickConfirmed(View v, Marker marker) { 
     Toast.makeText(getApplicationContext(), 
      "click on button 1", Toast.LENGTH_LONG).show(); 
     } 
    }; 
    infoButton1.setOnTouchListener(infoButtonListener); 


    infoButtonListener = new OnInfoWindowElemTouchListener(infoButton2, 
     getResources().getDrawable(R.drawable.ic_launcher), 
     getResources().getDrawable(R.drawable.ic_launcher)) { 
     @Override 
     protected void onClickConfirmed(View v, Marker marker) { 
     Toast.makeText(getApplicationContext(), 
      "click on button 2", Toast.LENGTH_LONG).show(); 
     } 
    }; 
    infoButton2.setOnTouchListener(infoButtonListener); 

    infoWindow.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     // TODO Auto-generated method stub 
     } 
    }); 

    map.setInfoWindowAdapter(new InfoWindowAdapter() { 
     @Override 
     public View getInfoWindow(Marker marker) { 
     infoButtonListener.setMarker(marker); 
     mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow); 
     return infoWindow; 
     } 

     @Override 
     public View getInfoContents(Marker marker) { 
     // Setting up the infoWindow with current's marker info 
     return null; 
     } 
    }); 
    ki.showInfoWindow(); 

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(50.08, 14.43), 15)); 
    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null); 
    } 

    public static int getPixelsFromDp(Context context, float dp) { 
    final float scale = context.getResources().getDisplayMetrics().density; 
    return (int) (dp * scale + 0.5f); 
    } 
} 

activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:background="@drawable/marker" > 

     <Button 
      android:id="@+id/b1" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="Button1" 
      android:layout_marginBottom="10dp" /> 

     <Button 
      android:id="@+id/b2" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="Button2" 
      android:layout_marginBottom="10dp" /> 

    </LinearLayout> 
</RelativeLayout> 

tại sao chép các tập tin sau đây từ các liên kết https://stackoverflow.com/a/15040761/2183804

  1. mapwrapperlauot (bao gồm tên gói của bạn trong thẻ)
  2. MapWrapperLayout .java
  3. OnInfoWindowElemTouchListener.java

Nó sẽ hoạt động.

+0

I tin rằng bạn cần phải tạo 'OnInfoWindowElemTouchListener' cho mỗi nút. Mã của bạn có thể hoạt động mà không có ngoại lệ vì bạn chỉ hiển thị cửa sổ bật lên bánh mì nướng mà không có gì để làm với một điểm đánh dấu cụ thể. Nếu bạn cố gắng hiển thị tiêu đề của nhãn hiệu khi nhấp vào 'infoButton1', bạn sẽ nhận được 'nullpointerexception'. – korujzade

1

Tôi đã tạo một dự án studio android mẫu cho câu hỏi này.

ảnh chụp màn hình đầu ra: -

enter image description here

enter image description here

enter image description here

Tải về mã nguồn dự án đầy đủ Bấmhere

Xin lưu ý: bạn phải thêm khóa API của bạn trong AndroidManifest.xml

Các vấn đề liên quan