Search code examples
c#sql-server-2008geolocation

System.FormatException: 24201: Latitude values must be between -90 and 90 degrees


UPDATE

Problem was user's locale! I have sorted the issue with

double lat = double.Parse(Request.Form["lat"], CultureInfo.InvariantCulture);

Actual question

This is my first attempt to implement location on my mobile site. I'm getting the user's location from their phone with w3 geolocation API and convert it to double then store it as float in sqlserver.

In most cases it works fine but in some cases the coordinates provided by the phone are quite long. For example latitude may be 3.1234567890123 so 14 numbers after the dot.

This gets saved as 3.1234567890123+E16 and causes error in the title when I try to calculate distance of user to certain point.

What is the best way to avoid it? Should I trim input from the user to a certain length so it would be converted properly?

Method to get and save data

double lat = Convert.ToDouble(Request.Form["lat"]);
double lng = Convert.ToDouble(Request.Form["lng"]);
user.UpdateMyLocationCoordinates(user.id, lat, lng);

public void UpdateMyLocationCoordinates(int userId, double lat, double lng)
{
    SiteDbHelper db = new SiteDbHelper();
    List<SqlParameter> sqlParameters = new List<SqlParameter>();
    sqlParameters.Add(db.CreateParameter("@userId", SqlDbType.Int, userId));
    sqlParameters.Add(db.CreateParameter("@latitude", SqlDbType.Float, lat));
    sqlParameters.Add(db.CreateParameter("@longitude", SqlDbType.Float, lng));
    db.UpdateInsertDeleteReturnAffectedRowCount ("SocialSiteUser_UpdateMyLocationCoordinates", CommandType.StoredProcedure, sqlParameters);
}

CreateParameter

public SqlParameter CreateParameter(string name, SqlDbType type, double value)
{
    SqlParameter param = new SqlParameter(name, value);
    param.SqlDbType = type;
    return param;
}

SP o store data is

ALTER PROCEDURE [dbo].[SocialSiteUser_UpdateMyLocationCoordinates]
@userId int,
@latitude float,
@longitude float
AS
BEGIN
    SET NOCOUNT ON
    update domains_users set latitude=@latitude, longitude=@longitude where id=@userId
END

Solution

  • Problem was user's locale! I have sorted the issue with

     double lat = double.Parse(Request.Form["lat"], CultureInfo.InvariantCulture);