flutter 如何将lat lng转换为gt06格式

628mspwn  于 2023-05-30  发布在  Flutter
关注(0)|答案(1)|浏览(235)

我是新的Flutter,我正试图转换lat lng到这种格式
格式长度(字节)起始位2包长度1协议号1信息内容N信息序列号2错误校验2停止位2
我写了这个函数,但它不工作。

String convertCoordinatesToFormat(double latitude, double longitude) {
    // Start Bit (2 bytes)
    String startBit = (0x78.toString()) ;

    // Protocol Number (1 byte)
    int protocolNumber = 0x22; // Replace with the appropriate protocol number

    // Information Content (N bytes)
    // Convert latitude and longitude to the desired format, e.g., as strings
    String latitudeString = latitude.toStringAsFixed(6); // Replace with the desired precision for latitude
    String longitudeString = longitude.toStringAsFixed(6); // Replace with the desired precision for longitude
    String informationContent = "$latitudeString$longitudeString";

    // Calculate the length in bytes
    int length = 5 + informationContent.length ~/ 2; // Divide by 2 since each byte is represented by 2 characters in hexadecimal

    // Length (1 byte)
    String lengthString = length.toRadixString(16).padLeft(2, "0");

    // Packet Length (1 byte)
    // int packetLength = lengthString; // Length of the remaining bytes (Protocol Number + Information Content + Information Serial Number + Error Check + Stop Bit)

    // Information Serial Number (2 bytes)
    int informationSerialNumber = 1; // Replace with the appropriate serial number

    // Error Check (2 bytes)
    int errorCheck = calculateErrorCheck(informationContent);
    String errorCheckString = errorCheck.toRadixString(16).padLeft(4, "0");

    // Stop Bit (2 bytes)
    String stopBit = (0x0D.toString()) + (0x0A.toString());

    // Concatenate all the fields together
    String result = "$startBit$lengthString$protocolNumber$informationContent$informationSerialNumber$errorCheckString$stopBit";

    List<int> bytes = utf8.encode(result);
    String answer = "";
    for (int a in bytes){
      answer += a.toString();
    }

    // Convert the formatted coordinates to uppercase
    return answer;
  }

  int calculateErrorCheck(String content) {
    int sum = 0;
    for (int i = 0; i < content.length; i++) {
      sum += content.codeUnitAt(i);
    }
    return sum % 65536;
  }
slwdgvem

slwdgvem1#

以下是Traccar GPS服务器(Java)源代码的相关部分:

DateBuilder dateBuilder = new DateBuilder(timezone)
            .setDate(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte())
            .setTime(buf.readUnsignedByte(), buf.readUnsignedByte(), buf.readUnsignedByte());
    position.setTime(dateBuilder.getDate());

    if (hasLength && buf.readUnsignedByte() == 0) {
        return false;
    }

    if (hasSatellites) {
        position.set(Position.KEY_SATELLITES, BitUtil.to(buf.readUnsignedByte(), 4));
    }

    double latitude = buf.readUnsignedInt() / 60.0 / 30000.0;
    double longitude = buf.readUnsignedInt() / 60.0 / 30000.0;

    if (hasSpeed) {
        position.setSpeed(UnitsConverter.knotsFromKph(buf.readUnsignedByte()));
    }

    int flags = buf.readUnsignedShort();
    position.setCourse(BitUtil.to(flags, 10));
    position.setValid(BitUtil.check(flags, 12));

    if (!BitUtil.check(flags, 10)) {
        latitude = -latitude;
    }
    if (BitUtil.check(flags, 11)) {
        longitude = -longitude;
    }

    position.setLatitude(latitude);
    position.setLongitude(longitude);

GT06解码器的完整源代码是here
请注意,它使用的是Netty库,因此代码片段使用的是Netty ByteBuf

相关问题