How to use Baidu map sdk in Android

Source: Internet
Author: User

Baidu map Android SDK is an application interface based on Android 2.1 (v1.3.5 and earlier versions support android 1.5 and later versions)

You can use this SDK to develop a map application for Android mobile devices. by calling the map SDK interface, you can easily access Baidu map service and data, build an LBS (MAP) application with rich functions and strong interactivity.

All services provided by Baidu map Android SDK are free of charge, and there is no limit on the number of times the interface is used. You must apply for a key before using the Baidu map Android SDK. Use any non-profit product directly. This outsourcers means that profitable products must be paid for by Baidu.

1. Baidu map api platform.

Baidu map API Web site: http://developer.baidu.com/map/sdk-android.htm

The Baidu map development procedure is

① Obtain the key
② Apply for a new key and view the previously applied key
③ Development Guide for using Baidu map on the Android platform
④ Download the class and method descriptions provided by the class Reference Library
⑤ Related packages used for development and Examples

Grunt, This is the UI of Baidu map's apk platform:

First, we apply for a key.

The steps are also very simple:

① Click to obtain the key ② enter the key application page and read the relevant Terms of Use ③ check and agree to the terms ④ enter the application name and enter the corresponding functions and descriptions of the application ⑤ enter the verification code to confirm 6 the key can be applied successfully.

Shows the key I applied:

How to import the applied jar package to the project.

I,Create a libs folder in the project (if it does not exist), copy the baidumapapi_v2_00001.jar in the development package to the libs root directory, and copy libapp_BaiduMapApplib_v2_1_1.so and libvi_voslib.so to the Lib.

II,In Java Build Path-> Order and Export in the project properties, check whether the selection box before Android Private Libaries and Android Dependencies is selected,

III,Click "OK"> "clean" to re-compile the Project. Then, you can use the map function for programming.

IV,The liblockSDK3.1.jar in the package and the corresponding so file are used for locating and searching. If you do not need it, you can not add it to the project.

Shows the architecture of the jar package:

The following figure shows how the jar package is imported:

Under the configuration file, add the following permissions.

<Supports-screens android: anyDensity = "true" android: largeScreens = "true" android: normalScreens = "true" android: resizeable = "true" android: smallScreens = "true"/>

Set attributes for the Activity to load the map

<Activity android: name = "com. dfrz. map. DFRZMapActivity "android: configChanges =" orientation | keyboardHidden | screenSize "android: label =" @ string/app_name "android: screenOrientation =" sensor "> </activity>

After these preparations are completed, we need to enter the corresponding map control

<Com. baidu. mapapi. map. mapView android: id = "@ + id/bmapsView" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_weight = "1" android: clickable = "true"/>

The source code of the above xml is the control box for loading Baidu map. You can adjust the layout as needed or place it to the desired location of your Activity. This control needs to receive click events, so you need to set clickable to true.

The principle of Baidu's map is like this ...........

The map, positioning, and search functions must be managed using BMapManager objects. BMapManager provides four interfaces: init (), start (), stop (), and destroy (). Generate and initialize the BMapManager object in the application, call destroy () when the program exits, and call the start () method in the onCreate () method of the Activity that requires the sdk function, call the stop () method in the onDestroy () method or onResume ()/onPause () to call start () and stop () respectively (). Common method: destroy (): Call boolean init (java. lang. string strKey, MKGeneralListener) initialize the map engine. Enter the applied Baidu map Key boolean start (): Enable Baidu map API boolean stop (): Terminate Baidu map API, after this function is called, no callback will occur.

MapView -- display of Baidu map.

A View control that displays maps. When selected by the focus, it can capture key events and touch gestures to pan and zoom the map setBuiltInZoomControls (boolean on ): set whether to enable the built-in zoom control getController (): returns the MapController of the map. This object can be used to control and drive translation and zoom setDoubleClickZooming (boolean bDoubleClickZooming ): set whether mapview supports double-click to enlarge the effect getOverlays (): Get the existing layer refresh () in the current map control: refresh the map control.

How to use Baidu map to implement positioning requires LocationClient

The core class of the location processing function, registerLocationListener (BDLocationListener), receives the location data. After receiving the location data, the onReceiveLocation (BDLocation) method in the specified listener can receive the relevant data and construct the processing structure: new LocationClient (getApplicationContext () setLocOption (LocationClientOption) sets the positioning parameter start () to enable the positioning function. This function is continuous and can define the interval refresh time stop () to end the positioning function, however, the previously set location parameters will not disappear. You can start requestLocation () to initiate an asynchronous location request. If the location does not change, no network request will be initiated and the previous location will be returned directly. (The interval must be greater than 1 second)

LocationClientOption is the class used to locate the operation.

Setting the Positioning Method of the positioning function disableCache (boolean) really indicates disabling the cache to locate whether setOpenGPS (boolean) uses GPS positioning, provided that the mobile phone has enabled the GPS Positioning setAddrType (String) if the String is "all", the location address is returned. If the String is set to "all", "setCoorType (String)" is not returned. The format of the returned coordinates is gcj02.-Baidu latitude and longitude coordinates of bd09 Baidu mocato bd0911 (default mobile phone) setScanSpan (int) scheduled time interval (MS) when this value is greater than 1000, it initiates a location at a time specified by this value when this value is smaller than 1000 or when this value is not set, call the requestLocation () method once to locate

This is the class we need to define mainactivity. Source code:

Protected void onDestroy () {mapView. destroy (); if (mapManager! = Null) {mapManager. destroy (); mapManager = null;} super. onDestroy ();}

With these preparations, the corresponding running results are as follows:

This is the general step of the Baidu map application under android.

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.