zoukankan      html  css  js  c++  java
  • 基于经纬度自动校时功能的实现【本文摘自智车芯官网】

    1. 说明

    智能手表在国外工作的时候因为在不同的时区,需要显示不同的时间,但是每次时区的改变,都要发一个短信重新设置时区,这样比较麻烦,大部分的客户不可能随时都知道改时区的指令,所以要做成自动校时的。

    具体做法是:设备重新启动,或者MCC改变的时候就会发送一个指令到服务器,服务器收到该指令后给设备下发正确时区的时间。

    实现的原理:设备上发校时指令的时候会同时带上基站信息,平台根据基站信息获得当前的位置,根据当前的位置去Google取到当前的时区,再用utc时间计算当前时区的时间发送回到设备,完成校时。

    2. 流程图

     

    3. 示例代码:

    首先可以根据设备上传的LBS或者WIFI信息利用Google LBS接口获取相应的经纬度,然后将经纬度发送给google接口

    https://maps.googleapis.com/maps/api/timezone/json?location=43.29712312,5.382137115&timestamp=1374868635&sensor=false

    返回如下:

    {

       "dstOffset" : 3600.0,

       "rawOffset" : 3600.0,

       "status" : "OK",

       "timeZoneId" : "Europe/Paris",

       "timeZoneName" : "Central European Summer Time"

    }

    获取本地时间(用到RestSharp Library)

    public DateTime GetLocalDateTime(double latitude, double longitude, DateTime utcDate)

    {

        var client = new RestClient("https://maps.googleapis.com");

        var request = new RestRequest("maps/api/timezone/json", Method.GET);

        request.AddParameter("location", latitude + "," + longitude);

        request.AddParameter("timestamp", utcDate.ToTimestamp());

        request.AddParameter("sensor", "false");

        var response = client.Execute<GoogleTimeZone>(request);

    return utcDate.AddSeconds(response.Data.rawOffset + response.Data.dstOffset);

    }

    public class GoogleTimeZone

    {

        public double dstOffset { get; set; }

        public double rawOffset { get; set; }

        public string status { get; set; }

        public string timeZoneId { get; set; }

    public string timeZoneName { get; set; }

    }

    public static double ToTimestamp(this DateTime date)

    {

        DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);

        TimeSpan diff = date.ToUniversalTime() - origin;

    return Math.Floor(diff.TotalSeconds);

    }

    调用示例:

    var myDateTime = GetLocalDateTime(33.8323, -117.8803, DateTime.UtcNow);

    本文摘自智车芯官网:http://www.thinkobd.cn/

  • 相关阅读:
    windows系统Redis客户端连接远程服务器
    Linux安装JDK1.8示例
    Nexus OrientDB数据库变为只读 storage is switched to 'read only' mode
    在SQL中有时候我们需要查看现在正在SQL Server执行的命令
    注册asp.net 到 iis时出错中的一种的 解决办法
    python 装饰器
    python 类调用实例的方法
    linux 命令大全
    python多态
    python扩展字典的功能
  • 原文地址:https://www.cnblogs.com/fangxinyu/p/9305090.html
Copyright © 2011-2022 走看看