/** * 得到坐标群的中心点坐标 * * @param list * @return */ public LatLng getCenterPointer(List<MyPoint> list) { double maxLng = 0; double maxLat = 0; double minLng = 0; double minLat = 0; for (int i = 0; i < list.size(); i++) { double lat = list.get(i).getLat(); double lng = list.get(i).getLng(); if (i == 0) { minLat = maxLat = lat; minLng = maxLng = lng; continue; } if (minLat > lat) { minLat = lat; } if (minLng > lng) { minLng = lng; } if (maxLat < lat) { maxLng = lat; } if (maxLng < lng) { maxLng = lng; } } LatLng southwest = new LatLng(minLat, minLng); LatLng northeast = new LatLng(maxLat, maxLng); LatLngBounds bounds = new LatLngBounds.Builder().include(northeast).include(southwest).build(); return bounds.getCenter(); }
public void initOverlay() { // add marker overlay addOverlay(39.963175, 116.400244, R.drawable.icon_marka, 9); addOverlay(39.942821, 116.369199, R.drawable.icon_markb, 5); MarkerOptions ooC = (MarkerOptions) addOverlay(39.939723, 116.425541, R.drawable.icon_markc, 7); MarkerOptions ooD = (MarkerOptions) addOverlay(39.906965, 116.401394, R.drawable.icon_markd, 0); ooC.perspective(false).anchor(0.5f, 0.5f).rotate(30); ooD.period(10); // ArrayList<BitmapDescriptor> giflist = new ArrayList<BitmapDescriptor>(); // giflist.add(bdA); // giflist.add(bdB); // giflist.add(bdC); BitmapDescriptor bd = BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding); mBitmapList.add(bd); // add ground overlay LatLng southwest = new LatLng(39.92235, 116.380338); LatLng northeast = new LatLng(39.947246, 116.414977); LatLngBounds bounds = new LatLngBounds.Builder().include(northeast).include(southwest).build(); BitmapDescriptor bdGround = BitmapDescriptorFactory.fromResource(R.drawable.ground_overlay); mBitmapList.add(bdGround); OverlayOptions ooGround = new GroundOverlayOptions().positionFromBounds(bounds).image(bdGround).transparency(0.8f); mBaiduMap.addOverlay(ooGround); MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(bounds.getCenter()); mBaiduMap.setMapStatus(u); mBaiduMap.setOnMarkerDragListener( new OnMarkerDragListener() { public void onMarkerDrag(Marker marker) {} public void onMarkerDragEnd(Marker marker) { Toast.makeText( OverlayDemo.this, "拖拽结束,新位置:" + marker.getPosition().latitude + ", " + marker.getPosition().longitude, Toast.LENGTH_LONG) .show(); } public void onMarkerDragStart(Marker marker) {} }); }
@SuppressLint("NewApi") public void run() { if (clusters.equals(DefaultClusterRenderer.this.mClusters)) { mCallback.run(); return; } final MarkerModifier markerModifier = new MarkerModifier(); final float zoom = mMapZoom; final boolean zoomingIn = zoom > mZoom; final float zoomDelta = zoom - mZoom; final Set<MarkerWithPosition> markersToRemove = mMarkers; final LatLngBounds visibleBounds = mMap.getMapStatus().bound; // TODO: Add some padding, so that markers can animate in from off-screen. // Find all of the existing clusters that are on-screen. These are candidates for // markers to animate from. List<Point> existingClustersOnScreen = null; if (DefaultClusterRenderer.this.mClusters != null && SHOULD_ANIMATE) { existingClustersOnScreen = new ArrayList<Point>(); for (Cluster<T> c : DefaultClusterRenderer.this.mClusters) { if (shouldRenderAsCluster(c) && visibleBounds.contains(c.getPosition())) { Point point = mSphericalMercatorProjection.toPoint(c.getPosition()); existingClustersOnScreen.add(point); } } } // Create the new markers and animate them to their new positions. final Set<MarkerWithPosition> newMarkers = Collections.newSetFromMap(new ConcurrentHashMap<MarkerWithPosition, Boolean>()); for (Cluster<T> c : clusters) { boolean onScreen = visibleBounds.contains(c.getPosition()); if (zoomingIn && onScreen && SHOULD_ANIMATE) { Point point = mSphericalMercatorProjection.toPoint(c.getPosition()); Point closest = findClosestCluster(existingClustersOnScreen, point); if (closest != null) { LatLng animateTo = mSphericalMercatorProjection.toLatLng(closest); markerModifier.add(true, new CreateMarkerTask(c, newMarkers, animateTo)); } else { markerModifier.add(true, new CreateMarkerTask(c, newMarkers, null)); } } else { markerModifier.add(onScreen, new CreateMarkerTask(c, newMarkers, null)); } } // Wait for all markers to be added. markerModifier.waitUntilFree(); // Don't remove any markers that were just added. This is basically anything that had // a hit in the MarkerCache. markersToRemove.removeAll(newMarkers); // Find all of the new clusters that were added on-screen. These are candidates for // markers to animate from. List<Point> newClustersOnScreen = null; if (SHOULD_ANIMATE) { newClustersOnScreen = new ArrayList<Point>(); for (Cluster<T> c : clusters) { if (shouldRenderAsCluster(c) && visibleBounds.contains(c.getPosition())) { Point p = mSphericalMercatorProjection.toPoint(c.getPosition()); newClustersOnScreen.add(p); } } } // Remove the old markers, animating them into clusters if zooming out. for (final MarkerWithPosition marker : markersToRemove) { boolean onScreen = visibleBounds.contains(marker.position); // Don't animate when zooming out more than 3 zoom levels. // TODO: drop animation based on speed of device & number of markers to animate. if (!zoomingIn && zoomDelta > -3 && onScreen && SHOULD_ANIMATE) { final Point point = mSphericalMercatorProjection.toPoint(marker.position); final Point closest = findClosestCluster(newClustersOnScreen, point); if (closest != null) { LatLng animateTo = mSphericalMercatorProjection.toLatLng(closest); markerModifier.animateThenRemove(marker, marker.position, animateTo); } else { markerModifier.remove(true, marker.marker); } } else { markerModifier.remove(onScreen, marker.marker); } } markerModifier.waitUntilFree(); mMarkers = newMarkers; DefaultClusterRenderer.this.mClusters = clusters; mZoom = zoom; mCallback.run(); }