Vedant Kakade
Vedant Kakade

Reputation: 1

How to convert lat lng in the format for gt06

I am new to flutter and I am trying to convert lat lng to this format

Format Length(Byte) Start Bit 2 Packet Length 1 Protocol Number 1 Information Content N Information Serial Number 2 Error Check 2 Stop Bit 2

I have written this function but this isn't working.

  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;
  }

Upvotes: 0

Views: 132

Answers (1)

Anton Tananaev
Anton Tananaev

Reputation: 2523

Here's the relevant part of the source code from Traccar GPS server (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);

Full source code for the GT06 decoder is here.

Note that it is using Netty library, so the snippet is using Netty ByteBuf.

Upvotes: 0

Related Questions