python根据经纬在google 绘制轨迹_使用Python调用谷歌地图并记录运动轨迹进行可视化...
时间: 2023-07-15 17:11:33 浏览: 217
使用 Python 绘制 Google 地图上的轨迹需要使用 Google Maps API。您需要首先获取一个 Google 地图 API 密钥,然后使用 Python 的 requests 库向 Google Maps API 发送请求,以获取地图数据。然后,您可以使用 matplotlib 库或其他绘图库将数据可视化为轨迹。
以下是一个示例代码,演示如何在 Google 地图上绘制一个简单的轨迹:
```python
import requests
import matplotlib.pyplot as plt
# Replace YOUR_API_KEY with your actual API key
API_KEY = "YOUR_API_KEY"
# Define the starting and ending points of the track
start = "40.748817,-73.985428"
end = "37.7749,-122.419416"
# Send a request to Google Maps API to get the directions
url = f"https://maps.googleapis.com/maps/api/directions/json?origin={start}&destination={end}&key={API_KEY}"
response = requests.get(url)
data = response.json()
# Extract the coordinates from the directions data
points = []
for step in data["routes"][0]["legs"][0]["steps"]:
points.extend(step["polyline"]["points"])
# Decode the polyline data into latitude and longitude pairs
def decode_polyline(polyline_str):
index, lat, lng = 0, 0, 0
coordinates = []
while index < len(polyline_str):
b, shift = 0, 0
while True:
b = ord(polyline_str[index]) - 63
index += 1
lat += (b & 0x1f) << shift
shift += 5
if not b & 0x20:
break
shift, b = 0, 0
while True:
b = ord(polyline_str[index]) - 63
index += 1
lng += (b & 0x1f) << shift
shift += 5
if not b & 0x20:
break
coordinates.append((lat / 100000.0, lng / 100000.0))
return coordinates
coordinates = decode_polyline("".join(points))
# Plot the track on a Google map
fig, ax = plt.subplots(figsize=(8,8))
ax.plot([lng for lat, lng in coordinates], [lat for lat, lng in coordinates], color='blue', alpha=0.7, linewidth=3, solid_capstyle='round', zorder=2)
ax.set_title('Track from New York to San Francisco')
ax.set_xlim(min([lng for lat, lng in coordinates])-1, max([lng for lat, lng in coordinates])+1)
ax.set_ylim(min([lat for lat, lng in coordinates])-1, max([lat for lat, lng in coordinates])+1)
ax.axis('off')
plt.show()
```
在这个例子中,我们使用 Google Maps API 获取从纽约到旧金山的方向,并使用 matplotlib 绘制了轨迹。当然,您可以使用其他库和数据来绘制更复杂的轨迹。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)