“This is the 29th day of my participation in the August Gwen Challenge.

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.

Public number link:

Previous tweet “Bus route” error corrected

In particular, after my query and verification over the weekend, I found that there was something wrong with the implementation of bus information query in the last tweet. When I was writing, I just intercepted the picture from the official website. I had planned to verify it the next day, but when I verified it this morning, I found that the bus route could not appear as promised.

Let me list the specific problems here

It is not possible to import overlayManager. Java and BusLineOverlay. After testing, we found that we need to introduce four folder data in the official DEMO, namely assets, com, layout and Value.

Assets: Copy the entire folder to the main directory.

Com: Copy it to the main/ Java directory, there may be errors, mainly in the IconGenerator class and DefaultClusterRenderer class R error, you need to change its R path to the current package R.

Layout: Introduce text_bubble from its DEMO project Layout into your project.

Value: Copies the contents of style.xml into style.xml in the project.

This brings in all the files and resources needed to draw tracks on the map.

One caveat is that if you are searching for bus routes in the onCreate() method, you will get an error in the callback, with PERMISSION_UNFINISHED. Officials did not say what caused the error, suggesting that the license was not completed.

The retrieval method must be placed in a click event or other method.

// Search mpoisearch.searchinCity (new PoiCitySearchOption().city(" tianjin ").keyword("707").scope(2));Copy the code

The final effect is as follows:

Tianjin bus route map.

All right, let’s get down to business. The last tweet mainly introduced the retrieval of data on the map, POI operation, can find a store, query the direction of the bus route. The following tweet mainly introduces how we use Baidu Map to realize navigation functions, such as going to Tianjin West Railway Station, how to get there from Tianjin South Railway Station, what the route is and how to display it in the interface.

There are many forms of route planning in the official tutorial, such as walking, cycling, subway + bus, driving, etc. The logic codes are almost the same. Here, I will simply implement the route planning of walking according to the tutorial to see how to quickly realize the route of location and destination.

Route navigation on the map

Now we according to the tutorial tips, simple implementation of the route planning on baidu map.

Walking route planning can use Walkin Group Overlay to draw a walking route, including starting and ending points and turning points, based on the starting and ending points of the walking route. Support its custom icon. (Note that the planned walking route should not exceed 100km).

/ / route planning mSearch = RoutePlanSearch. NewInstance (); mSearch.setOnGetRoutePlanResultListener(routePlanResultListener);Copy the code
. / / pedestrian route planning PlanNode stNode = PlanNode withCityNameAndPlaceName (" tianjin ", "south" estuary metro subway); PlanNode enNode = PlanNode. WithCityNameAndPlaceName (" tianjin ", "royal" estuary metro subway); mSearch.walkingSearch((new WalkingRoutePlanOption()).from(stNode).to(enNode));Copy the code
/ * * * route planning listener * / OnGetRoutePlanResultListener routePlanResultListener = new OnGetRoutePlanResultListener () {@ Override Public void onGetWalkingRouteResult(WalkingRouteResult) {// Create a WalkingRouteOverlay instance WalkingRouteOverlay overlay = new WalkingRouteOverlay(mBaiduMap); if (walkingRouteResult.getRouteLines()! = null) {if (walkingRouteResult getRouteLines (). The size () > 0) {/ / access path planning data (return to the first data), Set overlay path data overlay. SetData (walkingRouteResult getRouteLines () get (0)); overlay.addToMap(); // Draw on the map}}}}Copy the code

This shows the route plan on the map. Here we can according to the official website to the tutorial to achieve cycling route planning, driving route planning and other functions, are very practical. But there are many pits, so pay special attention to them.

Coordinate transformation

In our actual projects, many times we get more accurate coordinates for the GPS global positioning system. But our Baidu map display coordinates are BD09LL coordinates, that is to say, we actually get coordinates to use in Baidu map, we must first coordinate conversion can be.

So how do coordinates get classified.

There are three main types:

1. WGS84: A geodetic coordinate system, which is also used in the widely used GPS global positioning system.

2. GCJ02: The coordinate system of geographic information system formulated by the State Bureau of Surveying and Mapping of China is the coordinate system encrypted by WGS84 coordinate system. (For the sake of security, all domestic maps, mapping companies to get coordinates are after a certain encryption of the coordinates, that is to say, there may be a little deviation in the actual position).

3. BD09: Baidu coordinate system, encrypted again on the basis of GCJ02 coordinate system.

At present, Baidu Map cannot be displayed directly with WGS84 coordinate system, and must be converted to BD09 coordinate system, otherwise, even if loaded, the position will have deviation.

There are two conversion modes:

1. General coordinate conversion method (coordinate conversion)

Other coordinate conversion (Gaode Map, Tencent Map, etc.) for Baidu latitude and longitude coordinates BD09LL

// Initialize the left conversion utility class, SourceLatLng CoordinateConverter Converter = new CoordinateConverter().from(COMMON) .coord(sourceLatLng); // Convert LatLng desLatLng = converter.convert();Copy the code

The original GPS coordinates collected by GPS devices are converted into baidu coordinates

/ / sourceLatLng to convert coordinates CoordinateConverter converter = new CoordinateConverter () from (GPS CoordinateConverter. CoordType.) .coord(sourceLatLng); //desLatLng = converter.convert();Copy the code

2. Automatic coordinate conversion (GCJ02 coordinate input and output)

Globally declared as GCJ02 coordinate type, the coordinate conversion from GCJ02 to BD09LL is automatically performed throughout the application.

// Include BD09LL and GCJ02 coordinates, default is BD09LL coordinates. SDKInitializer.setCoordType(CoordType.GCJ02); / / the coordinates used for current type SDKInitializer getCoordType (); //BD09LL or GCJ02 coordinatesCopy the code

Note: The default value of Baidu map is BD09LL in China and WGS84 in foreign countries.


Well, happy times are always short.

Baidu Map development series has also been updated for 9 articles, which have basically gone through all the pits that baidu Map development should step on in Android project. Later, I will synchronize my new Testandroid project to Gitee and Github, or you can send “Baidu Map DEMO” in the background of the official account, and you can get the download link of the web disk. I hope it helps.

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.