Java – GeoPoint getLatitudeE6() returns -80000000 but getLongitudeE6() returns the correct value

GeoPoint getLatitudeE6() returns -80000000 but getLongitudeE6() returns the correct value… here is a solution to the problem.

GeoPoint getLatitudeE6() returns -80000000 but getLongitudeE6() returns the correct value

Very strange bug, I can’t seem to figure it out, I

spent about an hour trying to fix this by refactoring but I can’t seem to figure it out, maybe the new eyes will help me. A small snippet of code is listed below. Thanks for your help.

Example KML coordinate string

-98.493095,29.416311,0.000000

KMLHandler.java (I read in KML in string format).

String[] coords = s.split(",");  
   if ( coords.length == 3 ) {  
      GeoPoint gp = GeoPointUtils.getGeoPoint(coords[0].trim(), coords[1].trim());
      ((Region)overlayItem).addCoordinate(gp);
      Log.d(TAG, "gp.getLat(): " + gp.getLatitudeE6())
      Log.d(TAG, "gp.getLong():" + gp.getLongitudeE6());
    }

GeoPointUtils.java

public static GeoPoint getGeoPoint(double latitude , double longitude) {
   Log.d(TAG, "GeoPointUtils.getGeoPoint(double)");
   Log.d(TAG, "\tIncoming lat -> " + latitude);
   Log.d(TAG, "\tConverted lat -> " + (int) (latitude * 1E6));
   Log.d(TAG, "\tIncoming long -> " + longitude);
   Log.d(TAG, "\tConverted long -> " + (int) (longitude * 1E6));

return new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));
}

public static GeoPoint getGeoPoint(String latitude , String longitude) {
   Log.d(TAG, "GeoPointUtils.getGeoPoint(String)");
   Log.d(TAG, "\tIncoming lat -> " + latitude);
   Log.d(TAG, "\tConverted lat -> " + Double.parseDouble(latitude));
   Log.d(TAG, "\tIncoming long -> " + longitude);
   Log.d(TAG, "\tConverted long -> " + Double.parseDouble(longitude));

return getGeoPoint(Double.parseDouble(latitude), Double.parseDouble(longitude));
}

Logcat result (sorry for syntax highlighting).

GeoPointUtils.getGeoPoint(String)
   Incoming lat  -> -98.493095
   Converted lat -> -98.493095
   Incoming long ->  29.416311
   Converted long -> 29.416311
GeoPointUtils.getGeoPoint(double)
   Incoming lat  -> -98.493095
   Converted lat -> -98493095
   Incoming long ->  29.416311
   Converted long -> 29416311
gp.getLat(): -80000000
gp.getLong(): 29416311

Solution

Wild Donkey Guess: Latitude is defined as the angle north or south of the equator, so it is limited to -90..+90 degrees. The latitude you entered, around -98.5, is fake, which could mess things up.

Now, assuming the latitude in your code is expressed in units other than degrees, I’d say you’ve hit some kind of hard fence somewhere.

 You expected gp.getLat(): -98493095
 You got      gp.getLat(): -80000000

Related Problems and Solutions