如何发送HTTP请求到百度地图API的接口
时间: 2023-12-13 12:03:32 浏览: 27
要发送HTTP请求到百度地图API的接口,你可以使用ESP8266的HTTPClient库来实现。以下是一个简单的示例代码,演示如何发送GET请求到百度地图API的地理编码接口:
```cpp
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
}
void loop() {
if (WiFi.status() == WL_CONNECTED) {
HTTPClient http;
// 设置百度地图API的URL
String url = "http://api.map.baidu.com/geocoding/v3/?address=北京市海淀区上地十街10号&city=北京市&output=json&ak=your_API_KEY";
http.begin(url); // 发起HTTP GET请求
int httpResponseCode = http.GET(); // 获取HTTP响应代码
if (httpResponseCode == HTTP_CODE_OK) {
String payload = http.getString(); // 获取响应内容
Serial.println(payload);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end(); // 关闭HTTP连接
delay(5000); // 延迟5秒后再次发送请求
}
}
```
在上面的代码中,你需要将`your_SSID`和`your_PASSWORD`替换为你的无线网络的名称和密码。同时,将URL中的`your_API_KEY`替换为你在百度地图开放平台上获取的API密钥。
此代码将发送GET请求到百度地图API的地理编码接口,以北京市海淀区上地十街10号为例。你可以根据需要修改URL中的地址和其他参数。
希望这能帮助到你!