2015-10-19 36 views
3

您好我正在使用谷歌地方api javascript sdk,並希望將結果保存在我的數據庫中使用ROR - 我想知道,前四天,它正在節省lat/lng成功,但現在它保存爲空。下面是我的解釋谷歌地圖經緯度在幾何中顯示爲空 - Javascript

我有這個代碼在我的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(results[0].geometry.location.lng());它顯示我作爲結果而74.30889000000002當我做

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":{}}] 

我跳過爲了清楚起見,其它參數,因爲所有其他的事情存在於該請求。但是你可以看到位置完全是空的,爲什麼?以及如何發送results[0].geometry.location.lng()值,即在位置對象內

回答

2

它發生,因爲最近搜索請求返回的google.maps.LatLng typelocation財產運輸經度和緯度,以保存需要顯式序列化,爲此目的,可以使用以下功能:

  • toString()轉換爲字符串表示。

  • toUrlValue爲此LatLng返回形式爲「lat,lng」的字符串。我們將lat/lng值默認爲六位小數。

你可以引入一個新的屬性,用於存儲位置字符串表示用於該目的,如下面所示:

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

function initialize() { 
 
    var pyrmont = new google.maps.LatLng(-33.8665, 151.1956); 
 

 
    var map = new google.maps.Map(document.getElementById('map'), { 
 
     center: pyrmont, 
 
     zoom: 15, 
 
     scrollwheel: false 
 
    }); 
 

 
    // Specify location, radius and place types for your Places API search. 
 
    var request = { 
 
     location: pyrmont, 
 
     radius: '500', 
 
     types: ['store'] 
 
    }; 
 

 
    // Create the PlaceService and send the request. 
 
    // Handle the callback with an anonymous function. 
 
    var service = new google.maps.places.PlacesService(map); 
 
    service.nearbySearch(request, function (results, status) { 
 
     if (status == google.maps.places.PlacesServiceStatus.OK) { 
 

 

 
      //serialize location to string 
 
      var results = results.map(function(item){ 
 
       item.geometry.locationString = item.geometry.location.toString(); 
 
       return item; 
 
      }); 
 

 
      document.getElementById('output').innerHTML = JSON.stringify(results, null, 2); 
 
     } 
 
    }); 
 
} 
 

 
// Run the initialize function when the window has finished loading. 
 
google.maps.event.addDomListener(window, 'load', initialize);
html, body { 
 
      height: 100%; 
 
      margin: 0; 
 
      padding: 0; 
 
     } 
 

 
     #map { 
 
      height: 100%; 
 
     }
<script src="https://maps.googleapis.com/maps/api/js?signed_in=true&libraries=places"></script> 
 
<div id="map"></div> 
 
<pre id="output"></pre>

1

函數(例如,在此例中爲lat()lng())可能無法通過JSON傳輸。

您必須首先分析result和創建具有所期望的值一些自定義的值,以便能夠通過JSON

+1

親愛的你正確地解釋了邏輯,但@vadim也給出了邏輯代碼,這就是爲什麼我將這個標記爲答案,但是你的答案也是100%正確的 – ImranNaqvi

相關問題