谷歌地图纬度/经度在几何体中显示空值-Javascript

谷歌地图纬度/经度在几何体中显示空值-Javascript,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,您好,我正在使用google places api javascript sdk,希望使用ROR将结果保存在我的数据库中-我想知道四天前,它成功地保存了lat/lng,但现在保存为空。下面是我的解释 我的javascript中有这段代码 places = new google.maps.places.PlacesService(map); places.nearbySearch(request, callback); function callback(results, status) {

您好,我正在使用google places api javascript sdk,希望使用ROR将结果保存在我的数据库中-我想知道四天前,它成功地保存了lat/lng,但现在保存为空。下面是我的解释

我的javascript中有这段代码

places = new google.maps.places.PlacesService(map);
places.nearbySearch(request, callback);

function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
      clearMarkers();
//alert(JSON.stringify(results));

$.ajax({
  url: "/myurl",
  data:{
    'google_searched_locations': JSON.stringify(results)
  },
  dataType: "json",
  method: 'post',
  async : false,
  success: function(data){

  }
  });
}
在回调函数中执行
console.log(结果[0].geometry.location.lng())当我这样做时,它将结果显示为
74.3088900000002

console.log(results[0]);
输出

geometry
  Object { location=L}
location

L       { lat=function(),  lng=function(),  toString=function(),  more...}
equals  function(a)
j       function(a)
lat     function()
lng     function()
toString function()
这显然是有道理的。但是在firefox的控制台中,ajax数据参数以以下形式显示数据

[{"geometry":{"location":{}}]
为了清晰起见,我跳过了其他参数,因为请求中存在所有其他内容。但你可以看到,地点完全不同 空的,为什么?以及如何发送
结果[0]。geometry.location.lng()
值以及位置对象内的函数(如本例中的
lat()
lng()
)可能无法通过JSON传输


您必须首先解析
结果
并使用所需的值创建一些自定义值,以便能够通过JSON传输纬度和经度

,这是因为附近的搜索请求返回的
位置
属性,并且为了保存它,需要显式序列化,为此,可使用以下功能:

  • toString()
    转换为字符串表示形式

  • toUrlValue
    为该LatLng返回格式为“lat,lng”的字符串。默认情况下,我们将lat/lng值四舍五入到小数点后6位

为此,您可以引入一个新属性来存储位置字符串表示,如下所示:

var results = results.map(function(item){
         item.geometry.locationString = item.geometry.location.toString(); //string represenation of location property
         return item;   
});
示例

函数初始化(){
var pyrmont=new google.maps.LatLng(-33.8665151.1956);
var map=new google.maps.map(document.getElementById('map'){
中心:皮尔蒙特,
缩放:15,
滚轮:错误
});
//为Places API搜索指定位置、半径和位置类型。
var请求={
地点:皮尔蒙特,
半径:'500',
类型:['store']
};
//创建PlaceService并发送请求。
//使用匿名函数处理回调。
var service=newgoogle.maps.places.PlacesService(地图);
service.nearbySearch(请求、功能(结果、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
//将位置序列化为字符串
var results=results.map(函数(项){
item.geometry.locationString=item.geometry.location.toString();
退货项目;
});
document.getElementById('output').innerHTML=JSON.stringify(results,null,2);
}
});
}
//在窗口完成加载后运行initialize函数。
google.maps.event.addDomListener(窗口“加载”,初始化)
html,正文{
身高:100%;
保证金:0;
填充:0;
}
#地图{
身高:100%;
}

亲爱的,您正确地解释了逻辑,但@vadim也给出了代码和逻辑,这就是为什么我将其标记为答案,但您的答案也是100%正确的