Android App--Baidu maps latest SDK3.0 application to achieve the most commonly used labeling overlays and pop-up window coverings

Source: Internet
Author: User
Tags event listener response code

I. Overview

The latest version of the Baidu map SDK3.0, modified a lot of methods, before a lot of methods have been simplified, just do map this piece, by the way, the use of the latest version of the SDK.

Download the official demo, found that the change is quite large, some of the previous methods have been changed, the map initialization has also been adjusted. More than a few classes, specific usage refer to the following example, detailed instructions can be referred to the official documentation.

Two

Labeling Overlays:


Pop-up window overlays:


Third, the realization process

Most of them are based on the official demo.

A, configuration file:

The first step: Create a new Libs folder in the project, copy the Baidumapapi_vx_x_x.jar in the development package to the Libs root directory, copy libbaidumapsdk_vx_x_x.so to Libs\ Armeabi Directory (the official website has already had these two files, if you want to integrate into your project, you need to add it), copy the completed project catalog as shown;

Note: liblocsdk3.so and Locsdk_3.1.jar are the resources used by Baidu to locate the SDK, and developers can add their own according to their needs.

The second step: select "Add External JARs" in the project properties->java Build path->libraries, choose Baidumapapi_vx_x_x.jar, and return when determined.

With the above two steps, you will be able to use the Baidu Map SDK to provide you with all the functions.

Note: Because of the ADT plugin upgrade, if you are using Eclipse ADT 22, you need to set the development environment accordingly, as follows:

1. Select project in Eclipse, right-click Properties->java Build path->order and Export to make Android Private libraries tick;

2. Project-clean-> clean all.

B, in the androidmanifest to add the development key, the required permissions and other information;

<application    <meta-data        android:name= "Com.baidu.lbsapi.API_KEY"        android:value= "developer KEY"/> </application>

C, add some appropriate permissions

<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"/&GT;&LT;ses-permission android:name= "Android.permission.READ_PHONE_STATE"/> 


D, in the layout file, add Baidu Map of the custom control

  <com.baidu.mapapi.map.mapview        android:id= "@id/mapview_baidu_address"        android:layout_width= "Match_ Parent "        android:layout_height=" match_parent "        />

E, initializing the context global variables referenced by the SDK when the application is created

public class Mainactivity extends Activity {    @Override    protected void onCreate (Bundle savedinstancestate) {        super.oncreate (savedinstancestate);         Initialize the context information before using each component of the SDK, passing in ApplicationContext        //Note that the method is implemented before Setcontentview the method        sdkinitializer.initialize (Getapplicationcontext ());        Setcontentview (R.layout.activity_main);    }}

Note: before each functional component of the SDK is used, you need to call

Sdkinitializer.initialize (Getapplicationcontext ()), so we recommend that the method be placed in the application initialization method

F. Create map activity

Unlike the previous version, the 3.0 version of the SDK, the Mapview object needs to get the Baidumap object before the Baidumap object can subsequently add the overlay to the operation.

protected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsetcontentview (r.layout.d1_ activity_address); super.oncreate (savedinstancestate); Mmapview = (Mapview) Findviewbyid (R.id.mapview_baidu_address ); mbaidumap = Mmapview.getmap ();//Get Baidumap class Baidumap class to add a custom layer mapstatusupdate MSU = Mapstatusupdatefactory.zoomto (16.0f);//Set the scale mbaidumap.setmapstatus (MSU) of the map, or the previous parameter to Baidumap class Initoverlay ( latitude, longitude);//Draw a cartographic layer showpop ();//Popup Popup}

Manage the map life cycle

The life cycle of the @Overrideprotected void OnPause () {//Mapview is synchronized with the activity and calls Mapview.onpause () Mmapview.onpause () when the activity is suspended; Super.onpause ();} The life cycle of the @Overrideprotected void Onresume () {//Mapview is synchronized with the activity and calls Mapview.onresume () Mmapview.onresume when activity resumes ( ); Super.onresume ();} The life cycle of the @Override protected void OnDestroy () {///Mapview is synchronized with the activity and calls Mapview.destroy () Mlocclient.stop () when the activity is destroyed; Turn off positioning layer mbaidumap.setmylocationenabled (false); if (mmapview!=null) {Mmapview.ondestroy (); mmapview = null;}//Reclaim bitmap resource bitmap.recycle (); }

where the overlay is drawn, i.e. the layer's method code is as follows:

LATLNG the description of this official document is: geographic coordinates basic data structure

public void Initoverlay (double latitude, double longitude) {//define maker coordinate point latlng ll = new Latlng (latitude, longitude);//Build Marker icon Bitmapdescriptor bd1 = Bitmapdescriptorfactory.fromresource (R.drawable.pin_green);//Build Markeroption, Used to add markeroverlayoptions option = new Markeroptions (). Position (LL). Icon (BD1) on the map,//Add marker on the map, and display mbaidumap.addoverlay (option);//Add current branch information layer mapstatusupdate U = mapstatusupdatefactory.newlatlng (ll); Mbaidumap.setmapstatus (u);}

Pop-up Overlay click Overlay, pop-up navigation selection list

public void Showpop () {//create Infowindow show Viewview popup = View.inflate (this, r.layout.pop, null); TextView title = (TextView) Popup.findviewbyid (r.id.tv_title); TextView content = (TextView) Popup.findviewbyid (r.id.tv_content); Title.settext (name); Content.settext (addr);// Defines the coordinate point used to display the Infowindow latlng pt = new Latlng (latitude, longitude);//point p = mbaidumap.getprojection (). Toscreenlocation (PT);//create Infowindow Click event Listener Oninfowindowclicklistener Listener = new Oninfowindowclicklistener () { public void Oninfowindowclick () {//Add post-click event Response code URI URI = Uri.parse ("Geo:" + Latitude + "," + Longitude + "); Intent it = New Intent (Intent.action_view, URI); startactivity (it);}};/ /create Infowindowinfowindow Minfowindow = new Infowindow (Popup, PT, listener);//Display Infowindowmbaidumap.showinfowindow ( Minfowindow);}

Iv. Summary

Using the Baidu Map SDK development of several steps:

1. Importing jar packages and so files

2, the layout file added Baidu map control, manifest manifest file, add the response permissions, application node added key

3 . Join Sdkinitializer.initialize (Getapplicationcontext ()) before Setcontentview in the main activity; initialize

4, get the Map Mapview object, and then do other operations

5, generate the cover (see the code above)

6. Generate pop-up cover ( see code above )





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.