Javascript Can';t反序列化GoogleMaps DirectionsResult对象

Javascript Can';t反序列化GoogleMaps DirectionsResult对象,javascript,json,google-maps,serialization,Javascript,Json,Google Maps,Serialization,我正在使用GoogleMapsAPIv3.0,试图将DirectionsResult保存到我的数据库中,然后检索它以在地图上使用。我的问题是,当我试图通过从数据库中提取其JSON表示来重新水合保存的对象时,该对象只是哑JSON,它没有其组成对象的原始方法和函数。因此,我构建了一个修复例程,它接受dumbalt文本JSON,并通过重建所有LatLng和LatLngBound对象来重建它。但是,仍然缺少一些东西,因为我的固定对象与原始对象不一样,这两个点显示在我的地图上,但它们之间的紫色线丢失了 如

我正在使用GoogleMapsAPIv3.0,试图将DirectionsResult保存到我的数据库中,然后检索它以在地图上使用。我的问题是,当我试图通过从数据库中提取其JSON表示来重新水合保存的对象时,该对象只是哑JSON,它没有其组成对象的原始方法和函数。因此,我构建了一个修复例程,它接受dumbalt文本JSON,并通过重建所有LatLng和LatLngBound对象来重建它。但是,仍然缺少一些东西,因为我的固定对象与原始对象不一样,这两个点显示在我的地图上,但它们之间的紫色线丢失了

如果您有任何关于序列化/水合的更好技术的建议,或者关于我的修复例程可能缺少什么的想法,我将不胜感激

谢谢

