我正在尝试保存纬度和经度的地理数据类型。我的脚本是:
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":
有什么想法吗?谢谢。
最佳答案
您必须手动构建整个 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)
.
关于sql-server - Azure移动服务脚本保存经纬度的地理信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25788238/