Search code examples
androidgoogle-maps-android-api-2infowindow

Sending data through InfoWindow to another activity Android


I have a for loop which populate markers on my map. The for loop also contains an OnInfoWindowClickListener so that an activity opens when clicking of the InfoWindow. This activity should display details depending on which InfoWindow is selected. Now the InfoWindow displays different details for different markers as it should but the activity which opens when clicking on the different InfoWindows is displaying the same information every time.

the for loop:

for (final infoToStore details : info) {
        marker = mMap.addMarker(new MarkerOptions()
                .position(new LatLng(Double.parseDouble(details.getLat()), Double.parseDouble(details.getLng())))
        .title(details.getName())
        .snippet(details.getDesc()));

        mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
            @Override
            public void onInfoWindowClick(Marker marker) {
                Intent intent = new Intent(MainActivity.this, InfoWindow.class);
                intent.putExtra(NAME, details.getName());
                intent.putExtra(DESCRIPTION, details.getDesc());
                intent.putExtra(CATEGORY, details.getCat());
                intent.putExtra(IMAGE_URL, details.getUrl());
                intent.putExtra(PUSH_ID, details.getPushID());
                startActivity(intent);
                startActivity(new Intent(MainActivity.this, InfoWindow.class));
            }
        });
    }
}

part of the code of the activity which opens when selecting an InfoWindow:

name = (TextView) findViewById(R.id.name);
    desc = (TextView) findViewById(R.id.desc);
    cat = (TextView) findViewById(R.id.cat);
    image = (ImageView) findViewById(R.id.image);
    report = (Button) findViewById(R.id.report);

    String nameString = returnValueFromBundles (MainActivity.NAME);
    String descString = returnValueFromBundles (MainActivity.DESCRIPTION);
    String catString = returnValueFromBundles (MainActivity.CATEGORY);
    String URLString = returnValueFromBundles (MainActivity.IMAGE_URL);
    final String pushID = returnValueFromBundles(MainActivity.PUSH_ID);

    name.setText(nameString);
    desc.setText(descString);
    cat.setText(catString);


    Picasso.with(getApplicationContext())
            .load(URLString)
            .into(image);

    report.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent email = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailTo","jolan.goburdhun@uom.umail.ac.mu",null));
            email.putExtra(Intent.EXTRA_SUBJECT, "Reporting a service");
            email.putExtra(Intent.EXTRA_TEXT, pushID);
            startActivity(Intent.createChooser(email, "Choose an email client"));
        }
    });

}

private String returnValueFromBundles(String key) {
    Bundle inBundle = getIntent().getExtras();
    String returnedValue = inBundle.get(key).toString();
    return returnedValue;
}

Solution

  • Try this way, You will get different value on InfoWindow Click if your bean (details) have different value.

     for (final infoToStore details : info) {
            marker = mMap.addMarker(new MarkerOptions()
                    .position(new LatLng(Double.parseDouble(details.getLat()), Double.parseDouble(details.getLng())))
                    .title(details.getName())
                    .snippet(details.getDesc()));
             // Attach your bean with marker
            marker.setTag(details);
    
            mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
                @Override
                public void onInfoWindowClick(Marker marker) {
                    //Get Attached bean from marker
                    infoToStore infoAttached = ((infoToStore) marker.getTag());
    
                    Intent intent = new Intent(MainActivity.this, InfoWindow.class);
                    intent.putExtra(NAME, infoAttached.getName());
                    intent.putExtra(DESCRIPTION, infoAttached.getDesc());
                    intent.putExtra(CATEGORY, infoAttached.getCat());
                    intent.putExtra(IMAGE_URL, infoAttached.getUrl());
                    intent.putExtra(PUSH_ID, infoAttached.getPushID());
                    startActivity(intent);
    
                }
            });
        }