如何处理在谷歌地图的多个标志物相同的位置?

如何处理在谷歌地图的多个标志物相同的位置?

问题描述:

我在一个应用程序使用谷歌地图和很可能是多个标记附加到相同的位置,其中每个标记重新present一个人。在这种情况下,用户将不知道有其它标记物/这个特定标记后面的人

I use Google Maps in an app and it is likely that multiple markers are attached to same location where each marker represent a person. In this situation user will not know that there are other markers/persons behind this particular marker.

我环顾四周来处理这种情况,一个问题的SO建议我可以显示一个标志和相关联的所有人员与单一标志物。当用户点击该标记我要显示所有与该标记相关联的用户列表。这是一个很好的解决方法,但我想,以避免显示一种观点认为,主要是隐藏谷歌地图。

I looked around to handle this situation and one question on SO suggests that I can display a single marker and associate all persons with that single marker. When user taps that marker I should display a list of all the user associated with that marker. This is a good workaround but I would want to avoid displaying a view that mostly hides Google Maps.

有没有人使用的任何解决办法在类似的情况?

Has anyone used any workaround in similar situation?

解决方法我用的是为了让用户获得多个标记轻微的想法,以显示与相同位置稍微分开的标记在地图上。

Workaround I used is to display markers with same location little bit apart on map so that user gets a slight idea of multiple marker.

我一直在地图上和他们在地图上的位置跟踪标记,每当我想要添加在地图上标记我要确保没有其他标记显示在相同的位置。如果是的话,那我加一个偏移量,我要添加新的标记的位置。

I keep track of markers on map and their locations on map, and whenever I want to add a marker on map I make sure that no other marker is displayed on same location. If yes, then I add an offset to location of new marker that I want to add.

static final float COORDINATE_OFFSET = 0.00002f; // You can change this value according to your need

下面方法将返回具有要用于新的标记的位置。此方法作为参数的新标志物的当前的经纬度。

Below method returns the location that has to be used for new marker. This method takes as parameters new marker's current latitude and longitude.

// Check if any marker is displayed on given coordinate. If yes then decide
// another appropriate coordinate to display this marker. It returns an
// array with latitude(at index 0) and longitude(at index 1).
private String[] coordinateForMarker(float latitude, float longitude) {

    String[] location = new String[2];

    for (int i = 0; i <= MAX_NUMBER_OF_MARKERS; i++) {

        if (mapAlreadyHasMarkerForLocation((latitude + i
                * COORDINATE_OFFSET)
                + "," + (longitude + i * COORDINATE_OFFSET))) {

            // If i = 0 then below if condition is same as upper one. Hence, no need to execute below if condition.
            if (i == 0)
                continue;

            if (mapAlreadyHasMarkerForLocation((latitude - i
                    * COORDINATE_OFFSET)
                    + "," + (longitude - i * COORDINATE_OFFSET))) {

                continue;

            } else {
                location[0] = latitude - (i * COORDINATE_OFFSET) + "";
                location[1] = longitude - (i * COORDINATE_OFFSET) + "";
                break;
            }

        } else {
            location[0] = latitude + (i * COORDINATE_OFFSET) + "";
            location[1] = longitude + (i * COORDINATE_OFFSET) + "";
            break;
        }
    }

    return location;
}

// Return whether marker with same location is already on map
private boolean mapAlreadyHasMarkerForLocation(String location) {
    return (markerLocation.containsValue(location));
}

在上面的code, markerLocation 是一个HashMap。

In above code, markerLocation is a HashMap.

HashMap<String, String> markerLocation;    // HashMap of marker identifier and its location as a string

这答案有code为Android,但同样的逻辑也适用于iOS的。

This answer has code for android but same logic applies in iOS.