简体   繁体   中英

How can I associate an object with a Google Maps marker?

I am building an Android GPS application which shows the nearest places based on the users current location. I am using a MapFragment and I am placing markers down on the map like this inside an AsyncTask:

protected void onPostExecute(Void result) { 
    super.onPostExecute(result);

    for(int i = 0; i < list.size(); i++)
    {
        HashMap<String,String> location = new HashMap<String,String>();
        location = list.get(i);

        String type = location.get("type");
        int marker = getTypeDrawable(type);

        LatLng position = new LatLng(Float.parseFloat(location.get("lat")), Float.parseFloat(location.get("long")));
         map.addMarker(new MarkerOptions()
        .position(position)
        .title(location.get("name"))
        .snippet(location.get("distance") + location.get("type"))                                  
        .icon(BitmapDescriptorFactory.fromResource(marker)));
        System.out.println(list.get(i));
    }

    Toast.makeText(MainActivity.this, "Finished retrieving nearby locations",
            Toast.LENGTH_SHORT).show();         
}

This is my global variable list which contains all the information of the nearest locatoins: ArrayList<HashMap<String, String>> list;

My plan is to display a popup dialog when I click on the marker, just like the geocaching application 'C:Geo'. My problem is, I don't know how to show the information of that marker inside the dialog. As you can see, I am using the map.addMarker() method but there is no way where I can associate that location with that marker.

I wish to make a popup dialog like this:

@Override
public boolean onMarkerClick(Marker marker) {
 //Get location associated with marker. 
//Fill popup dialog with information associated 
//with location and then invoke the dialog.
}

You need to chose something that identifies the marker and the location uniquely. Maybe that's the title you set on the marker (you'll have some corresponding string in your locations) or the Lat/Lng combo. When the user taps a marker, you'll receive that marker in your onMarkerClick(Marker marker) method , you read the title or lat/lng from the marker and look up the corresponding location from your locations. For example you could iterate through your HashMap of locations and check if that location has the same lat/lng as your marker, then you have the location that correspons with your marker.

There sure are more efficient ways, but you get the idea.

You are on the right track. See my answer here for a discrete example of how to use a HashMap to link an object to a Marker.

Given the format of your desired method, I'd think you could use the hashcode of the marker as a key to access a location. I haven't tried it myself, but my following idea should work (and be more efficient). First, you'd have to change the way you add the marker, to:

Marker locationMarker = map.addMarker(new MarkerOptions()
    .position(position)
    .title(location.get("name"))
    .snippet(location.get("distance") + location.get("type"))                                  
    .icon(BitmapDescriptorFactory.fromResource(marker)));

This assures that you can access the added marker object. Then, have a HashMap with an Integer as a key, and a Location as the resulting object. Store the location with the key of the marker's hashcode, which you can access with: marker.hashCode();

I normally use the getTag() / setTag() methods to save object references, but unfortunately the Marker class doesn't have tags :(

A possible caveat to this way, is that MAYBE the hashCode will collide for 2 markers. I don't know how likely that would be, and that would be something to look into/test.

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