gpt4 book ai didi

javascript - 无法反序列化 GoogleMaps DirectionsResult 对象

转载 作者:可可西里 更新时间:2023-11-01 02:25:23 32 4
gpt4 key购买 nike

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

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

谢谢

alt text alt text

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);
}
}
}

最佳答案

从代码的外观来看,您似乎没有正确访问纬度和经度。缩小了 google maps api 库。变量名称通常被缩短为一组随机字符。你不应该通过这些变量来接近 x 和 y,而是通过它们的 setter/getter :即。 lat()lng() 以避免 future 版本出现同样的问题。希望这是导致您的方向无法呈现的问题。

获取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 map 数据的存储在服务期限内。在进一步处理数据存储之前,您可能需要了解以下限制:

 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).

关于javascript - 无法反序列化 GoogleMaps DirectionsResult 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4556602/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com