2013-01-23 40 views
33

Tôi đang sử dụng Google Map API V2 và tôi đã tạo một tùy chỉnh InfoWindow cho một Marker trên bản đồ.Ở đây InfoWindow tôi có một nút.Tùy chỉnh infowindow trong bản đồ Google android v2

Vấn đề của tôi là không có khả năng thiết lập OnClickListener/có chức năng Nút đó (giả) Bất cứ ai cho tôi một số ý tưởng để giải quyết này:

enter image description here

Dưới đây là đoạn mã:

public class MarkerView extends FragmentActivity implements OnMarkerClickListener,OnInfoWindowClickListener{ 

private GoogleMap mMap; 
private Marker chennai; 
private View infoWindow; 
@Override 
protected void onCreate(Bundle arg0) { 
    super.onCreate(arg0); 
    setContentView(R.layout.basic_demo); 

    infoWindow=getLayoutInflater().inflate(R.layout.custom_info_contents, null); 

    mMap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 
    chennai=mMap.addMarker(new MarkerOptions().position(new LatLng(13.0810, 80.274)).anchor(2, 1).title("Android").snippet("Snippet").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher))); 
    mMap.setInfoWindowAdapter(new CustomInfoAdapter()); 
    mMap.setOnInfoWindowClickListener(null); 
    mMap.setOnMarkerClickListener(this); 
    Button dummy=(Button) infoWindow.findViewById(R.id.dummy); 
    dummy.setVisibility(View.VISIBLE); 
    dummy.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Toast.makeText(MarkerView.this, "Dummy Button", Toast.LENGTH_SHORT).show(); 

     } 
    }); 
} 


class CustomInfoAdapter implements InfoWindowAdapter{ 


    @Override 
    public View getInfoContents(Marker arg0) { 
     displayView(arg0); 
     return infoWindow; 
    } 

    @Override 
    public View getInfoWindow(Marker arg0) { 

     return null; 
    } 


} 


public void displayView(Marker arg0) { 

    ((ImageView)infoWindow.findViewById(R.id.badge)).setImageResource(R.drawable.arrow); 
    ((ImageView)infoWindow.findViewById(R.id.badge)).setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      Toast.makeText(MarkerView.this, "Arrow Image", Toast.LENGTH_SHORT).show(); 

     } 
    }); 
    ((TextView)infoWindow.findViewById(R.id.title)).setText(arg0.getTitle()); 
    ((TextView)infoWindow.findViewById(R.id.snippet)).setText(arg0.getTitle()); 

} 


@Override 
public boolean onMarkerClick(Marker arg0) { 
    if(arg0.equals(chennai)){ 

     infoWindow.setClickable(false); 

    } 
    return false; 
} 


@Override 
public void onInfoWindowClick(Marker arg0) { 
    Toast.makeText(MarkerView.this, "Info window", Toast.LENGTH_SHORT).show(); 
} 
+0

Hãy thử nó, Xem xem = (View) findViewById (R.id.your_custom_info_window_layout); Nút bDummy = (Button) xem \t \t \t \t \t \t .findViewById (R.id.button_dummy); bDummy.setOnclick ... nó có thể giúp bạn – TamiL

+0

Đã cố gắng.Không sử dụng. – SBK

+0

Bạn có thể đăng mã của bạn và tôi sẽ xem xét nó .. – TamiL

Trả lời

32

Vui lòng tham khảo Sự kiện nhấp vào cửa sổ thông tin trong số này link

Cửa sổ thông tin không phải là Chế độ xem trực tiếp, thay vì chế độ xem được hiển thị dưới dạng hình ảnh trên bản đồ. Kết quả là, bất kỳ người nghe nào bạn đặt trên chế độ xem đều bị bỏ qua và bạn không thể phân biệt giữa các sự kiện nhấp chuột trên các phần khác nhau của chế độ xem. Bạn không nên đặt các thành phần tương tác - chẳng hạn như các nút, hộp kiểm hoặc đầu vào văn bản - trong cửa sổ thông tin tùy chỉnh của bạn.

+8

Ah, làm như tôi nói, không phải như tôi làm, huh Google? Google Maps có hai mục tiêu cảm ứng trên InfoWindow của ứng dụng của họ. Một tìm kiếm cho một cái gì đó cung cấp một InfoWindow phân đoạn nơi bạn có thể chạm vào biểu tượng Nav để nhận chỉ đường hoặc văn bản để có thêm thông tin về điểm ưa thích. – Greg

+0

Rất tiếc, nếu chúng tôi chỉ có thể sử dụng dự án MapViewBalloons cho bản đồ cũ api. https://github.com/jgilfelt/android-mapviewballoons –

+0

Điều này giải thích tại sao tôi không thể nhấp vào biểu ngữ AdMob được đặt trong InfoWindowAdapter – Bevor

17

Bạn phải triển khai Điểm đánh dấu tùy chỉnh; Nó là như thế này:

