How to handle multiple markers on Google Maps with same location?

前端 未结 5 666
遇见更好的自我
遇见更好的自我 2020-12-24 13:43

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 t

5条回答
  •  半阙折子戏
    2020-12-24 14:10

    Inspired by the Geek's answer, I created a function. This function returns a new latitude longitude pair, nearby the original value, almost in a circular fashion. COORINDATE_OFFSET is adjustable based on the need.

    private static final float COORDINATE_OFFSET = 0.000085f;
    private ArrayList markerCoordinates = new ArrayList<>();
    private int offsetType = 0;
    
    private LatLng getLatLng(LatLng latLng) {
    
        LatLng updatedLatLng;
    
        if (markerCoordinates.contains(latLng)) {
            double latitude = 0;
            double longitude = 0;
            if (offsetType == 0) {
                latitude = latLng.latitude + COORDINATE_OFFSET;
                longitude = latLng.longitude;
            } else if (offsetType == 1) {
                latitude = latLng.latitude - COORDINATE_OFFSET;
                longitude = latLng.longitude;
            } else if (offsetType == 2) {
                latitude = latLng.latitude;
                longitude = latLng.longitude + COORDINATE_OFFSET;
            } else if (offsetType == 3) {
                latitude = latLng.latitude;
                longitude = latLng.longitude - COORDINATE_OFFSET;
            } else if (offsetType == 4) {
                latitude = latLng.latitude + COORDINATE_OFFSET;
                longitude = latLng.longitude + COORDINATE_OFFSET;
            }
            offsetType++;
            if (offsetType == 5) {
                offsetType = 0;
            }
    
    
            updatedLatLng = getLatLng(new LatLng(latitude, longitude));
    
        } else {
            markerCoordinates.add(latLng);
            updatedLatLng = latLng;
        }
        return updatedLatLng;
    }
    

提交回复
热议问题