简体   繁体   English

使用 Python 进行实时位置跟踪

[英]Live Location Tracking using Python

I am using an app called cedalo connect to get my phone's GPS data using an online mqtt server.我正在使用一个名为 cedalo connect 的应用程序,通过在线 mqtt 服务器获取我手机的 GPS 数据。 I constantly get latitude and longitude information using this app.我经常使用这个应用程序获取经纬度信息。

I want to show it on a map using python. I wrote such a code but it kept giving me errors like (AttributeError: module 'folium' has no attribute 'MarkerCluster') I honestly don't know what else to use.我想使用 python 在 map 上显示它。我写了这样的代码,但它一直给我这样的错误 (AttributeError: module 'folium' has no attribute 'MarkerCluster') 我真的不知道还能用什么。

Could you please tell me what I can do about it?你能告诉我我能做些什么吗?

what I tried:我试过的:

import json
import paho.mqtt.client as mqtt
import folium
import time
from folium.plugins import MarkerCluster

# Variable to hold the current latitude and longitude
latitude = 50.780036278929614
longitude = 6.10363592985153

# Callback function when a message is received
def on_message(client, userdata, message):
    global latitude, longitude
    # Convert the message payload from bytes to string
    data = message.payload.decode()
    # parse the json string
    data = json.loads(data)
    # Extract the latitude and longitude
    if 'GPS' in data:
        gps_data = data["GPS"]
        latitude = gps_data["Latitude"]
        longitude = gps_data["Longitude"]

# Create MQTT client
client = mqtt.Client()

# Attach callback function to the message event
client.on_message = on_message

# Connect to the MQTT broker
client.connect("broker.mqttdashboard.com", 1883)

# Subscribe to the desired topic
client.subscribe("gpsdata")

# Start the MQTT loop
client.loop_start()

# Initialize the map
# Initialize the map
m = folium.Map(location=[latitude, longitude], zoom_start=15)

mc = folium.MarkerCluster()
m.add_child(mc)

marker = None
while True:
    # Check if the latitude and longitude have been updated
    if latitude and longitude:
        # Remove the previous marker
        if marker:
            mc.remove_child(marker)
        # Update the marker on the map to the current location
        marker = folium.Marker(location=[latitude, longitude])
        mc.add_child(marker)

        # Save the map
        m.save("current_location.html")
    # Wait for one second
    time.sleep(1)

The result is an attribute error.结果是属性错误。

The MarkerCluster class is located in folium.plugins . MarkerCluster class 位于folium.plugins中。 So, instead of所以,而不是

mc = folium.MarkerCluster()

try尝试

mc = MarkerCluster()

or或者

mc = folium.plugins.MarkerCluster()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM