百度地圖之——自定義marker樣式


title: 百度地圖之——自定義marker樣式

優(yōu)秀資源集錦

如果我比別人看得遠些,那是因為我站在巨人們的肩上

在百度地圖提供的API中,我們可以通過圖片來設置marker的樣式,但是當實現的效果復雜時就不能單單靠圖片解決务漩,我們已經見到了太多太多的marker樣式咱台。就讓我們來實現自己想要的樣式巡蘸。

首先讓我們來看看效果圖奋隶,由這你可以任意發(fā)揮想象,做成你想要的樣式悦荒。

基礎的百度地圖配置請參照 百度地圖開發(fā)指南進行配置

一唯欣、在布局文件添加百度地圖的MapView

    <com.baidu.mapapi.map.MapView
        android:id="@+id/bmapView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true" />

然后新建marker.xml布局文件,實現一個marker的樣式搬味。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/cover_nameId"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:gravity="center"
        android:text="名稱:我擦我擦我擦路"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/cover_temptureId"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="溫度:20 C"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/cover_concentrationId"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="濃度:20 C"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/darker_gray"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/cover_licquid_levelId"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="液位:200"
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/cover_electricityId"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:text="電量:20%"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    </LinearLayout>
    <ImageView
        android:layout_marginTop="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@mipmap/marker"/>

</LinearLayout>

二境氢、使用到的變量聲明

MapView mMapView = null;MapStatus 
mMapStatus;MapStatusUpdate mMapStatusUpdate;
BaiduMap mBaiduMap;Marker coverMarker;
List<Map<String, String>> coverArray = newArrayList<Map<String, String>>();

三、地圖初始化配置

        //在使用SDK各組件之前初始化context信息碰纬,傳入ApplicationContext
        //注意該方法要再setContentView方法之前實現
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_covermap);
        //獲取地圖控件引用
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
        Intent intent = getIntent();
        coverArray = (List<Map<String, String>>) intent.getSerializableExtra("coverArray");
        System.out.println("coverArray:" + coverArray.size());
        if (coverArray.size() != 0) {
            //經緯度   Latitude and longitude
            String latitude = coverArray.get(0).get("latitude");
            String longitude = coverArray.get(0).get("longitude");
            System.out.println("latitude:" + latitude + "   longitude:" + longitude);
            LatLng cenpt = new LatLng(Double.valueOf(latitude), Double.valueOf(longitude));
            mMapStatus = new MapStatus.Builder().target(cenpt).zoom(16).build();
            mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
            mBaiduMap.setMapStatus(mMapStatusUpdate);
            addMarkers();   //添加標注萍聊。
        }

四、addMarker()函數

private void addMarkers() {
    double[][] coordinates = new double[coverArray.size()][2];
    for (int i = 0; i < coverArray.size(); i++) {
        coordinates[i][0] = Double.parseDouble(coverArray.get(i).get("longitude"));
        coordinates[i][1] = Double.parseDouble(coverArray.get(i).get("latitude"));
    }
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.marker, null);
    TextView coverNameTv = (TextView) view.findViewById(R.id.cover_nameId);
    TextView covertemptureTv = (TextView) view.findViewById(R.id.cover_temptureId);
    TextView coverconcentrationTv = (TextView) view.findViewById(R.id.cover_concentrationId);
    TextView coverlicquidlevelTv = (TextView) view.findViewById(R.id.cover_licquid_levelId);
    TextView coverelectricityTv = (TextView) view.findViewById(R.id.cover_electricityId);
    for (int j = 0; j < coverArray.size(); j++) {
        LatLng lla = new LatLng(coordinates[j][1], coordinates[j][0]);
        coverNameTv.setText(coverArray.get(j).get("covername"));
        covertemptureTv.setText("溫度:" + coverArray.get(j).get("temperature") + "℃");
        coverconcentrationTv.setText("濃度:" + coverArray.get(j).get("concentration"));
        coverlicquidlevelTv.setText("液位:" + coverArray.get(j).get("liquidlevel"));
        coverelectricityTv.setText("電量:" + coverArray.get(j).get("electricity") + "%");
        if (coverArray.get(j).get("lostflag").equals("1")) {
            coverNameTv.setBackgroundColor(Color.RED);
            covertemptureTv.setBackgroundColor(Color.RED);
            coverconcentrationTv.setBackgroundColor(Color.RED);
            coverlicquidlevelTv.setBackgroundColor(Color.RED);
            coverelectricityTv.setBackgroundColor(Color.RED);
        }
        else{
            coverNameTv.setBackgroundColor(Color.GRAY);
            covertemptureTv.setBackgroundColor(Color.WHITE);
            coverconcentrationTv.setBackgroundColor(Color.WHITE);
            coverlicquidlevelTv.setBackgroundColor(Color.GRAY);
            coverelectricityTv.setBackgroundColor(Color.GRAY);
        }
        BitmapDescriptor bd1 = BitmapDescriptorFactory.fromBitmap(getBitmapFromView(view));
        MarkerOptions ooA = new MarkerOptions().position(lla).icon(bd1).zIndex(9).draggable(true).title(coverArray.get(j).get("coverid"));

        coverMarker = (Marker) mBaiduMap.addOverlay(ooA);
    }
}