请求={
产地:家乡,
目的地:工作地点,
travelMode:google.maps.Directions travelMode.DRIVING
};
路由(请求、功能(响应、状态){
if(status==google.maps.directionstatus.OK){

var str=Ext.encode(response);//从代码的外观来看,您似乎没有正确访问lat和lng。google maps api库已缩小。变量名通常被缩短为一组随机字符。您不应该通过这些变量接近x和y,而应该通过它们的getter:即
lat()
lng()
以避免未来版本出现同样的问题。希望这就是导致您的方向无法渲染的问题

获得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);
致:


谷歌地图数据的存储在服务期限内。在进一步使用数据存储之前,您可能需要查看以下限制:

作品或内容的显示。您 不得复制、翻译、修改或删除 创作衍生作品(包括 创建或促成一个项目 数据库),或公开显示任何 内容或其任何部分,除非 根据这些条款明确允许 术语。例如,以下是 禁止:(i)创建服务器端 修改地图分幅;(ii) 拼接多幅静态地图图像 一起显示一个 大于Maps API中允许的值 文件;(三)创建邮件 列表或电话营销列表基于 内容;或(iv)出口, 编写或将内容保存到 第三方基于位置的平台 或服务

    (b) No Pre-Fetching, Caching, or Storage of Content. You must not
预取、缓存或存储任何 内容,但您可以存储的内容除外: (i) 网站的内容数量有限 提高绩效的目的 如果您 以临时、安全和安全的方式执行此操作 不允许使用 服务之外的内容;以及 (ii)任何内容标识符或密钥 映射API文档 特别允许您存储。对于 例如,您不能使用内容 创建一个独立的 “地方。”

    (c) No Mass Downloads or Bulk Feeds of Content. You must not use the
以一种能给你或 任何其他人进入弥撒室 下载或批量订阅任何 内容,包括但不限于 数字纬度或经度 坐标、图像、可见地图 数据,或放置数据(包括 例如,您 不允许提供一批 使用内容的地理编码服务 包含在地图API中


我无法使上述代码正常工作,因此我编写了自己的代码。以下两个函数将序列化和反序列化DirectionResult对象。但是,它将仅序列化绘制路由所需的最小数据量。如果发现返回的反序列化DirectionResult缺少所需的功能,则必须修改代码添加您需要的任何其他内容

请不要滥用此代码。谷歌只允许您在特定情况下存储地图数据,并且只允许临时存储(即永远不会超过)

//将Google Maps API v3 DirectionRequest和DirectionResult对象作为输入。
//返回序列化的DirectionResult字符串。
函数序列化directionsResult(directionsRequest、directionsResult){
var copyright=directionsResult.routes[0]。版权;
var travelMode=directionsRequest.travelMode;
var startat=directionsResult.routes[0]。支腿[0]。起始位置。lat();
var starting=directionsResult.routes[0]。legs[0]。start_location.lng();
var endLat=directionsResult.routes[0]。legs[0]。end_location.lat();
var endLng=directionsResult.routes[0]。支腿[0]。end_location.lng();
var步骤=[];
对于(var i=0;i 10.1.3 Restrictions against Data Export or Copying.

    (a) No Unauthorized Copying, Modification, Creation of Derivative
    (b) No Pre-Fetching, Caching, or Storage of Content. You must not
    (c) No Mass Downloads or Bulk Feeds of Content. You must not use the
//Takes Google Maps API v3 directionsRequest and directionsResult objects as input.
//Returns serialized directionsResult string.
function serializeDirectionsResult (directionsRequest, directionsResult) {
        var copyright = directionsResult.routes[0].copyrights;
        var travelMode = directionsRequest.travelMode;
        var startLat = directionsResult.routes[0].legs[0].start_location.lat();
        var startLng = directionsResult.routes[0].legs[0].start_location.lng();
        var endLat = directionsResult.routes[0].legs[0].end_location.lat();
        var endLng = directionsResult.routes[0].legs[0].end_location.lng();
        var steps = [];
        for (var i = 0; i < directionsResult.routes[0].legs[0].steps.length; i++){
                var pathLatLngs = [];
                for (var c = 0; c < directionsResult.routes[0].legs[0].steps[i].path.length; c++){
                        var lat = directionsResult.routes[0].legs[0].steps[i].path[c].lat();
                        var lng = directionsResult.routes[0].legs[0].steps[i].path[c].lng();
                        pathLatLngs.push( { "lat":lat , "lng":lng }  );
                }
                steps.push( pathLatLngs );
        }
        var serialSteps = JSON.stringify(steps);
        //Return custom serialized directions result object.
        return copyright + "`" + travelMode + "`" + startLat + "`" + startLng + "`" + endLat + "`" + endLng + "`" + serialSteps;
}

//Takes serialized directionResult object string as input.
//Returns directionResult object.
function deserializeDirectionsResult (serializedResult) {
        var serialArray = serializedResult.split("`");
        const travMode = serialArray[1];
        var directionsRequest = {
            travelMode: travMode,
            origin: new google.maps.LatLng(serialArray[2], serialArray[3]),
            destination: new google.maps.LatLng(serialArray[4], serialArray[5]),
        };
        var directionsResult = {};
        directionsResult.request = directionsRequest;
        directionsResult.routes = [];
        directionsResult.routes[0] = {};
        directionsResult.routes[0].copyrights = serialArray[0];
        directionsResult.routes[0].legs = [];
        directionsResult.routes[0].legs[0] = {};
        directionsResult.routes[0].legs[0].start_location = directionsRequest.origin;
        directionsResult.routes[0].legs[0].end_location = directionsRequest.destination;
        directionsResult.routes[0].legs[0].steps = [];
        var deserializedSteps = JSON.parse(serialArray[6]);
        for (var i = 0; i < deserializedSteps.length; i++){
                var dirStep = {};
                dirStep.path = [];
                for (var c = 0; c < deserializedSteps[i].length; c++){
                        var lat = deserializedSteps[i][c].lat;
                        var lng = deserializedSteps[i][c].lng;
                        var theLatLng = new google.maps.LatLng(lat, lng);
                        dirStep.path.push( theLatLng );
                }
                dirStep.travel_mode = travMode;
                directionsResult.routes[0].legs[0].steps.push( dirStep );
        }
        return directionsResult;
}
function deserializeDirectionsResult(directions) {
    directions.routes.forEach(function (route) {
        route.legs.forEach(function (leg) {
            leg.end_location = new google.maps.LatLng(leg.end_location.lat, leg.end_location.lng)
            leg.start_location = new google.maps.LatLng(leg.start_location.lat, leg.start_location.lng)

            leg.steps.forEach(function (step) {
                step.end_location = new google.maps.LatLng(step.end_location.lat, step.end_location.lng);
                step.end_point = new google.maps.LatLng(step.end_point.lat, step.end_point.lng);
                step.start_location = new google.maps.LatLng(step.start_location.lat, step.start_location.lng);
                step.start_point = new google.maps.LatLng(step.start_point.lat, step.start_point.lng);

                step.lat_lngs.forEach(function (lat_lng) {
                    lat_lng = new google.maps.LatLng(lat_lng.lat, lat_lng.lng);
                });

                // Optional property
                if (step.hasOwnProperty('steps')) { 
                    step.steps.forEach(function (stepStep) {
                        stepStep.end_location = new google.maps.LatLng(stepStep.end_location.lat, stepStep.end_location.lng);
                        stepStep.start_location = new google.maps.LatLng(stepStep.start_location.lat, stepStep.start_location.lng);

                        stepStep.lat_lngs.forEach(function (lat_lng) {
                            lat_lng = new google.maps.LatLng(lat_lng.lat, lat_lng.lng);
                        });

                        stepStep.path.forEach(function (path_item) {
                            path_item = new google.maps.LatLng(stepStep.path.lat, stepStep.path.lng);
                        });
                    });
                }
            });
        });
    });

    return directions;   
}
function deserializeAnything(previouslyStringified) {
    return _.cloneDeepWith(previouslyStringified, function (value) {
        if (value.lat !== undefined && value.lng !== undefined) {
            return new google.maps.LatLng(value.lat, value.lng);
        }
    });
}