“This is the 24th day of my participation in the August More Text Challenge.

In the last article, you can see the default Baidu map, that means our operation is correct, there is no problem in applying the key and introducing the map SDK, the next step is to use Baidu Map according to their own business needs.

Baidu Map can be set to display different map types, including normal map (default), satellite map and blank map. All three were demonstrated in the last tweet, so you can see which one you should use based on your needs.

In fact, Baidu map can do a lot of operations, such as display real-time positioning information of mobile phones, drawing points, drawing lines, adding marks and information boxes. So let’s look at how to achieve real-time location information display.

Welcome friends to search “Andy Hui” on wechat to pay attention to a wave!

Write some thoughts of the programmer, hope it will help you.

Baidu Map development – Display real-time location information 04 (QQ.com)

Displays real-time location information

Currently, the phones we use have their own GPS positioning function, which allows us to get longitude and latitude satellite coordinates in an open environment without interference or with operators or wifi networks.

Android positioning SDK, it supports global positioning, accurate access to latitude and longitude information.

At present, there are three coordinate system types obtained in China:

  • Coordinates of the NATIONAL Survey Office
  • Baidu Mercator coordinates
  • Baidu latitude and longitude coordinates

In overseas areas, only WGS84 coordinates can be obtained. During development, we need to pay attention to the fact that the coordinate system obtained in each place may be different. If the development business has overseas requirements, coordinate conversion needs to be done in advance, otherwise the normal business logic may be affected.

By default, the positioning SDK outputs coordinates GCJ02 and the map SDK outputs coordinates BD09II.

At present, the latest map SDK downloaded, need to select the positioning SDK, our tutorial at the beginning of the choice is full positioning, that is to say, now can directly use the operation code to obtain the latitude and longitude, can achieve real-time positioning function.

1. Configure the androidmanifest.xml file

You need to add the corresponding permission application in the androidmanifest.xml file in advance. Here, as explained before, you need to apply for the permission of network positioning and GPS positioning. If the target host has a higher Version of Android, you need to apply dynamically (see previous tweet).

<! - the permissions for network positioning - > < USES - permission android: name = "android. Permission. ACCESS_COARSE_LOCATION" / > <! - the permissions to access the GPS positioning - > < USES - permission android: name = "android. Permission. ACCESS_FINE_LOCATION" / >Copy the code

Then you need to declare the location service component in this file. This is very important, because the location SDK cannot get GPS information without applying for it.

<service android:name="com.baidu.location.f"
    android:enabled="true"
    android:process=":remote"/>
Copy the code

2. Turn on the map positioning layer to construct the map data

The location function of Baidu Map is in the location layer, which is disabled by default and needs to be manually enabled in the code. Once the positioning layer is turned on, the subsequent real-time positioning display, markers, lines, planes, etc. can be displayed on the map.

mBaiduMap.setMyLocationEnabled(true);
Copy the code

Baidu Map obtains location information by inheriting the abstract class BDAbstractListener and rewriting its onReceieveLocation method to obtain location data and pass it to MapView.

In fact, after rewriting it, if the built-in positioning chip or network positioning to obtain GPS information will be rewritten method to obtain. At this point we can get the latitude and longitude information that our business needs in the method.

3. Initiate location through the LocationClient

In the OnCreate() method, the mLocationClient variable starts receiving satellite information by instantiating and initializing it. Note that you need to set the parameter information to the mLocationClient, which is the specific information to instantiate the LocationClientOption.

LocationClientOption includes details such as whether GPS is enabled and how often it is received.

There’s a pit here

At present, according to the code given by the map SDK, only the start GPS is set for parameters, and the coordinate type is set and the interval time for positioning is set, so the coordinate information cannot be obtained. Take a closer look at the location SDK tutorial on how to declare and set the LocationClientOption class. You’ll see that there’s a lot you can set to suit your business needs.

The following code is the one THAT I have verified successfully to obtain the location information at that time. It is necessary to declare an initial open variable in advance, which is used to show the map at the location at the first location.

The above picture is all the logic code, it is recommended that you operate according to the above logic, will get real-time Baidu map coordinates. Baidu map official website tutorial can only be regarded as a reference, mainly to introduce some basic concepts clearly, but how the new person only according to his operation, is very results (I am here by pit miserably, has not been debugging).

Here you can also adjust the aperture size of its anchor point, positioning mode, whether to open the direction, set a custom positioning icon, precision ring fill color and precision ring border color 5 properties, these can refer to the official Baidu tutorial, the above is very detailed.

The picture below shows a successful run.

Some official tutorials can sometimes be taken with a grain of salt. If you want to consider, according to the official tutorials to encode, won’t appear the results you want, then most likely tutorial no update or not made clear to you, so you need at this time to the Internet to find solutions, to see if someone else encountered this problem, if there is a solution, himself started operation, operation.

That’s it for today, and I’ll see you next time.

It’s a little late, I went to bed, and I have to get up and go to work tomorrow. Gas worker

Small remarks

Life is short, I don’t want to go after what I can’t see, I just want to catch what I can see.

Original is not easy, give a attention.

I am Hui, thank you for reading, if it is helpful to you, please like, forwarding thank you.