What is the right type to store geolocation data about the user? Can I store accuracy in DbGeography?

2.6k views Asked by At

I got LoginViewModel consisting of some double which define user location in the world:

public class LoginViewModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }

    public double Latitude { get; set; }
    public double Longitude { get; set; }
    public double Accuracy { get; set; }

}

later on I would like to store them in the database as user properties.

Should I store them as three doubles or as the other already defined type: presumably ICollection<DbGeography> or define ICollection of my own type consiting of Latitude, Longitude, Accuracy?

1

There are 1 answers

0
TheLukeMcCarthy On

Don't worry about how you will store them in the database, the ORM / datalayer can take care of storing and retrieving them from your database.

Think about how they will be used in your code. If they are always used together or are releated to each other, which in your case they are for both points, then you should create a type for them. A example of this is System.Drawing.Point you don't want to pass around x and y coordinates all the time and making sure the correct x and y coordinates are passed together. You just create a point and use it.

Finally I would personally use a decimal and not a double just is case you want to calculate distance or use the lat, long in calculation in the future. decimal vs double! - Which one should I use and when? is a good explanation if you are interested.

Edit
As SteveG said in his comment above, the best type to use would be the DbGeography which was added in .Net 4.5.