Custom map overlays (including view)

Source: Internet
Author: User

Original address: http://my.oschina.net/freestyletime/blog/291638Official examples

This Baidu map Android SDK example of a base map overlay

http://developer.baidu.com/map/wiki/index.php?title=androidsdk/guide/basicmap#. E6. A0.87.e6.b3.a8.e8.a6.86.e7.9b.96.e7.89.a9

New LATLNG (39.963175, 116.400244// build marker icon bitmapdescriptor   Bitmap =// build markeroption to add Marker  new  markeroptions ()      to the map . Position (point)      // add marker to the map and display   mbaidumap.addoverlay (option);

The marker is the meaning of the mark, which is what we are going to say today, the difficulty is that it only receives a bitmap to show, if we need to add some complex view ( For example, LinearLayout nested a textview and ImageView) to replace this bitmap do? After I looked at a lot of information, I finally found a solution.

Coverings (Marker) custom

In fact, the solution is very simple, is to convert the view or ViewGroup to bitmap on the line. Because the Map SDK is provided by a third party, we have no right to ask others to change the interface to adapt to us, so we have to find ways to adapt to them.

Now I have a custom layout, as shown below

<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"android:orientation= "Horizontal"Android:background= "@drawable/popup"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content">        <ImageViewAndroid:layout_width= "35DP"Android:layout_height= "35DP"Android:scaletype= "Centercrop"android:padding= "5dip"android:src= "@drawable/head_1"/>        <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:textcolor= "@android: Color/black"android:textsize= "20SP"Android:text= "Test"              /></LinearLayout>

Then you want to turn it into a bitmap and put it into marker. Here is the core conversion function:

  PrivateBitmap Getviewbitmap (View addviewcontent) {addviewcontent.setdrawingcacheenabled (true); Addviewcontent.measure (View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec (0, View.MeasureSpec.UNSPECIFIED)); Addviewcontent.layout (0, 0, Addviewcontent.getmeasuredwidth (), Addviewcontent.getmeasuredheight ());        Addviewcontent.builddrawingcache (); Bitmap Cachebitmap=Addviewcontent.getdrawingcache (); Bitmap Bitmap=Bitmap.createbitmap (CACHEBITMAP); returnbitmap; }

Among them, the outermost layout I tried with relativelayout, but can not be converted to bitmap, deep-seated reasons are interested you can try.

As long as the outermost layer is linearlayout, the layout of the inside you can write freely, no matter how many layers of nested other layout, such as relativelayout, can be smoothly displayed on the map.

Custom map overlays (including view)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.