Home > Java > javaTutorial > body text

How to use Java code to draw insightful driving routes by dragging route planning on Baidu Maps?

WBOY
Release: 2023-07-31 20:34:52
Original
1051 people have browsed it

How to use Java code to draw insight driving routes by dragging route planning on Baidu Maps?

Introduction:
With the improvement of people's living standards, more and more people choose to travel by car. In order to provide better driving route navigation services, Baidu Maps provides rich interfaces and functions. This article will introduce how to use Java code to realize the function of drawing insightful driving routes by dragging route planning on Baidu Maps.

1. Preparation

  1. Register a Baidu developer account and create an application, and obtain the application key (ak).
  2. Download, install and configure the Java development environment.

2. Obtain the Java SDK of Baidu Map API
You can download the latest Java SDK from the official website of Baidu Map Open Platform. After the download is complete, import the SDK into your Java project.

3. Introduce the Java SDK of Baidu Map API
Introduce the Java SDK of Baidu Map API into the Java code. The specific code is as follows:

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.SupportMapFragment;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.overlayutil.DrivingRouteOverlay;
import com.baidu.mapapi.search.core.RouteLine;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.RoutePlanSearchOption;
Copy after login

4. Set the map page layout
Add a map control in the layout file as follows:


android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
    android:id="@+id/map"
    android:name="com.baidu.mapapi.map.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
Copy after login

5. Initialize the map
Initialize the map in Java code, as follows:

SDKInitializer.initialize(getApplicationContext());
BaiduMap mBaiduMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getBaiduMap();
Copy after login

6. Initialize route planning search
Initialize route planning search in Java code, as shown below:

RoutePlanSearch mRoutePlanSearch = RoutePlanSearch.newInstance();
mRoutePlanSearch.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() {
    @Override
    public void onGetDrivingRouteResult(DrivingRouteResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            // 路线规划失败
        } else if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) {
            // 路线规划起终点有歧义,需要解决
        } else if (result.error == SearchResult.ERRORNO.NO_ERROR) {
            // 路线规划成功
            DrivingRouteLine drivingRouteLine = result.getRouteLines().get(0);
            DrivingRouteOverlay overlay = new DrivingRouteOverlay(mBaiduMap);
            overlay.setData(drivingRouteLine);
            overlay.addToMap();
            overlay.zoomToSpan();

        }
    }

    @Override
    public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {}

    @Override
    public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {}
});
Copy after login

7. Drag the map for route planning
In Add the function of dragging the map for route planning in the Java code, as shown below:

mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus) {}

    @Override
    public void onMapStatusChange(MapStatus mapStatus) {}

    @Override
    public void onMapStatusChangeFinish(MapStatus mapStatus) {
        LatLng startPoint = new LatLng(mapStatus.target.latitude, mapStatus.target.longitude);
        LatLng endPoint = new LatLng(【目的地纬度】, 【目的地经度】);

        PlanNode stNode = PlanNode.withLocation(startPoint);
        PlanNode enNode = PlanNode.withLocation(endPoint);
        DrivingRoutePlanOption option = new DrivingRoutePlanOption().from(stNode).to(enNode);
        mRoutePlanSearch.drivingSearch(option);
    }
});
Copy after login

8. Test run
Compile and run the Java code, open the map page, click on the map and drag, it will be on the map Insight driving routes are plotted on.

Summary:
Through the above steps, we can use Java code to realize the function of drawing insight into driving routes by dragging line planning on Baidu Maps. With the help of Baidu Map API's Java SDK, we can easily implement map-related functions in our own Java projects. Through continuous learning and exploration, we can develop richer and more practical map applications.

The above is the detailed content of How to use Java code to draw insightful driving routes by dragging route planning on Baidu Maps?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template