proper data type for storing google maps lattitude and longitude values?
...I have a Grails domain class dealing with geo location data for interacting with google maps:
Float latitude
Float longitude
When the following values get saved to a db :
latitude : 开发者_开发技巧2.5485291419153366, longitude : -76.03939712047577
I end up having : 2.54853;-76.0394 respectively. What's the best way of preserving the initial values in this case?
Thanks in advannce
Another option would be to just store them as Strings. I know that seems odd, but you're probably just passing the values back to the maps API and not doing any real calculations yourself. If that is the case, Strings are easy. Just because something is a number, doesn't mean it has to be stored in a numeric data type.
You will probably want to use BigDecimal. Floating point numbers arn't garanteed to be 100% accurit while you can control the number of digits with BigDecimal. Grails doesn't offer constraints for this so you'll have to use methods as setScale to determine the number of digits to store. FOr more info see http://download.oracle.com/javase/1.5.0/docs/api/java/math/BigDecimal.html
... in the end here's what I decided to go with:
BigDecimal latitude
BigDecimal longitude
static constraints = {
latitude( scale : 16 )
longitude( scale : 16 )
}
精彩评论