我正在使用 Microsoft bot 框架构建一个聊天机器人。我被困在如何从“事件”中获取纬度和经度值。
有没有办法从“事件”中获取纬度和经度信息?
有一个similar question这里已经存在,但这并没有给出这个问题的答案。
我已经尝试过Microsoft..Bot.Builder.Location这不会给出用户的经纬度,但有助于从用户获取信息并验证它。
如有任何建议,我们将不胜感激。
最佳答案
您可以使用这个免费的 REST API 来获取该地点的纬度和经度,此 REST API 适用于 Wi-Fi 训练。
REST API for getting Latitude and Longitude of a place
制作一个与用于反序列化的 REST API 的输出相对应的模型类:
public class PlaceGeography
{
public string ip { get; set; }
public string country_code { get; set; }
public string country_name { get; set; }
public string region_code { get; set; }
public string region_name { get; set; }
public string city { get; set; }
public string zip_code { get; set; }
public string time_zone { get; set; }
public float latitude { get; set; }
public float longitude { get; set; }
public int metro_code { get; set; }
}
现在,只需从 Bot Framework 中的 C# 代码发出 GET 请求即可获得如下结果:
public async static Task<HttpResponseMessage> GetCoOrdinates()
{
string responseJSON = string.Empty;
PlaceGeography obj = new PlaceGeography();
using (HttpClient client = new HttpClient())
{
string URI = $"http://freegeoip.net/json/";
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (HttpResponseMessage msg = await client.GetAsync(URI))
{
if (msg.IsSuccessStatusCode) //if HTTP 200 then
{
responseJSON = await msg.Content.ReadAsStringAsync();
JsonConvert.PopulateObject(responseJSON, obj); // Deserialize model class object and get latitude and longitude
return msg;
}
else
return msg;
}
}
}
关于geolocation - 如何使用 BotFramework 获取 ChatBot 用户纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46908048/