2014-12-03 101 views
2

我想将LatlngBounds对象从客户端传递到nodejs服务器。如何使用javascript将LatlngBounds对象传递给nodejs服务器

var bounds = map.getBounds(); 

    socket.emit('sendbounds', bounds); 

在服务器:

socket.on('sendbounds',function(data){ 
    console.log(data); 
    data.getNorthEast();// undefined method getNorthEast() 

    } 

服务器可以从客户端发送的数据。但是我无法使用方法getNorthEast()。

我的解决方案是创建一个对象的LatLngBounds:

bounds = new google.maps.LatLng(data.Ea.k, data.va.j), 
            new google.maps.LatLng(data.Ea.j, data.va.k)); 

但是不建议这样做,因为我们不能确定这些按键的名称总是“EA”和“VA”。 我注意到,有时这些按键的名称是“FA”和WA”,他们是‘无证属性’。他们改变与API版本。

任何解决方案来解决这个问题呢?

+0

您的客户的边界是否包含有效值?在你的服务器中,console.log()打印什么? – Koh 2014-12-16 17:33:56

回答

1

我尝试了这种方式,太多,但有同样的问题:不能依靠变量名

相反,我把它传递到服务器作为字符串,而不是一个对象: corners = map.getBounds(); var c = corners.toString();

然后使用任何AJAX库发送字符串到服务器。 看起来如此mething这样的:

((33.94310833405608, -118.44952616442868), (33.985820303992334, -118.34120783557125))

你将不得不选择它拆开使用正则表达式,但至少它是可靠的格式化。

相关问题