其中最重要的是

 BitmapDescriptorFactory.fromBitmap(getBitmapFromView(view))

而在百度地圖的API中悦析,

BitmapDescriptorFactory.fromBitmap(Bitmap bitmap)

傳入的是一個Bitmap的對象脐区,這樣我們就知道要怎么做了。我們就應該把我們的剛才新建的marker.xml 頁面轉換成一個Bitmap對象她按。

新建getBitmapFromView()函數,

將一個view的對象轉換成一個bitmap形的對象炕柔。


    private Bitmap getBitmapFromView(View view) {
        view.destroyDrawingCache();
        view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.UNSPECIFIED);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.setDrawingCacheEnabled(true);
        Bitmap bitmap = view.getDrawingCache();
        return bitmap;
    }

這樣我們就實現了自定義的marker.其中關鍵的代碼就是
getBitmapFromView() 這個方法酌泰,其他的都是百度API可以解決的。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末匕累,一起剝皮案震驚了整個濱河市陵刹,隨后出現的幾起案子,更是在濱河造成了極大的恐慌欢嘿,老刑警劉巖衰琐,帶你破解...
    沈念sama閱讀 217,084評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件也糊,死亡現場離奇詭異,居然都是意外死亡羡宙,警方通過查閱死者的電腦和手機狸剃,發(fā)現死者居然都...
    沈念sama閱讀 92,623評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來狗热,“玉大人钞馁,你說我怎么就攤上這事∧涔危” “怎么了僧凰?”我有些...
    開封第一講書人閱讀 163,450評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長熟丸。 經常有香客問我训措,道長,這世上最難降的妖魔是什么光羞? 我笑而不...
    開封第一講書人閱讀 58,322評論 1 293
  • 正文 為了忘掉前任绩鸣,我火速辦了婚禮,結果婚禮上狞山,老公的妹妹穿的比我還像新娘全闷。我一直安慰自己,他們只是感情好萍启,可當我...
    茶點故事閱讀 67,370評論 6 390
  • 文/花漫 我一把揭開白布总珠。 她就那樣靜靜地躺著,像睡著了一般勘纯。 火紅的嫁衣襯著肌膚如雪局服。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,274評論 1 300
  • 那天驳遵,我揣著相機與錄音淫奔,去河邊找鬼。 笑死堤结,一個胖子當著我的面吹牛唆迁,可吹牛的內容都是我干的。 我是一名探鬼主播竞穷,決...
    沈念sama閱讀 40,126評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼唐责,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了瘾带?” 一聲冷哼從身側響起鼠哥,我...
    開封第一講書人閱讀 38,980評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后朴恳,有當地人在樹林里發(fā)現了一具尸體抄罕,經...
    沈念sama閱讀 45,414評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,599評論 3 334
  • 正文 我和宋清朗相戀三年于颖,在試婚紗的時候發(fā)現自己被綠了呆贿。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,773評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡恍飘,死狀恐怖榨崩,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情章母,我是刑警寧澤母蛛,帶...
    沈念sama閱讀 35,470評論 5 344
  • 正文 年R本政府宣布,位于F島的核電站乳怎,受9級特大地震影響彩郊,放射性物質發(fā)生泄漏。R本人自食惡果不足惜蚪缀,卻給世界環(huán)境...
    茶點故事閱讀 41,080評論 3 327
  • 文/蒙蒙 一秫逝、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧询枚,春花似錦违帆、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,713評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至渊抄,卻和暖如春尝胆,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背护桦。 一陣腳步聲響...
    開封第一講書人閱讀 32,852評論 1 269
  • 我被黑心中介騙來泰國打工含衔, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人二庵。 一個月前我還...
    沈念sama閱讀 47,865評論 2 370
  • 正文 我出身青樓贪染,卻偏偏與公主長得像,于是被迫代替她去往敵國和親催享。 傳聞我的和親對象是個殘疾皇子抑进,可洞房花燭夜當晚...
    茶點故事閱讀 44,689評論 2 354

推薦閱讀更多精彩內容