Home > Backend Development > PHP Tutorial > How to use PHP and Baidu Map API to query and display bus routes

How to use PHP and Baidu Map API to query and display bus routes

PHPz
Release: 2023-07-29 22:04:02
Original
1923 people have browsed it

Method of using PHP and Baidu Map API to query and display bus routes

Foreword:
In today's urban life, bus routes have become one of the important means of transportation for people's daily travel. In order to facilitate users to query specific bus route information, we can use PHP and Baidu Map API to implement the bus route query and display functions. This article will introduce in detail the method of using PHP and Baidu Map API to query and display bus routes, and provide corresponding code examples.

Step 1: Apply for Baidu developer account and key
First, we need to apply for a Baidu developer account and create an application to obtain the key to access Baidu Map API. You can create an application and obtain the key by visiting the Baidu Map Open Platform website (http://lbsyun.baidu.com/).

Step 2: Write HTML page
In the root directory of the project, create an index.html file to display the bus route query page. The code example is as follows:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>公交路线查询与展示</title>
    <style>
        #map {
            width: 100%;
            height: 600px;
        }
    </style>
</head>
<body>
    <div id="map"></div>

    <script src="http://api.map.baidu.com/api?v=2.0&ak=你的密钥"></script>
    <script src="main.js"></script>
</body>
</html>
Copy after login

Step 3: Write PHP file
In the root directory of the project, create a main.php file to process the logic of bus route query. In this file, we will call the bus route query interface provided by Baidu Map API to obtain bus route information. The code example is as follows:

<?php
$origin = $_GET['origin']; // 起点
$destination = $_GET['destination']; // 终点
$key = '你的密钥'; // 百度地图API密钥

$url = "http://api.map.baidu.com/directionlite/v1/transit?origin={$origin}&destination={$destination}&ak={$key}";

$result = file_get_contents($url);
echo $result;
?>
Copy after login

Step 4: Write JavaScript file
In the root directory of the project, create a main.js file to display bus route information on the front end. In this file, we will call the JavaScript SDK provided by Baidu Map API to display bus routes. The code example is as follows:

window.onload = function () {
    var map = new BMap.Map('map');
    var origin = new BMap.Point(116.404, 39.915); // 起点经纬度
    var destination = new BMap.Point(116.390, 39.915); // 终点经纬度
    
    map.centerAndZoom(origin, 12); // 设置地图初始化中心点和缩放级别
    
    var transit = new BMap.TransitRoute(map, {
        renderOptions: {map: map},
        onSearchComplete: function(result){
            if (transit.getStatus() == BMAP_STATUS_SUCCESS){
                var plan = result.getPlan(0);
                var route = plan.getRoute(0);
                var steps = route.getSteps();
                
                var html = '';
                for (var i=0; i<steps.length; i++) {
                    html += steps[i].getDescription() + '<br>';
                }
                
                document.getElementById('route').innerHTML = html;
            }
        }
    });
    
    transit.search(origin, destination);
};
Copy after login

Step 5: Test and run
Open the index.html file in the browser, and enter the addresses of the starting point and the ending point in the starting point and ending point input boxes respectively, and click the query button. You can get bus route information and display the route on the map.

Conclusion:
By using PHP and Baidu Map API, we can easily realize the function of querying and displaying bus routes. The code examples provided above can help us better understand and quickly get started with the development of this feature. Through this method, users can easily check bus routes, which provides convenience for daily travel.

The above is the detailed content of How to use PHP and Baidu Map API to query and display bus routes. 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