Azure移动服务脚本从lat long保存地理位置

时间:2014-09-11 13:04:17

标签: sql-server azure azure-mobile-services

我正在尝试从纬度和经度中保存地理数据类型。我的剧本是:

function insert(item, user, request) {

    var incidentesTable = tables.getTable('incidentes');
    var detallesTable = tables.getTable('detalle_incidente');
    var point = "geography::STPointFromText('POINT(" + item.latitud.substring
        + " " + item.longitud.substring +")', 4326)";
        console.log(point);
    var items = {
        descripcion: item.descripcion,
        image: item.image,
        fecha: item.fecha,
        latitud: item.latitud,
        longitud: item.longitud,
        violencia: item.violencia,
        geopoint: point
    }

    incidentesTable.insert(items, {
        success: function()
        {
            var detalle = {incidente_id: items.id}; 
            detallesTable.insert(detalle, {
                success: function(){
                    request.respond(statusCodes.OK, {id: items.id});
                }
            })

        }
    }); 
}

记录点变量,其值为:

geography::STPointFromText('POINT(-33.5359 -70.7802)', 4326)

看起来很好,但它没有用,它引发了一个错误:

Error in script '/table/incidentes.insert.js'. Error: [Microsoft][SQL Server Native Client 10.0][SQL Server]A .NET Framework error occurred during execution of user-defined routine or aggregate "geography":

有什么想法吗?感谢。

1 个答案:

答案 0 :(得分:1)

您必须手动构造整个INSERT语句。我找到了一个例子here。从那篇文章:

function insert(item, user, request) { 
    var queryString = "INSERT INTO Place (title, description, location) VALUES (?, ?, geography::STPointFromText('POINT(' + ? + ' '
+ ? + ')', 4326))";         
    mssql.query(queryString, [item.title, item.description, item.longitude.toString(), item.latitude.toString()], { 
                success: function() { 
                     request.respond(statusCodes.OK, {}); 
                } 
          });
}

编辑2 : 请注意,您的经度和经度都是向后的; "着名的文字" SQL Server使用的格式首先是经度。

修改: 此外,您引用的错误消息从下一行开始有完整的异常。如果构造SQL语句时出错,请尝试在SSMS中执行SQL或等效以获取完整错误。这是一个例子:

[SQL Server]A .NET Framework error occurred during execution of user-defined routine or aggregate "geography":  
System.FormatException: 24201: Latitude values must be between -90 and 90 degrees. System.FormatException:
 at Microsoft.SqlServer.Types.GeographyValidator.ValidatePoint(Double x, Double y, Nullable`1 z, Nullable`1 m)
 at Microsoft.SqlServer.Types.Validator.BeginFigure(Double x, Double y, Nullable`1 z, Nullable`1 m)
 at Microsoft.SqlServer.Types.ForwardingGeoDataSink.BeginFigure(Double x, Double y, Nullable`1 z, Nullable`1 m)
 at Microsoft.SqlServer.Types.CoordinateReversingGeoDataSink.BeginFigure(Double x, Double y, Nullable`1 z, Nullable`1 m)
 at Microsoft.SqlServer.Types.WellKnownTextReader.ParsePointText(Boolean parseParentheses)
 at Microsoft.SqlServer.Types.WellKnownTextReader.ParseTaggedText(OpenGisType type)
 at Microsoft.SqlServer.Types.WellKnownTextReader.Read(OpenGisType type, Int32 srid)
 at Microsoft.SqlServer.Types.SqlGeography.GeographyFromText(OpenGisType type, SqlChars taggedText, Int32 srid)
.