Baidu Map Android SDK-Annotations (Marker) basic use

Source: Internet
Author: User

Annotations (Marker) are the most commonly used map overlays, and today we will introduce some of the most basic ways to use annotations (Marker)!

Achieve the goal:

1, build the basic map page;

2, add Marker at the center of the map;

3, to achieve Marker click event Response;

4, the implementation of Marker add and flexible deletion;

In order to achieve the above objectives, we will be divided into the following several steps.

The first step, create the project, import Baidu map Android SDK;

The specific way to create the project (slightly). This example is intended primarily for maps and related overlays, so I'm only using the "Base map" function module provided by the SDK development resource download platform.

The corresponding: Http://lbsyun.baidu.com/sdk/download

Check:

After the selection is complete, click on the development package download to correctly import the downloaded development package into the development project.


The second step, the application developer secret key;

Once the project is created, go to the API console and request the appropriate development key to prepare for the specific development work.

Specific application methods and precautions, please refer to: http://blog.csdn.net/callmesen/article/details/39523767 here not to repeat.


The third step, modify the Androidmanifest file, add the development secret key and the required permissions;

In the Androidmanifest file, add the development secret key and the required permissions.

Add the development key in the following manner:

    <meta-data          android:name= "Com.baidu.lbsapi.API_KEY"        android:value= "Development secret key! "/>
Notice the inside of the application.

Add the required permissions as follows:

<uses-permission android:name= "Android.permission.GET_ACCOUNTS"/> <uses-permission android:name= " Android.permission.USE_CREDENTIALS "/> <uses-permission android:name=" Android.permission.MANAGE_ACCOUNTS "/ > <uses-permission android:name= "Android.permission.AUTHENTICATE_ACCOUNTS"/> <uses-permission android: Name= "Android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name= " Android.permission.INTERNET "/> <uses-permission android:name=" com.android.launcher.permission.READ_ SETTINGS "/> <uses-permission android:name=" Android.permission.CHANGE_WIFI_STATE "/> <uses-permission Android:name= "Android.permission.ACCESS_WIFI_STATE"/> <uses-permission android:name= " Android.permission.READ_PHONE_STATE "/> <uses-permission android:name=" android.permission.WRITE_EXTERNAL_ STORAGE "/> <uses-permission android:name=" Android.permission.BROADCAST_STICKY "/> <uses-permission Android:name= "Android.permissioN.write_settings "/> <uses-permission android:name=" Android.permission.READ_PHONE_STATE "/> 

Fourth step, create the base map;

Add the Mapview control to the XML file as follows:

    <com.baidu.mapapi.map.mapview      android:id= "@+id/bmapview"      android:layout_width= "Fill_parent      " android:layout_height= "Fill_parent"      android:clickable= "true"/>
Initialize the map SDK in Java code to enable the presentation of the underlying map:
Initialize Map sdksdkinitializer.initialize (Getapplicationcontext ()); Setcontentview (R.layout.activity_main);//bind XML Mapview Control Mapview = (Mapview) Findviewbyid (R.id.bmapview);


Fifth step, get the center point of the map, and add Marker object;

The core code for implementation is as follows:

Baidumap = Mapview.getmap (); LATLNG latlng = Baidumap.getmapstatus (). target;//Prepare marker pictures Bitmapdescriptor bitmap = Bitmapdescriptorfactory.fromresource (R.drawable.ic_launcher);//Prepare marker option to add marker using Markeroptions = new Markeroptions (). icon (bitmap). Position (latlng);//Get added marker This facilitates subsequent operations marker = (marker) Baidumap.addoverlay ( Markeroptions);

The sixth step, realizes the Marker click event Response;

Using the Map monitoring method of Baidumap object to achieve the click Response for marker, the core code is as follows:

Add to marker click the corresponding event Baidumap.setonmarkerclicklistener (new Onmarkerclicklistener () {@Overridepublic Boolean Onmarkerclick (Marker arg0) {//TODO auto-generated method Stubtoast.maketext (Getapplicationcontext (), "Marker was clicked! ", Toast.length_short). Show (); return false;});

The seventh step, using the Button control, control the addition and deletion of Marker;

Here, a button is used to control the addition or deletion of a marker on the map, first adding a button control to the XML:

    <button        android:id= "@+id/button1"        android:layout_width= "wrap_content"        android:layout_height= " Wrap_content "        android:layout_alignparentright=" true "        android:layout_alignparenttop=" true "        Android: text= "Delete marker"/>
Add this button's Click event Response in the Java code, with the following core code:

Button control Add Delete buttonbutton.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View arg0) {// TODO auto-generated Method Stubif (button.gettext () = = "Add marker") {marker = (marker) baidumap.addoverlay (markeroptions) ; Button.settext ("delete marker");} else {marker.remove (); Button.settext ("Add marker");}});

Through the above operation, you can achieve the corresponding marker add, marker Click Response, marker Delete and other operations.




Baidu Map Android SDK-Annotations (Marker) basic use

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.