2017-03-22 35 views
0

我正在使用Google maps API V2填充地圖列表中的地點,每當用戶單擊標記視圖時,它都應該將其帶到一個新的活動並提供更多詳細信息。但是我有執行該 這裏我 MapsActivity.java在地圖上實現onClickListener標記

@Override 
public View getInfoWindow(Marker marker) { 
    return null; 
    //return prepareInfoView(marker); 
} 

@Override 
public View getInfoContents(Marker marker) { 
    //return null; 
    return prepareInfoView(marker); 

} 

private View prepareInfoView(Marker marker){ 
    //prepare InfoView programmatically 

    Restaurants restaurants = places.get(marker); 
    Log.d(TAG,"restaurants1"+ restaurants.getName()); 

    LinearLayout infoView = new LinearLayout(MapsActivity.this); 
    LinearLayout.LayoutParams infoViewParams = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    infoView.setOrientation(LinearLayout.HORIZONTAL); 
    infoView.setLayoutParams(infoViewParams); 
    ImageView infoImageView = new ImageView(MapsActivity.this); 
    //Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher); 
    Drawable drawable = getResources().getDrawable(android.R.drawable.ic_dialog_map); 
    infoImageView.setImageDrawable(drawable); 
    infoView.addView(infoImageView); 

    LinearLayout subInfoView = new LinearLayout(MapsActivity.this); 
    LinearLayout.LayoutParams subInfoViewParams = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    subInfoView.setOrientation(LinearLayout.VERTICAL); 
    subInfoView.setLayoutParams(subInfoViewParams); 

    TextView subInfoName = new TextView(MapsActivity.this); 
    subInfoName.setText("Name: " +restaurants.getName()); 
    TextView subInfoCost = new TextView(MapsActivity.this); 
    subInfoCost.setText("Cost: " + restaurants.getCost() +"€"); 
    TextView subInfoCuisine = new TextView(MapsActivity.this); 
    subInfoCuisine.setText("Cuisine: " +restaurants.getMenuType()); 
    TextView subInfoPhone = new TextView(MapsActivity.this); 
    subInfoPhone.setText("Phone: " +restaurants.getPhone()); 
    subInfoView.addView(subInfoName); 
    subInfoView.addView(subInfoPhone); 
    subInfoView.addView(subInfoCost); 
    subInfoView.addView(subInfoCuisine); 
    infoView.addView(subInfoView); 
    Intent intent = new Intent(MapsActivity.this, RestaurantDetailsActivity.class); 
    Log.d(TAG, "lat" + restaurants.getLat()); 
    Log.d(TAG, "lng" + restaurants.getLng()); 
    intent.putExtra("id", restaurants.getId()); 
    intent.putExtra("name", restaurants.getName()); 
    intent.putExtra("phone", restaurants.getPhone()); 
    intent.putExtra("lat", restaurants.getLat()); 
    intent.putExtra("lng", restaurants.getLng()); 
    intent.putExtra("address1", restaurants.getAddress1()); 
    intent.putExtra("address2", restaurants.getAddress2()); 
    intent.putExtra("offer", restaurants.getOffer()); 
    intent.putExtra("menu_type", restaurants.getMenuType()); 
    intent.putExtra("phone", restaurants.getPhone()); 
    intent.putExtra("rate", restaurants.getRate()); 
    intent.putExtra("cost", restaurants.getCost()); 
    intent.putExtra("delivery", restaurants.getHasDelivery()); 
    intent.putExtra("parking", restaurants.getHasParking()); 
    intent.putExtra("bar", restaurants.getHasBar()); 
    intent.putExtra("card", restaurants.getHasCards()); 
    intent.putExtra("terrace", restaurants.getHasTerrace()); 
    intent.putExtra("reserve", restaurants.getHasReservation()); 
    intent.putExtra("wifi", restaurants.getHasWifi()); 
    infoView.setOnClickListener(this); 
    return infoView; 


    } 
@Override 
public void onClick(View infoView){ 
    //passing restaurants data to an intent. 

    infoView.getContext().startActivity(intent); 
} 

}

我得到的錯誤是「無法解決的意圖」和問題,當我通過「意圖」的方法的onClick我我得到一個錯誤說「不能覆蓋過。

回答

0

您可以使用setOnInfoWindowClickListener(),並把你的代碼中onInfoWindowClick()啓動一個新的活動

map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() { 
     @Override 
     public void onInfoWindowClick(Marker marker) { 

     Restaurants restaurants = places.get(marker); 
     Log.d(TAG,"restaurants1"+ restaurants.getName()); 
     Intent intent = new Intent(MapsActivity.this, RestaurantDetailsActivity.class); 
     Log.d(TAG, "lat" + restaurants.getLat()); 
     Log.d(TAG, "lng" + restaurants.getLng()); 
     intent.putExtra("id", restaurants.getId()); 
     intent.putExtra("name", restaurants.getName()); 
     startActivity(intent); 
     } 
    }); 
+0

嗨,我不知道應該在哪裏放這段代碼,我試圖把它放在prepareView方法之外,但我得到了一個錯誤,我把它移動到onMapReady(),我得到「無法解析OnInfoWindowClickListener()」 – Abdul

+0

把它放在onCreate()..你也必須模仿聽衆..ALT + ENTER然後導入類 – rafsanahmad007

+0

謝謝!,出於某種原因Android Studio沒有自動導入它。 – Abdul

0

意圖是本地的對象,這就是爲什麼它沒有找到它,讓全球。

Intent intent = new Intent(MapsActivity.this, RestaurantDetailsActivity.class); 

@Override 
public void onClick(View infoView){ 
    //passing restaurants data to an intent. 

    infoView.getContext().startActivity(intent); 
} 

而且還從prepareInfoWindow()方法刪除下面一行:

Intent intent = new Intent(MapsActivity.this, RestaurantDetailsActivity.class); 
+0

我只是做了它,沒有編譯錯誤,但是當我點擊應用程序的窗口視圖時,我沒有收到任何迴應或錯誤。 – Abdul

相關問題