jhc
jhc

Reputation: 1759

Linking an object or class to GoogleMap marker

In a GoogleMap app, I am currently using markers to mark ship positions. I have made a Ship class that records many properties such as ship name, latitude, longitude, MMSI code, etc. When a user clicks on one of the many markers on the map, a resulting clickable info window opens. When that user clicks the info window, another Activity is opened showing detailed information about that marker.

Well at least that last sentence is what I want. I can extract latitude and longitude positions but not much else. The problem is, although I have a Ship class with all the information I need, it isn't connected to the marker in any referential way, neither do I know how to do such a thing!

My best idea was to make Ship extend Marker, but that is a final class according to Eclipse IDE, and therefore cannot be extended.

This is my Ship class:

public class Ship {

    /** Ship has the following properties
     *  */
    private String shipname = "";
    private String MMSI = "XXXX";
    private double[] loc = new double[2];

    public Ship(String _shipname, String _MMSI, double[] _loc) {
        shipname = _shipname;
        MMSI = _MMSI;
        loc[0] = _loc[0]; // Latitude
        loc[1] = _loc[1]; // Longitude

    }
    public String getName() {
        return shipname;
    }
    public String getMMSI() {
        return MMSI;
    }
    public double getLat() {
        return loc[0];
    }
    public double getLongi() {
        return loc[1];
    }
}

While, the GoogleMap markers are inserted dynamically via:

public void setupRegion(GoogleMap map, ArrayList<Ship> ships) throws IOException {
        for(Ship ship : ships) {
            map.addMarker(new MarkerOptions()
               .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_red))
               .position(new LatLng(ship.getLat(), ship.getLongi()))
               .title(ship.getName())
               .snippet("Simulate"));
        }
}

Upvotes: 0

Views: 164

Answers (2)

MaciejG&#243;rski
MaciejG&#243;rski

Reputation: 22232

A commonly used solution is to keep a Map<Marker, Ship> in your Activity and do map.get(marker) inside onInfoWindowClick callback (or any other).

You can also try my library, which adds Marker.setData(Object) and Marker.getData() functions, so you can put Ship instances there after you create markers and get them back inside onInfoWindowClick callback.

http://code.google.com/p/android-maps-extensions/

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006674

Instead of populating snippet() with "Simulate", populate it with some unique identifier of your Ship, such that you can look up the Ship in the Dock or Wharf or Shipyard or OnePercenterYachtClub or HashMap<String,Ship> or wherever you store your Ship instances.

You will also need to set up an InfoWindowAdapter, so you can customize your info window contents to not show the snippet, assuming that you are using info windows.

Upvotes: 1

Related Questions