天天看點

Android_百度地圖,點聚合功能,點選事件&&設定聚合數字的背景顔色

API:http://developer.baidu.com/map/android_refer/overview-summary.html

推薦文章:

http://blog.csdn.net/ynxdb2002/article/details/51786246

http://blog.csdn.net/aconghui/article/details/50958715

SDK下載下傳:

http://lbsyun.baidu.com/sdk/download

秘鑰申請:

http://developer.baidu.com/map/geosdk-android-key.htm

總結:

點聚合功能不是更新jar包,而是将下載下傳的sdk項目BaiduMapsApiDemo裡面所需要的src,xml,styles.xml整合。

附:

一:點選mark的時候,顯示額外的資訊。

1.添加mark:

itemsList.add(new MyItem(shop_ll,shop_type,bun));
           

2.設定每個mark

/**
     * 每個Marker點,包含Marker點坐标,圖示和額外資訊
     */
    public class MyItem implements ClusterItem {
        private final LatLng mPosition;
        private String shop_type;
        private Bundle buns;

        public MyItem(LatLng latLng) {
            mPosition = latLng;
        }
        
        public MyItem(LatLng latLng,String shopType,Bundle bun) {
            mPosition = latLng;
            shop_type=shopType;
            buns=bun;
        }

        @Override
        public LatLng getPosition() {
            return mPosition;
        }
        
        @Override
        public Bundle <strong><span style="color:#6600CC;">getExtraInfo</span></strong>() {
            return buns;
        }

        @Override
        public BitmapDescriptor getBitmapDescriptor() {
        	if (shop_type.trim().equals("2")) {
        		return BitmapDescriptorFactory.fromResource(R.drawable.delicacies_logo);
			} else if (shop_type.trim().equals("3")) {
				return BitmapDescriptorFactory.fromResource(R.drawable.shopping_logo);
			} else if (shop_type.trim().equals("4")) {
				return BitmapDescriptorFactory.fromResource(R.drawable.hotel_logo);
			} else if (shop_type.trim().equals("5")) {
				return BitmapDescriptorFactory.fromResource(R.drawable.entertainment_logo);
			} else if (shop_type.trim().equals("6")) {
				return BitmapDescriptorFactory.fromResource(R.drawable.life_service_logo);
			}else{
				return BitmapDescriptorFactory .fromResource(R.drawable.icon_gcoding);
			}
        }
    }
           

3.接口添加新的抽象方法:

public interface ClusterItem {

    /**
     * The position of this marker. This must always return the same value.
     */
    LatLng getPosition();

    BitmapDescriptor getBitmapDescriptor();
    
    Bundle <strong><span style="color:#FF6666;">getExtraInfo</span></strong>();
    
}
           

4.DefaultClusterRenderer類。預設是sdk是沒有添加額外資訊,是以要自己加:

mRenderer.setOnClusterItemClickListener(mOnClusterItemClickListener);
           
/**
     * Creates markerWithPosition(s) for a particular cluster, animating it if necessary.
     */
    private class CreateMarkerTask {
        private final Cluster<T> cluster;
        private final Set<MarkerWithPosition> newMarkers;
        private final LatLng animateFrom;

        /**
         * @param c            the cluster to render.
         * @param markersAdded a collection of markers to append any created markers.
         * @param animateFrom  the location to animate the markerWithPosition from, or null if no
         *                     animation is required.
         */
        public CreateMarkerTask(Cluster<T> c, Set<MarkerWithPosition> markersAdded, LatLng animateFrom) {
        	this.cluster = c;
        	this.newMarkers = markersAdded;
        	this.animateFrom = animateFrom;
        }

        private void perform(MarkerModifier markerModifier) {
            // Don't show small clusters. Render the markers inside, instead.
            if (!shouldRenderAsCluster(cluster)) {
                for (T item : cluster.getItems()) {
                    Marker marker = mMarkerCache.get(item);
                    MarkerWithPosition markerWithPosition;
                    if (marker == null) {
                        MarkerOptions markerOptions = new MarkerOptions();
                        if (animateFrom != null) {
                            markerOptions.position(animateFrom);
                            markerOptions.icon(item.getBitmapDescriptor());
                            <span style="color:#FF6666;"><strong>markerOptions.extraInfo(item.getExtraInfo());</strong></span>
                        } else {
                            markerOptions.position(item.getPosition());
                            markerOptions.icon(item.getBitmapDescriptor());
                            <span style="color:#FF6666;"><strong>markerOptions.extraInfo(item.getExtraInfo());</strong></span>
                        }
                        onBeforeClusterItemRendered(item, markerOptions);
                        marker = mClusterManager.getMarkerCollection().addMarker(markerOptions);
                        markerWithPosition = new MarkerWithPosition(marker);
                        mMarkerCache.put(item, marker);
                        if (animateFrom != null) {
                            markerModifier.animate(markerWithPosition, animateFrom, item.getPosition());
                        }
                    } else {
                        markerWithPosition = new MarkerWithPosition(marker);
                    }
                    onClusterItemRendered(item, marker);
                    newMarkers.add(markerWithPosition);
                }
                return;
            }

            MarkerOptions markerOptions = new MarkerOptions()
                    .position(animateFrom == null ? cluster.getPosition() : animateFrom);

            onBeforeClusterRendered(cluster, markerOptions);

            Marker marker = mClusterManager.getClusterMarkerCollection().addMarker(markerOptions);
            mMarkerToCluster.put(marker, cluster);
            mClusterToMarker.put(cluster, marker);
            MarkerWithPosition markerWithPosition = new MarkerWithPosition(marker);
            if (animateFrom != null) {
                markerModifier.animate(markerWithPosition, animateFrom, cluster.getPosition());
            }
            onClusterRendered(cluster, marker);
            newMarkers.add(markerWithPosition);
        }
    }
           

