Abdul Abdul -4 years ago 119
Android Question

implement onClickListener on maps markers

I am using Google maps API V2 to populate a map with a list of places, whenever the user clicks a marker view it should take him to a new activity with more details. however I am having problems with implementing that
here my
MapsActivity.java

@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);
}


}

the error I am getting is "cannot resolve intent" and when I pass "intent" to the onClick method I am getting an error says"cannot override its superclass.

Answer Source

You can use a setOnInfoWindowClickListener() and put your code in onInfoWindowClick() to start a new activity

 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);
        }
    });
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download