简体   繁体   English

在python-telegram-bot中从用户获取实时位置

[英]Get Live Location from user in python-telegram-bot

I need to get an updated user's location. 我需要获取更新的用户位置。 When I send my live location to the bot, I can read latitude and longitude, but they stay constant. 当我将实时位置发送给机器人时,我可以读取经度和纬度,但它们保持不变。 I see on the map how my location changes, but the bot is not able to update the coordinates. 我在地图上看到了我的位置如何变化,但是该漫游器无法更新坐标。

When I sent my live location, function getLocation save my message and show coordinates. 当我发送实时位置时,功能getLocation保存我的消息并显示坐标。

def getLocation(bot, update, user_data):
    msg = update.message
    user_data['msg'] = msg
    user_data['id'] = update.update_id
    update.message.reply_text('lat: {}, lng: {}'.format(
        msg.location.latitude, msg.location.longitude))

Then when I moves, I want to know my actual coordinates with /track command, but it shows old coordinates. 然后,当我移动时,我想使用/track命令知道我的实际坐标,但是它显示了旧坐标。

def showCoordinates(bot, update, user_data):
    # I need to update location here but I don't know
    newLoc = user_data['msg']
    update.message.reply_text('lat: {}, lng: {}'.format(
        newLoc.location.latitude, newLoc.location.longitude))

This is my dispatcher 这是我的调度员

dp = updater.dispatcher
dp.add_handler(MessageHandler(Filters.location,
                              getLocation,
                              pass_user_data=True))
dp.add_handler(CommandHandler('track',
                              showCoordinates,
                              pass_user_data=True))

I tried with Update(user_data['id'], edit_message=user_data['loc']) inside the function showCoordinates but it doesn't work for me. 我尝试在函数showCoordinates使用Update(user_data['id'], edit_message=user_data['loc']) ,但对我不起作用。

In getLocation you stored coordinates in user_data['msg'] , then you request newLoc from the old user_data['msg'] which contained previous coordinates. getLocation您将坐标存储在user_data['msg'] ,然后从包含先前坐标的旧user_data['msg']请求newLoc
You should update data variable when you moves. 移动时应更新数据变量。 it's like checkin in you case. 就像您要签到一样。 Otherwise, new Telegram API a support a live location. 否则,新的Telegram API支持现场位置。 please see the docs. 请参阅文档。

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

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