2017年4月15日 星期六

Convert NMEA format data to latitude and longitude for GMap Latlng


Reference...
http://aprs.gids.nl/nmea/

Format...
$GPRMC,hhmmss.ss,A,llll.ll,a,yyyyy.yy,a,x.x,x.x,ddmmyy,x.x,a*hh
1    = UTC of position fix
2    = Data status (V=navigation receiver warning)
3    = Latitude of fix
4    = N or S
5    = Longitude of fix
6    = E or W
7    = Speed over ground in knots
8    = Track made good in degrees True
9    = UT date
10   = Magnetic variation degrees (Easterly var. subtracts from true course)
11   = E or W
12   = Checksum

Formula...
Latitude / Longitude = ddmm.mmmm = dd + (mm.mmmm/60) (* -1 for W and S)

Java code...
String str = "$GPRMC,182807.036,A,3731.9404,N,10601.6986,W,0.078542,40.74,050107,,*20"
if( str.startsWith( "$GPRMC" ) )
{
    String[] tags = str.split(",");
    if( tags[2].equals("A") ) {
        String lotdd = tags[3].substring( 0, tags[3].indexOf(".") - 2 );
        String lotmm = tags[3].substring( tags[3].indexOf(".") -2 );
        float lot = Float.valueOf( lotdd ) + Float.valueOf( lotmm )/60;
        if( tags[4].equals("S") )
            lot = -lot;
        String lngdd = tags[5].substring( 0, tags[5].indexOf(".") - 2 );
        String lngmm = tags[5].substring( tags[5].indexOf(".") -2 );
        float lng = Float.valueOf( lngdd ) + Float.valueOf( lngmm )/60;
        if( tags[6].equals("W") )
            lng = -lng;
        LatLng pos = new LatLng( lot, lng );
    } else {
        // Invalid $GPRMC
    }
}

更高效處理 micro second 的方式

更高效處理 micro second 的方式…  以 STM32 為例… __IO unsigned long sys_tick = 0; void SysTick_Handler(void) {     HAL_IncTick();     sys_tick += (SysTi...