Convert Lat / Lon to Integer

The service I consume requires that I pass it lat / lon for the address as integers. Currently my lat / lon is stored as doubles:

double lat = 38.898748; double lon = -77.037684; 

I have exhausted my google-fu and cannot find a way to convert lat / lon to an integer representation. Any help would be appreciated.

+4
source share
7 answers

Sometimes it's simple, just multiply by 1 million.

Multiply by .000001 to go back.

Suppose it is assumed that you only want the precision of the 6th decimal place.

+5
source

You need to know what the Surveying System you are using and what the API expects. For instance. WGS84, NAD83, OSGB36, ED50 ... (Example: Google Earth uses WGS84 )

If they need an integer, they are probably using something other than Google and other vendors. Most likely, rounding off a double or some other integer conversion will not work. You need information about the geodetic system, and then do the conversion between the two values.

+6
source

The only way I see this is to change the decimal point.

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

If you need 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 coodains in higher resolution, such as the 100th second, just multiply by 100.

+1
source

I am going to take a wild hit and assume that you want to convert pure degrees to degrees: minutes: seconds as a whole.

Here is one way to convert this to an integer, but obviously you will need to confirm that this is the format they use.

 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, 385355 or 38 degrees 53 minutes 55 seconds is returned. Sorry if this looks a little messy and type casting might not be the most efficient (you definitely need to make sure it is rounded correctly, I think the casting to int type is rounded all the time), but it will give you longitude / latitude.

+1
source

The answer may 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 stated earlier by me and others, it all depends on what the API expects.

The second two options would be lossless, but fancy.

0
source
 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; 
0
source

Source: https://habr.com/ru/post/1380036/


All Articles