简体   繁体   中英

Google maps Android attaching bundle to marker

I have a google map with markers set from locations in a database, for each marker I have attached a click listener to go to another activity, what I need is each marker to have a bundle of variables from the database to then be used as data in the next screen, such as id, name, biography etc, how can I do this?

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


            Intent intent = new Intent(MapActivity.this,OtherActivity.class);

            Bundle b = new Bundle();

            // Storing data into bundle
            b.putString("name", name);
            b.putLong("phoneNumber", phone);


            intent.putExtras(b);

            startActivity(intent);


        }
    });

You problem is not very clear to me because in your question you say that you have a problem with passing the data from the listener to the activity but in the code you posted the problem is the opposite...

If you want to pass some data from the listener to another activity, you are doing it right, sending with an intent.

Otherwise if your problem is connecting the marker you receive from the listener method and the data associated with it, the things get a bit more complicated because you are not able to extend the Marker class; in this case you have two possibilities (afaik):

  1. each time you add a marker to the map you save that marker in a Map like this Map<Marker, YourDataAsObject> or like this Map<Marker, TheIdOfTheRecordContainingYourData>

  2. you save the id or the data, of the record connected with the marker, inside the marker itself using the method markerOptions.snippet("Your info") or marker.setSnippet("Your info") .

Both this ways you will create a solid connection between a marker and his data.

I apologies for my poor English and i hope that my answer will help you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM