How to use PHP and Baidu Map API to implement driving route planning function

WBOY
Release: 2023-07-30 09:54:01
Original
1434 people have browsed it

How to use PHP and Baidu Map API to implement driving route planning function

Overview:
Driving route planning is a commonly used function in map applications. This article will introduce how to use PHP and Baidu Map API to implement driving route planning function. We will obtain the driving route information through the interface provided by Baidu Map API, and use PHP to write code to implement it.

Step 1: Obtain Baidu Map Developer Key
First, we need to apply for a Baidu Map Developer Key at http://lbsyun.baidu.com/. After the application is successful, the obtained key will be used for subsequent interface calls.

Step 2: Write HTML page
Create a new HTML file for the user to enter the starting and ending addresses and display the driving route. The following is a simple sample code:

<!DOCTYPE html>
<html>
  <head>
    <title>驾驶路线规划</title>
    <script src="http://api.map.baidu.com/api?v=2.0&ak=your_key"></script>
  </head>
  <body>
    <div id="map" style="width: 100%; height: 600px;"></div>
    <div>
      <label for="start">起点:</label>
      <input type="text" id="start" name="start">
      <label for="end">终点:</label>
      <input type="text" id="end" name="end">
      <button onclick="calcRoute()">搜索路线</button>
    </div>
    <div id="result"></div>

    <script>
      var map = new BMap.Map("map");

      function calcRoute() {
        var start = document.getElementById("start").value;
        var end = document.getElementById("end").value;

        var driving = new BMap.DrivingRoute(map, { renderOptions: { map: map, panel: "result", autoViewport: true } });
        driving.search(start, end);
      }
    </script>
  </body>
</html>
Copy after login

In the above code, your_key should be replaced with the Baidu Maps developer key you applied for.

Step 3: Write PHP code
Now, we need to write PHP code to process the starting point and destination address entered by the user, and call the Baidu Map API interface to obtain driving route information. The following is a sample code:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $start = $_POST["start"];
  $end = $_POST["end"];

  $url = "http://api.map.baidu.com/directionlite/v1/driving?origin=".$start."&destination=".$end."&ak=your_key";
  $response = file_get_contents($url);
  $data = json_decode($response);

  if ($data->status == 0) {
    $routes = $data->result->routes;

    foreach ($routes as $route) {
      $distance = $route->distance;
      $duration = $route->duration;

      echo "驾驶距离:".$distance."米<br>";
      echo "驾驶时间:".$duration."秒";
    }
  } else {
    echo "获取驾驶路线失败";
  }
}
?>
Copy after login

In the above code, your_key should be replaced with the Baidu Maps developer key you applied for.

Step 4: Test the application
Now, you can open the HTML page in the browser, enter the start and end addresses, and click the Search Route button. The page will display the distance and time of the driving route.

Summary:
This article introduces how to use PHP and Baidu Map API to implement driving route planning function. By obtaining the starting point and ending address entered by the user and calling the Baidu Map API interface, we can obtain the driving route information and display it on the page. Such functions can be applied to various map applications to provide users with convenient navigation functions.

The above is the detailed content of How to use PHP and Baidu Map API to implement driving route planning function. For more information, please follow other related articles on the PHP Chinese website!

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