custommarker.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:layout_gravity="center_vertical|center_horizontal" 
android:background="#ADD8E6" 
android:gravity="center_vertical|center_horizontal" 
android:orientation="horizontal" > 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="5dp" 
    android:adjustViewBounds="true" 
    android:contentDescription="@string/app_name" 
    android:src="@drawable/sabarmati" /> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/snippet" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dp" 
     android:textColor="@android:color/black" 
     android:textSize="15sp" /> 

    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:padding="5dp" 
     android:textColor="@android:color/black" 
     android:textSize="10sp" 
     android:textStyle="bold" /> 
</LinearLayout> 

</LinearLayout> 

Hoạt động:

public class PlacesMapActivity extends android.support.v4.app.FragmentActivity 
    implements OnClickListener, LocationListener { 
/** 
* Note that this may be null if the Google Play services APK is not 
* available. 
*/ 
ImageButton btn_home; 
private GoogleMap mMap; 

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

    SupportMapFragment fragment = (SupportMapFragment)getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mMap = fragment.getMap(); 
    mMap.setMyLocationEnabled(true); 

    // mMap = ((SupportMapFragment) getSupportFragmentManager() 
    // .findFragmentById(R.id.map)).getMap(); 

    MarkerOptions markerOptions = new MarkerOptions(); 
    markerOptions.title("First Location"); 
    markerOptions.snippet("This Is Test Location"); 

    LatLng latlng = new LatLng(23.0333, 72.6167); 

    markerOptions.position(latlng); 
    // markerOptions.title("Ahmedabad Cordinat Found here"); 

    // Marker m = mMap.addMarker(markerOptions); 

    ***mMap.setInfoWindowAdapter(new InfoWindowAdapter() { 
     @Override 
     public View getInfoWindow(Marker arg0) { 
      return null; 
     } 
     @Override 
     public View getInfoContents(Marker marker) { 
      View myContentView = getLayoutInflater().inflate(
        R.layout.custommarker, null); 
      TextView tvTitle = ((TextView) myContentView 
        .findViewById(R.id.title)); 
      tvTitle.setText(marker.getTitle()); 
      TextView tvSnippet = ((TextView) myContentView 
        .findViewById(R.id.snippet)); 
      tvSnippet.setText(marker.getSnippet()); 
      return myContentView; 
     } 
    });*** 

    mMap.addMarker(new MarkerOptions() 
      .position(latlng) 
      .title("This is Sabarmati Ashram") 
      .snippet("Ahmedabad") 
      .icon(BitmapDescriptorFactory 
       .defaultMarker(BitmapDescriptorFactory.HUE_RED))); 

    mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() { 

     @Override 
     public void onInfoWindowClick(Marker arg0) { 
      // TODO Auto-generated method stub 
      Intent intent = new Intent(getBaseContext(), 
        DetailsOfPlacesActivity.class); 
      startActivity(intent); 
     } 
    }); 

    btn_home = (ImageButton) findViewById(R.id.activity_map_ibtn_home); 
    btn_home.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      finish(); 
     } 
    }); 
} 
} 
+0

Bí mật được setOnInfoWindowClickListener. Hoàn hảo. – SolArabehety

+2

Làm cách nào để nhận được phần nào của cửa sổ thông tin được nhấp? Tôi không thể thấy nó hoạt động như thế nào. – Barodapride

+0

Bạn là anh hùng của tôi !!! Cảm ơn, bạn đã làm cho ngày của tôi !!! – Cabezas

0

UI yếu tố bên trong InfoWindow xuất hiện trên các dấu hiệu của Google Maps không thể nhấp. Vì vậy, nó tốt hơn để sử dụng up tùy chỉnh.

0

Bạn không thể nhấp vào nút, vì the reason explained by @TamiL.
Tuy nhiên, bạn có thể nhấp vào InfoWindow, vì vậy, lưu trữ ID (s) của Marker (s) mà InfoWindow (s) sẽ có thể nhấp, thêm một GoogleMap.OnInfoWindowClickListener vào bản đồ như thế này:

map.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() 
{ 
    @Override 
    public void onInfoWindowClick(Marker marker) 
    { 
     // Called when ANY InfoWindow is clicked 
    } 
}); 

Sau đó, , khi gọi số onInfoWindowClick, hãy so sánh ID của Marker được nhấp với ID bạn đã lưu trước đó và nếu có bất kỳ mã nào khớp với nhau, hãy thực thi bất kỳ mã nào sẽ được thực thi trong trình nghe nhấp chuột của Button.

Bạn không phải giải quyết việc lưu ID Marker: nếu tất cả các Marker s 'InfoWindow s của bạn sẽ có thể nhấp được!

1

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

Vì vậy, bạn có thể tạo Google Map v2 Custom Infowindow bằng các nút có thể nhấp hoặc ImageView, v.v., Ả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

+1

có thể tải nó lên github hoặc một số repo khác. –

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