API:  MarkerOption    com.baidu.mapapi.map.MarkerOptions  

方法  

限定符和類型 方法和說明

MarkerOptions

anchor(float anchorX, float anchorY)

設定 marker 覆寫物的錨點比例,預設(0.5f, 1.0f)水準居中,垂直下對齊

MarkerOptions

draggable(boolean draggable)

設定 marker 是否允許拖拽,預設不可拖拽

MarkerOptions

extraInfo(Bundle extraInfo)

設定 marker 覆寫物的額外資訊

float

getAnchorX()

擷取 marker 覆寫物水準方向錨點比例

float

getAnchorY()

擷取 marker 覆寫物垂直方向錨點比例

Bundle

getExtraInfo()

擷取marker覆寫物的額外資訊

BitmapDescriptor

getIcon()

擷取 Marker 覆寫物的圖示

LatLng

getPosition()

擷取 marker 覆寫物的位置坐标

float

getRotate()

擷取 marker 覆寫物旋轉角度

java.lang.String

getTitle()

擷取 marker 覆寫物的标題

int

getZIndex()

擷取 marker 覆寫物的 zIndex

MarkerOptions

icon(BitmapDescriptor icon)

設定 Marker 覆寫物的圖示,相同圖案的 icon 的 marker 最好使用同一個 BitmapDescriptor 對象以節省記憶體空間。

boolean

isDraggable()

擷取 marker 覆寫物是否可以拖拽

boolean

isPerspective()

擷取 marker 覆寫物是否開啟近大遠小效果

boolean

isVisible()

擷取 marker 覆寫物的可見性

MarkerOptions

perspective(boolean perspective)

設定是否開啟 marker 覆寫物近大遠小效果,預設開啟

MarkerOptions

position(LatLng position)

設定 marker 覆寫物的位置坐标

MarkerOptions

rotate(float rotate)

設定 marker 覆寫物旋轉角度,逆時針

MarkerOptions

title(java.lang.String title)

設定 marker 覆寫物的标題

MarkerOptions

visible(boolean visible)

設定 marker 覆寫物的可見性

MarkerOptions

zIndex(int zIndex)

設定 marker 覆寫物的 zIndex

5.mark點選事件:

/**
	 * 地圖marker點選
	 */
	@Override
	public boolean onMarkerClick(Marker marker) {
		try {
	
				<span style="color:#FF6666;"><strong>Bundle bun=marker.getExtraInfo();</strong></span>
				if(bun!=null){
					if(bun.getString("no_infowindow")!=null&&bun.getString("no_infowindow").trim().equals("infowindow")){
						JSONObject js=new JSONObject(bun.getString("info"));
						LatLng latLng = marker.getPosition(); 
						mInfoWindow = new InfoWindow(show(js), latLng, -47);
						mBaiduMap.showInfoWindow(mInfoWindow);
						MapStatusUpdate mup=MapStatusUpdateFactory.newLatLng(latLng);
						mBaiduMap.animateMapStatus(mup);
					}
				}
			
			
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return true;
	}
           

5.設定聚合數:

/**
     * If cluster size is less than this size, display individual markers.(預設的聚合數)
     */
    private static final int <span style="color:#FF6666;"><strong>MIN_CLUSTER_SIZE</strong></span> = 3;
           

二:設定聚合數字的背景顔色,設定一個背景純顔色的圓形:

<?xml version="1.0" encoding="utf-8"?>
<!-- ~ Copyright (C) 2015 Baidu, Inc. All Rights Reserved. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <com.diandian.clusterutil.RotationLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        <span style="color:#FF6666;"><strong>android:background="@drawable/bg_maps_50"</strong></span>
        android:orientation="vertical" >

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:paddingBottom="5dp"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:paddingTop="5dp" />
    </com.diandian.clusterutil.RotationLayout>

</LinearLayout>