错误的标题与谷歌地图Android的API V2标志标志、错误、标题、地图

2023-09-05 04:48:11 作者:来者何人

我想开发一些algorythm的在地图上的聚类标记。显示标记的量应取决于当前的缩放级别。如果我表现出从10组一个标记我想设置它的标题为10。问题是,现在有时可见的标记没有标题的所有,我不知道它是如何可能的。 这是我的code:

I'm trying to develop some algorythm for clustering markers on map. Amount of displayed markers should depends on current zoom level. If I show one marker from group of 10 I want to set it's title to "10". The problem is that now sometimes visible markers doesn't have title at all, I've no idea how it's possible. Here is my code:

public class MainActivity extends FragmentActivity {
private ArrayList<Marker> markers = new ArrayList<Marker>();
private Bitmap markerImage;
private float oldZoom = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    markerImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.ic_launcher);

    setContentView(R.layout.activity_main);

    final GoogleMap map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    map.getUiSettings().setMyLocationButtonEnabled(true);
    map.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition cameraPosition) {
            if (cameraPosition.zoom != oldZoom) {
                checkMarkers(map);
            }
            oldZoom = cameraPosition.zoom;
        }
    });
    createMarkers(map);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

private void createMarkers(GoogleMap map) {
    double initLat = 48.462740;
    double initLng = 35.039572;
    for (float i = 0; i < 2; i += 0.2) {
        LatLng pos = new LatLng(initLat + i, initLng);
        Marker marker = map.addMarker(new MarkerOptions()
                .position(pos)
                .icon(BitmapDescriptorFactory.fromBitmap(markerImage)));
        markers.add(marker);
    }
    for (float i = 0; i < 2; i += 0.2) {
        LatLng pos = new LatLng(initLat, initLng + i);
        Marker marker = map.addMarker(new MarkerOptions()
                .position(pos)
                .icon(BitmapDescriptorFactory.fromBitmap(markerImage)));
        markers.add(marker);
    }

    initLat = 40.462740;
    initLng = 30.039572;
    for (float i = 0; i < 2; i += 0.2) {
        LatLng pos = new LatLng(initLat + i, initLng + i);
        Marker marker = map.addMarker(new MarkerOptions()
                .position(pos)
                .icon(BitmapDescriptorFactory.fromBitmap(markerImage)));
        markers.add(marker);
    }

}


private void checkMarkers(GoogleMap map) {
    Projection projection = map.getProjection();
    LatLngBounds bounds = map.getProjection().getVisibleRegion().latLngBounds;
    HashMap<Marker, Point> points = new HashMap<Marker, Point>();
    for (Marker marker : markers) {
        if (bounds.contains(marker.getPosition())) {
            points.put(marker, projection.toScreenLocation(marker.getPosition()));
            marker.setVisible(false);
        }
    }
    CheckMarkersTask checkMarkersTask = new CheckMarkersTask();
    checkMarkersTask.execute(points);
}

private class CheckMarkersTask extends AsyncTask<HashMap<Marker, Point>, Void, HashMap<Point, ArrayList<Marker>>> {


    private double findDistance(float x1, float y1, float x2, float y2) {
        return Math.sqrt(((x1 - x2) * (x1 - x2)) + ((y1 - y2) * (y1 - y2)));
    }

    @Override
    protected HashMap<Point, ArrayList<Marker>> doInBackground(HashMap<Marker, Point>... params) {
        HashMap<Point, ArrayList<Marker>> clusters = new HashMap<Point, ArrayList<Marker>>();
        HashMap<Marker, Point> points = params[0];
        boolean wasClustered;
        for (Marker marker : points.keySet()) {
            Point point = points.get(marker);
            wasClustered = false;
            for (Point existingPoint : clusters.keySet()) {
                if (findDistance(point.x, point.y, existingPoint.x, existingPoint.y) < 25) {
                    wasClustered = true;
                    clusters.get(existingPoint).add(marker);
                    break;
                }
            }
            if (!wasClustered) {
                ArrayList<Marker> markersForPoint = new ArrayList<Marker>();
                markersForPoint.add(marker);
                clusters.put(point, markersForPoint);
            }
        }
        return clusters;
    }

    @Override
    protected void onPostExecute(HashMap<Point, ArrayList<Marker>> clusters) {
        for (Point point : clusters.keySet()) {
            ArrayList<Marker> markersForPoint = clusters.get(point);
            Marker mainMarker = markersForPoint.get(0);
            mainMarker.setTitle(Integer.toString(markersForPoint.size()));
            mainMarker.setVisible(true);
        }
    }

}
}

正如你可以看到所有可见的标记应该有标题,但实际上他们往往没有。任何的IDE有什么不好?

As you can see all visible markers should have title, but actually often they doesn't. Any ides what is wrong?

UPD:我刚刚发现,如果每个CameraChange调用map.clear()和READD标记(更换标题和知名度insteed)一切工作正常。这看起来很奇怪,我

UPD: I've just found that if call map.clear() and readd markers on each CameraChange (insteed of replacing title and visibility) everything works fine. It looks strange for me

推荐答案

我通过清除每个CameraChange事件映射,然后添加需要的标记解决了这个问题。它为我工作正常。

I've solved this problem by clearing map on each CameraChange event and then adding needed markers. It works fine for me.