无法反序列化GoogleMaps DirectionsResult对象 [英] Can't Deserialize GoogleMaps DirectionsResult Object

查看:105
本文介绍了无法反序列化GoogleMaps DirectionsResult对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是GoogleMaps API v3.0,并试图将DirectionsResult保存到我的数据库中,然后稍后检索它以在地图上使用。我的问题是,当我尝试通过从我的数据库中提取JSON表示来重新保存保存的对象时,该对象只是愚蠢的JSON,它没有其构成对象的原始方法和函数。因此,我构建了一个修复例程,它采用dumbalt文本JSON并通过重建所有LatLng和LatLngBound对象来重建它。但是,由于我的固定对象不像原始图片那样工作,所以仍有一些东西丢失,这两个点出现在我的地图上,但是它们之间的紫色线缺失。



希望对任何关于序列化/水化的更好技术或任何关于我的修复程序可能会丢失的想法有任何建议。



谢谢



  request = {
origin: homeLocation,
destination:jobLocation,
travelMode:google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request,function(response,status){
if(status == google.maps.DirectionsStatus.OK){
var str = Ext.encode(response); / /<< ==将原始JSON对象保存到DB(我使用ExtJs)
var z = eval('('+ str +')'); //<< == REHYDRATING DirectionsResult RAW JSON OBJECT
FixDirectionResult(z); //<< ==尝试重新创建原始对象
directionsRenderer.setDirections(z); //<< ==这个工作带有响应但不是WITH z
}
);
函数FixDirectionResult(rslt){
for(r = 0; r var route = rslt.routes [r];
var bounds = route.bounds;
route.bounds = new google.maps.LatLngBounds(
new google.maps.LatLng(bounds.Ub,bounds.Od),
new google.maps.LatLng(bounds.Ud, bounds.Ob));

for(l = 0; l var leg = route.legs [l];
leg.start_location = new google.maps.LatLng(leg.start_location.wa,leg.start_location.ya);
leg.end_location = new google.maps.LatLng(leg.end_location.wa,leg.end_location.ya);

for(s = 0; s var step = leg.steps [s];
step.start_location =
new google.maps.LatLng(step.start_location.wa,step.start_location.ya);
step.end_location =
new google.maps.LatLng(step.end_location.wa,step.end_location.ya);

for(p = 0; p var path = step.path [p];
step.path [p] = new google.maps.LatLng(step.path.wa,step.path.ya);



$ b(o = 0; o< route.overview_path.length; o ++){
var overview = route.overview_path [O];
route.overview_path [o] = new google.maps.LatLng(overview.wa,overview.ya);




解决方案

从你的代码的外观看来,你没有正确访问lat和lng。谷歌地图api库被缩小。变量名通常缩写为一组随机字符。你不应该通过这些变量来访问x和y,而是通过他们的getter:即。 lat() lng()来避免未来版本的相同问题。希望这是导致你的方向无法呈现的问题。



获得经度和纬度的正确建议方法与以下类似:

  results [0] .geometry.location.lat()。toFixed(3); 
results [0] .geometry.location.lng()。toFixed(3);






所以,例如下面这行应该是:

  step.start_location = new google.maps.LatLng(step.start_location.wa,step.start_location.ya); 
step.end_location = new google.maps.LatLng(step.end_location.wa,step.end_location.ya);

收件人:

  step.start_location = new google.maps.LatLng(step.start_location.lat(),step.start_location.lng()); 
step.end_location = new google.maps.LatLng(step.end_location.lat(),step.end_location.lng());






Google地图数据的存储位于服务期限内。以下是限制条件,您可以在继续使用数据存储之前查看一下:


  10.1.3限制数据导出或复制。 

(a)没有未经授权复制,修改或衍生衍生产品

作品或显示内容。您
不得复制,翻译,修改或
创建派生作品(包括
创建或捐赠
数据库)或公开显示任何
内容或其中的任何部分,但在
条款下明确允许的
除外。例如,以下是禁止的
:(i)创建服务器端
修改地图图块; (ii)
将多个静态地图图片
拼接在一起,以显示比Maps API中允许的值大
的地图
文档; (iii)根据
内容创建邮寄
清单或电话营销清单;或者(iv)出口,
写作,或者将内容保存到
第三方的基于位置的平台
或服务。


<$ p $ b> (b)不提供预取,缓存或存储内容。您不得

预取,缓存或存储任何
内容,除了您可能会存储:
(i)
的内容数量有限,用于改善您的Maps API实现的性能
,如果您
暂时,安全地以
方式不允许使用服务以外的
内容; (ii)任何内容标识符或关键字
,地图API文档
明确允许您存储。例如,对于
,您不得使用Content
创建一个独立的
位置数据库。

 < (c)没有大量下载内容或批量馈送内容。您不得以某种方式使用

服务,以使您或
有其他人访问以批量
下载或批量提供任何
内容,包括但不限于
数字纬度或经度
坐标,图像,可见地图
数据或地点数据(包括
商户列表)。例如,您不允许您
提供使用Maps API中包含的Content
的批量
地理编码服务。



I'm using the GoogleMaps API v3.0 and trying to save a DirectionsResult to my database and then retrieve it later to use on a map. My problem is that when I try to re-hydrate the saved object by pulling its JSON representation from my database, the object is just dumb JSON, it doesn't have the original methods and functions of its constituent objects. So, I built a fix routine that takes the dumbalt text JSON and rebuilds it by reconstructing all the LatLng and LatLngBound objects. But, something is still missing because my fixed object doesn't work like the original, the two points show up on my map but the purple line between them is missing.

Would appreciate any advice on either a better technique for serialization/hydration or any ideas as to what my fix routine might be missing.

Thanks

request = {
   origin: homeLocation, 
   destination: jobLocation,
   travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
   if (status == google.maps.DirectionsStatus.OK) {
      var str = Ext.encode(response);  //<<==SAVING RAW JSON OBJECT TO DB (I USE ExtJs)
      var z = eval('(' + str + ')');   //<<==REHYDRATING DirectionsResult RAW JSON OBJECT
      FixDirectionResult(z);           //<<==ATTEMPT TO RE-ESTABLISH ORIGINAL OBJECTS
      directionsRenderer.setDirections(z);  //<<==THIS WORKS WITH response BUT NOT WITH z
   }
);
function FixDirectionResult(rslt) {
 for(r=0; r<rslt.routes.length; r++) {
  var route = rslt.routes[r];
  var bounds = route.bounds;
  route.bounds = new google.maps.LatLngBounds(
   new google.maps.LatLng(bounds.U.b,bounds.O.d), 
   new google.maps.LatLng(bounds.U.d,bounds.O.b));

  for(l=0; l<route.legs.length;l++) {
   var leg = route.legs[l];
   leg.start_location = new google.maps.LatLng(leg.start_location.wa,leg.start_location.ya);
   leg.end_location = new google.maps.LatLng(leg.end_location.wa,leg.end_location.ya);

   for(s=0; s<leg.steps.length;s++) {
    var step = leg.steps[s];
    step.start_location = 
     new google.maps.LatLng(step.start_location.wa,step.start_location.ya);
    step.end_location = 
     new google.maps.LatLng(step.end_location.wa,step.end_location.ya);

    for(p=0;p<step.path.length;p++) {
     var path=step.path[p];
     step.path[p] = new google.maps.LatLng(step.path.wa,step.path.ya);
    }
   }
  }

  for(o=0; o<route.overview_path.length;o++) {
   var overview = route.overview_path[o];
   route.overview_path[o] = new google.maps.LatLng(overview.wa,overview.ya);
  }
 }
} 

解决方案

It seems from the look of your code you aren't accessing the lat and lng properly. The google maps api library is minified. The variable names are often shortened to a random set of characters. You shouldn't approach the x and y through these variables but through their getters: ie. lat() and lng() to avoid the same issue with future versions. Hopefully, this is the issue that's causing your direction to not render.

The proper recommended way to obtain lat and lng is similar to the following:

results[0].geometry.location.lat().toFixed(3);
results[0].geometry.location.lng().toFixed(3);


So, for instance this following line should be:

step.start_location = new google.maps.LatLng(step.start_location.wa,step.start_location.ya);
step.end_location = new google.maps.LatLng(step.end_location.wa,step.end_location.ya);

To:

step.start_location = new google.maps.LatLng(step.start_location.lat(), step.start_location.lng());
step.end_location = new google.maps.LatLng(step.end_location.lat(), step.end_location.lng());


Storage of Google Map Data is within term of service. Here is the restriction that you might want to take a look before you go further with your data storage:

 10.1.3 Restrictions against Data Export or Copying.

    (a) No Unauthorized Copying, Modification, Creation of Derivative

Works, or Display of the Content. You must not copy, translate, modify, or create a derivative work (including creating or contributing to a database) of, or publicly display any Content or any part thereof except as explicitly permitted under these Terms. For example, the following are prohibited: (i) creating server-side modification of map tiles; (ii) stitching multiple static map images together to display a map that is larger than permitted in the Maps APIs Documentation; (iii) creating mailing lists or telemarketing lists based on the Content; or (iv) exporting, writing, or saving the Content to a third party’s location-based platform or service.

    (b) No Pre-Fetching, Caching, or Storage of Content. You must not

pre-fetch, cache, or store any Content, except that you may store: (i) limited amounts of Content for the purpose of improving the performance of your Maps API Implementation if you do so temporarily, securely, and in a manner that does not permit use of the Content outside of the Service; and (ii) any content identifier or key that the Maps APIs Documentation specifically permits you to store. For example, you must not use the Content to create an independent database of "places."

    (c) No Mass Downloads or Bulk Feeds of Content. You must not use the

Service in a manner that gives you or any other person access to mass downloads or bulk feeds of any Content, including but not limited to numerical latitude or longitude coordinates, imagery, visible map data, or places data (including business listings). For example, you are not permitted to offer a batch geocoding service that uses Content contained in the Maps API(s).

这篇关于无法反序列化GoogleMaps DirectionsResult对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