Can't affect values to a simple Double[] table

2019-08-25 12:51发布

I've asked a similar question about it : Can't affect values to a simple Double[] table which is always null

I have a TXT file containing NMEA frames. I retrieve the latitude and the longitude of $GPGGA and $GPRMC frames. Then I convert the latitude and longitude into Decimal Degrees. The first version of my code is working great. Then I made a second one, more structured, but which doesn't work.

Here is the second version of my code which doesn't work :

/**
 * Updating position on Google Maps
 * @param values
 */
public void showMyPosition(String values)
{
    String[]selectedposition=values.split(",");
    Double[]decimalcoordinates=new Double[2];
    if(selectedposition.length>1 || selectedposition[0].equals("$GPGGA"))
    {
        int[]coordinatesposition=new int[]{2,4};
        decimalcoordinates = convertNmeaToDecimal(selectedposition, coordinatesposition);
    }
    else if(selectedposition.length>1 || selectedposition[0].equals("$GPRMC"))
    {
        int[]coordinatesposition=new int[]{3,5};
        decimalcoordinates = convertNmeaToDecimal(selectedposition, coordinatesposition);
    }

    LatLng position=new LatLng(decimalcoordinates[0],decimalcoordinates[1]);
    googlemap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
    marker=googlemap.addMarker(new MarkerOptions().title("You are here !").position(position));
    googlemap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, 19));
}   

/**
 * Convert NMEA coordinates into Decimal degrees coordinates.
 * @param nmeaframes 
 *      Contains all the NMEA frame
 * @param coordinatesposition
 *      Contains the position of the latitude and longitude into nmeaframes[]
 * @return decimalcoordinates*      
 */
public Double[] convertNmeaToDecimal(String[]nmeaframes, int[]coordinatesposition)
{
    Double nmeacoordinates=null;
    Double[]decimalcoordinates=new Double[2];

    for(int i=0; i<2; i++)
    {
        nmeacoordinates=Double.parseDouble(nmeaframes[coordinatesposition[i]]);
        decimalcoordinates[i]=Math.floor(nmeacoordinates/100)+(nmeacoordinates-Math.floor(nmeacoordinates/100)*100)/60;
    }
    return decimalcoordinates;
}

Here is my LogCat :

12-17 14:47:03.476: E/AndroidRuntime(6769): FATAL EXCEPTION: main
12-17 14:47:03.476: E/AndroidRuntime(6769): java.lang.NumberFormatException: Invalid double: "A"
12-17 14:47:03.476: E/AndroidRuntime(6769):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)

I think I'm so in my code, that I can no longer see my mistakes. It seems to be the same as my precedent question. But I can't solve it.

Can you help me please ?

EDIT - This is a really careless mistakes... Thanks to Alex Walker who help me to pointed my error

To give the original functionality, you need to change both of those || operators to &&. That way it will check both cases as required. That is, use:

if(selectedposition.length>1 && selectedposition[0].equals("$GPGGA"))

and

else if(selectedposition.length>1 && selectedposition[0].equals("$GPRMC"))

标签: java gps nmea
1条回答
混吃等死
2楼-- · 2019-08-25 13:32

These code samples are extremely long and very similar, so it's painfully difficult to pin down what the actual problem is.

However one obvious mistake that does stand out is in these lines from version 2:

if(selectedposition.length>1 || selectedposition[0].equals("$GPGGA"))

and

else if(selectedposition.length>1 || selectedposition[0].equals("$GPRMC"))

According to your version 1 code, the algorithm should check that selectedposition.length>1, and if it is, then go and try to do stuff. If it isn't, don't do anything.

But in the version 2 code listed above, the block

else if(selectedposition.length>1 || selectedposition[0].equals("$GPRMC"))

will NEVER be called unless the input is exactly one character long.

To give the original functionality, you need to change both of those || operators to &&. That way it will check both cases as required. That is, use:

if(selectedposition.length>1 && selectedposition[0].equals("$GPGGA"))

and

else if(selectedposition.length>1 && selectedposition[0].equals("$GPRMC"))
查看更多
登录 后发表回答