Android Baidu Map API using marker and Infowindow

Source: Internet
Author: User

In the Android development process, the use of Baidu map is more common, but how to use, what version of the Baidu API or need some attention.

In the project process, need to use the Baidu map of the marker and Infowindow functions.

Label Overlay (Baidu Map Official chart)
The layout file is very simple, mainly Mapview, as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/    Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " android:background= "@color/overall_bg" > <linearlayout android:layout_width= "match_parent" android:l ayout_height= "wrap_content" android:orientation= "horizontal" > <button android:id= "@ +id/gps_button_reset "android:layout_width=" 0DP "android:layout_height=" Wrap_content "and            roid:layout_weight= "1" android:text= "@string/gps_reset_button" android:layout_marginbottom= "8DP" android:layout_marginleft= "16DP" android:layout_margintop= "8DP" android:layout_marginright= " 16DP "/> <button android:id=" @+id/gps_button_history "android:layout_width=" 0DP "Android:layout_height= "Wrap_content" android:layout_weight= "1" android:text= "@string/gps_history_button" and            Roid:layout_marginbottom= "8DP" android:layout_marginleft= "16DP" android:layout_margintop= "8DP" android:layout_marginright= "16DP"/> </LinearLayout> <include layout= "@layout/separate_        Line "/> <textview android:layout_width=" match_parent "android:layout_height=" Wrap_content " android:text= "@string/gps_text" android:paddingbottom= "8DP" android:paddingtop= "8DP" android:textsize = "15SP" android:layout_gravity= "center" android:gravity= "center"/> <include layout= "@layout /separate_line "/> <com.baidu.mapapi.map.mapview android:id=" @+id/bmapview "android:layout_width=" f Ill_parent "android:layout_height=" Fill_parent "android:clickable=" true "/></linearlayout>


The callout overlay implementation code is as follows:
    /** * Set the location on the map according to the latitude and longitude of the watch, update the position of the top text description */private void UpdateLocation (arraylist<gpsbean> list) {/ * * 1. New User Default display address */double LG = 104.06;        The latitude and longitude of Chengdu city Double lt = 30.67;        String location = Getresources (). getString (r.string.fake_position);        Baidumap.clear ();        List<latlng> potions = new arraylist<> (); for (int i = List.size ()-1; I >=0; i--) {//GPS non-empty, then set latitude and longitude, position text description LG = double.parsedouble (LIST.G            ET (i). Getlongitude ());            lt = double.parsedouble (List.get (i). Getlatitude ());            Location = List.get (i). GetAddress ();            Address is too long to be processed under location = Location.replace ("Sichuan province", "" "). Replace (" Chengdu "," ""). Replace (".", ""); Final LATLNG ll = new LATLNG (LT, LG);            Note the latitude and longitude order/** * For each footstep according to order number */int image_id = 0;                switch (i) {case 9:image_id = r.mipmap.icon_mark1;break; CASE 8:image_id = R.mipmap.icon_mark2;break;                Case 7:image_id = R.mipmap.icon_mark3;break;                Case 6:image_id = R.mipmap.icon_mark4;break;                Case 5:image_id = R.mipmap.icon_mark5;break;                Case 4:image_id = R.mipmap.icon_mark6;break;                Case 3:image_id = R.mipmap.icon_mark7;break;                Case 2:image_id = R.mipmap.icon_mark8;break;                Case 1:image_id = R.mipmap.icon_mark9;break;            Case 0:image_id = R.mipmap.icon_mark10;break;            } bitmapdescriptor Descriptor = Bitmapdescriptorfactory.fromresource (image_id);            Overlayoptions options = new Markeroptions (). Position (LL). Icon (descriptor). ZIndex (i);            Marker Marker = (Marker) baidumap.addoverlay (options);            Add identification tag information for marker bundle bundle = new bundle ();            Bundle.putserializable ("Info", List.get (i));            Marker.setextrainfo (bundle); Mapstatusupdate update = Mapstatusupdatefactory.newlatlngzoom (ll,17);        Baidumap.setmapstatus (update); }

Add a listener for the tagged marker, and implement pop-up Infowindow when clicked. (Infowindow display a maximum of one message at a time)

        /** * Add listener for Tag * Click to pop up location info */Baidumap.setonmarkerclicklistener (new Baidumap.onmark Erclicklistener () {@Override public boolean onmarkerclick (Marker Marker) {//Get Mark                Data in ER gpsbean info = (Gpsbean) marker.getextrainfo (). Get ("info");                Generate a TextView user in the map showing Infowindow TextView location = new TextView (Getapplicationcontext ());                Location.setbackgroundresource (R.mipmap.gps_button);                Location.setpadding (15, 15, 8, 35);                Location.settextcolor (Color.dkgray);                Location.settext ("Positioning time:" + info.getuploadtime () + "\ n" + info.getaddress ());                Location.settextsize (12);                Convert the latitude and longitude information of the marker to the coordinates on the screen final latlng ll = Marker.getposition ();                Point P = baidumap.getprojection (). Toscreenlocation (ll);                P.Y-= 47; LATLNG Llinfo = Baidumap.getprojectIon (). Fromscreenlocation (P); Add a click event for the popup Infowindow Minfowindow = new Infowindow (location,llinfo, New Infowindow.oninfowindowclickliste NER () {@Override public void Oninfowindowclick () {Baidumap.                    Hideinfowindow ();                }                });                Infowindow Baidumap.showinfowindow (Minfowindow) showing the last article;            return true; }        });

Finally, the map is displayed to the latest data, and the Infowindow is displayed.
    /**     * Show last location information     * Create the view *    /private void Updatebaidumapinfowindown of the Infowindow display, Double LT, double lg) {        TextView TextView = new TextView (Getapplicationcontext ());        Textview.setbackgroundresource (R.mipmap.gps_button);        Textview.setpadding (8, +);        Textview.settextcolor (Color.dkgray);        Textview.settext (location);        Textview.settextsize (n);        Final LATLNG ll = new LATLNG (LT,LG);        Minfowindow = new Infowindow (TextView, LL, new Infowindow.oninfowindowclicklistener () {            @Override            public void Oninfowindowclick () {                baidumap.hideinfowindow ();            }        });        Display Infowindow        Baidumap.showinfowindow (Minfowindow);        /**         * Move the map field of view to the latest location *        /mapstatusupdate update = Mapstatusupdatefactory.newlatlngzoom (ll,17);        Baidumap.setmapstatus (update);    }

This can be done. But there is the most important point, is the Baidu API version of the problem, this method in the new version of 3.4.1 does not seem to be implemented in this way, can only use the 3.0.0 version.
implementation of the Baidu API version download: http://download.csdn.net/detail/xygy8860/894

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Baidu Map API using marker and Infowindow

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.