1, Functional requirements
Develop Baidu map positioning according to official documents APP,Displays the coordinates of the current position.
2, Implementation process
1. Experimental environment
Android Studio3.1
2. Get Baidu developer KEY
https://lbsyun.baidu.com/index.php?title=androidsdk
(1) Create user
This part is the same as every app registration process and will not be repeated.
(2) Get key
Create a new application and select the application type you need
(3) Get SHA1 and PackageName
Open the terminal of Android studio and operate on the command line
First enter the C disk, and then enter the. android command, and then keytool -list -v -keystore debug.keystore
You can view the package name in build.gradle
(4) After creating the Key, download it
Download the file and unzip it on disk D
Copy the files under libs to libs under the project
In the libs directory, select baidulbs_ Right click the android.jar file and select Add As Library
Check the corresponding description of the jar file that the project depends on in the dependencies block of build.gradle
(5) Configure the AndroidManifest.xml file
<application> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="developer key" /> </application>
Add a permission statement outside the application:
3. Page design
In the layout file activity_ Add a map container to main.xml
<com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="true" />
But I want to realize the following map with longitude, latitude and address. After adding the code, it is:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <com.baidu.mapapi.map.MapView android:id="@+id/bmapView" android:layout_width="0dp" android:layout_height="0dp" android:clickable="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#e0000000" android:orientation="vertical" tools:ignore="MissingConstraints"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:layout_marginTop="20dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Latitude:" android:textColor="#ffffff" android:textSize="15dp" /> <TextView android:id="@+id/tv_Lat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textColor="#ffffff" android:textSize="15dp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:layout_marginTop="10dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Longitude:" android:textColor="#ffffff" android:textSize="15dp" /> <TextView android:id="@+id/tv_Lon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textColor="#ffffff" android:textSize="15dp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:layout_marginLeft="12dp" android:layout_marginTop="10dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Address:" android:textColor="#ffffff" android:textSize="15dp" /> <TextView android:id="@+id/tv_Add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textColor="#ffffff" android:textSize="15dp" /> </LinearLayout> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
4. Java code implementation
(1) Preparation of MainActivity
public class MainActivity extends AppCompatActivity { MapView mMapView = null; BaiduMap mBaiduMap ; LocationClient mLocationClient; TextView tv_Lat; //latitude TextView tv_Lon; //longitude TextView tv_Add; //address @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_Add = findViewById(R.id.tv_Add); tv_Lon = findViewById(R.id.tv_Lon); tv_Lat = findViewById(R.id.tv_Lat); mMapView = findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); //Set map type mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); //Turn on the positioning layer of the map mBaiduMap.setMyLocationEnabled(true); //Get location permission from the system if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[], 1); }else { //Set up client mLocationClient = new LocationClient(this); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); option.setCoorType("bd09ll"); // Set coordinate type option.setIsNeedAddress(true); //Set whether address information is required. It is not required by default mLocationClient.setLocOption(option); //Set listener MylocationListener myLocationListener = new MylocationListener(); mLocationClient.registerLocationListener(myLocationListener); //Turn on map positioning layer mLocationClient.start(); MyLocationConfiguration.LocationMode mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING; MyLocationConfiguration mLocationConfiguration = new MyLocationConfiguration(mCurrentMode,true,null,0xAAFFFF88,0xAA00FF00); mBaiduMap.setMyLocationConfiguration(mLocationConfiguration); } } private class MylocationListener extends BDAbstractLocationListener { @Override public void onReceiveLocation(BDLocation bdLocation) { if (bdLocation == null || mMapView == null){ return; } MyLocationData locationData = new MyLocationData.Builder() .accuracy(bdLocation.getRadius()) .direction(bdLocation.getDirection()) .latitude(bdLocation.getLatitude()) .longitude(bdLocation.getLatitude()) .build(); mBaiduMap.setMyLocationData(locationData); //Output latitude and longitude and location tv_Add.setText(bdLocation.getAddrStr()); tv_Lat.setText(bdLocation.getLatitude()+" "); tv_Lon.setText(bdLocation.getLongitude()+" "); LatLng ll = new LatLng(bdLocation.getLatitude(),bdLocation.getLongitude()); MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(ll); mBaiduMap.animateMapStatus(update); } } }
Add lifecycle for Map
@Override protected void onResume() { mMapView.onResume(); super.onResume(); } @Override protected void onPause() { mMapView.onPause(); super.onPause(); } @Override protected void onDestroy() { super.onDestroy(); mLocationClient.stop(); mBaiduMap.setMyLocationEnabled(false); mMapView.onDestroy(); } }
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); } }
After running, I found that the APP can't be opened at all, and DemoApplication is gray, so I suspect it's the problem here. You have not supplied the global APP context info from sdkinitializer.initialize (context) function. You understand that you did not declare the Application in the AndroidManifest.xml file
Solution:
3, Display of operation interface
It is found that only Beijing Tiananmen Square can be displayed. You need to add positioning permission here
<!-- This permission is used for network location --> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <!-- This permission is used to access GPS location --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Claim located services
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"/>
Run again to display the location of the virtual machine
Because this is the location of the virtual machine, in order to get its own location, we also need to download the apk file to the mobile phone and open it.
Download the file to your phone and open it
View location again