简体   繁体   中英

Convert Lat/Lon to Integer

A service that I'm consuming requires that I pass it the lat/lon for an address as integers. Currently, my lat/lon's are stored as doubles:

double lat = 38.898748;
double lon = -77.037684;

I've exhausted my Google-fu and can't find a method for converting lat/lon to an integer representation. Any help would be appreciated.

You need to know what Geodetic System you're using and what the API expects. Eg WGS84, NAD83, OSGB36, ED50... (Example: Google Earth uses WGS84 )

If they need an integer then they're probably using something different from Google and other providers. Chances are that rounding aa double or some other integer conversion will not work. You need the Geodetic System information and then make the conversion between the two values.

Sometimes it is simple, just multiply by 1 million.

Multiply by .000001 to convert back.

Granted this assumes you only want precision to the 6th decimal.

The only way I can see this happening is if you shift the decimal point.

int lat = (int)(38.898748 * 1000000.0);
int lon = (int)(-77.037684 * 1000000.0);

If you need the coordinates in seconds, just multiply by 60 * 60 :

int latitude = (int)Math.Round(lat * 60.0 * 60.0);
int longitude = (int)Math.Round(lon * 60.0 * 60.0);

If you need the coodinates in higher resolution, for example 100th of a second, just multiply by 100 more.

I am going to take a wild stab at this and guess that you are wanting to convert pure degrees to Degrees:Minutes:Seconds in the form of a single integer.

Here is one way you could convert this into an integer, but obviously you are going to want to confirm this is the format they are using.

double originalLat = 38.898748,
    TEMPdecimal;

int degrees = (int)originalLat;

TEMPdecimal = (originalLat - degrees) * 60;

int minutes = (int)TEMPdecimal;

TEMPdecimal = (TEMPdecimal - minutes) * 60;

int seconds = (int)TEMPdecimal;

int lat = (degrees * 10000) + (minutes * 100) + seconds;

In this case the return is 385355 or 38 degrees 53 minutes 55 seconds. Sorry if this looks a little sloppy and the type casting might not be the most efficient (definitely would need to make sure it rounds properly, i think type casting to int is rounding down all the time) but it will give you longitude/latitude.

double_lat = 38.898748;

double_lon = -77.037684;

A simple way to store decimal positions as integers:

lat = (double_lat + 90) * 1000000;

lon = (double_lon + 180) * 1000000;

lat = 128898748;
lon = 102962316;

The answer could be either,

int FactorConversion(double degrees)
{
    return (int) degrees * MagicConversionFactor 
    //You supply this number, a likely candidate is 360 since
    //this converts degrees to seconds 
}

or

 int BinaryConversion32(float degrees)
 {
     return BitConvetor.ToInt32(BitConvertor.GetBytes(degrees))
 }

or

long BinaryConversion64(double degrees)
{
    return BitConvetor.ToInt64(BitConvertor.GetBytes(degrees))
}

note that double is an 8 byte type. As previously stated by me and others, it all rather depends on what the API expects.

The second two options would be lossless but bizarre.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM