- Get Baidu Key
- Resource introduction
- Integrate Baidu map SDK in the project
- Display map
1.1. Register baidu account and log in
1.2. Create application
Write SHA1, select 1 from 2 for release version and development version, or select all. The acquisition method of development version SHA1 is provided here
(1) Open Terminal under Android Studio
(2) Enter command c: enter Disk c;
(3) Then enter the command cd Users\Administrator, where Administrator is your own user name. I use 64311 here. There is an. adnroid file under the Administrator file. Enter the command CD. android, and finally find. android (if. android is not found, it may have been set and changed during installation)
(4) Enter the command keytool -list -v -keystore debug.keystore, where debug.keystore is the default keystore of studio. Press enter, and then enter the secret key: android (system default) enter (the secret keystore password is invisible), as shown in the following figure:
After input, the web page will automatically check whether the format is correct
If an error is reported, check whether there are spaces before and after
Then find the project package name com.smmer.test
In this way, the application is successfully established
2.1 resource download
https://lbsyun.baidu.com/index.php?title=sdk/download
The download is complete, and the contents of the file are as follows
2.2 resource import
(1) First change Android to project view
(2) Open the unzipped development package folder, find the file and copy it to the app/libs directory of the project, as shown in the figure
(3) In the libs directory, select each jar file (there is only one BaiduLbs_Android.jar here), right-click and select Add As Library..., as shown in the figure:
(4) At this time, you will find that the corresponding description of the jar file that the project depends on is generated in the dependencies block of build.gradle in the app directory, as shown below:
3.1 configuring the AndroidManifest.xml file
Add the following code to configure the development key (AK):
<application> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="developer key" /> </application>
Here, the name is changed to the package name
value is changed to the key (AK) applied by Baidu map open platform
Add the following permission statement outside the application:
<!-- Access the network and request map related business data, including map data, route planning, POI Search, etc --> <uses-permission android:name="android.permission.INTERNET" /> <!-- Obtain the network state, and perform data request network conversion according to the network state switching --> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <!-- Read external storage. If developers use so Dynamic loading function and so If the file is placed in an external storage area, you need to apply for the permission, otherwise you don't need it --> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- Write external storage. If the developer uses an offline map and the data is written in the external storage area, you need to apply for this permission --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Thus, the AndroidManifest.xml file configuration is complete
3.2 adding a map container to a layout file
<com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" />
3.3 map initialization
Create a custom Application and complete SDK initialization in its onCreate method.
public class DemoApplication extends Application { @Override public void onCreate() { super.onCreate(); //Before using SDK components, initialize the context information and pass in ApplicationContext SDKInitializer.initialize(this); //Since 4.3.0, all interfaces of Baidu map SDK support Baidu coordinates and National Survey Bureau coordinates. Use this method to set the coordinate type you use //It includes BD09LL and GCJ02 coordinates. The default is BD09LL coordinates. SDKInitializer.setCoordType(CoordType.BD09LL); } }
Declare the Application in the AndroidManifest.xml file
3.4 create map Activity
public class MainActivity extends Activity { private MapView mMapView = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mMapView = (MapView) findViewById(R.id.bmapView); } @Override protected void onResume() { super.onResume(); //When the activity executes onResume, execute mMapView. onResume() to realize map life cycle management mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //When the activity executes onPause, execute mMapView. onPause() to realize map life cycle management mMapView.onPause(); } @Override protected void onDestroy() { super.onDestroy(); //When the activity executes onDestroy, execute mMapView.onDestroy() to realize map life cycle management mMapView.onDestroy(); } }
If an error is reported, you need to
import com.baidu.mapapi.map.MapView;
In this way, the function is realized.