Tôi đang sử dụng API GoogleMaps v3.0 và cố gắng lưu DirectionsResult vào cơ sở dữ liệu của mình rồi truy xuất lại sau để sử dụng trên bản đồ. Vấn đề của tôi là khi tôi cố gắng hydrat hóa đối tượng đã lưu bằng cách kéo biểu diễn JSON của nó từ cơ sở dữ liệu của tôi, đối tượng chỉ là JSON ngu ngốc, nó không có các phương thức và chức năng ban đầu của các đối tượng cấu thành của nó. Vì vậy, tôi đã xây dựng một thói quen sửa lỗi lấy văn bản dumbalt JSON và xây dựng lại nó bằng cách xây dựng lại tất cả các đối tượng LatLng và LatLngBound. Nhưng, một cái gì đó vẫn còn thiếu bởi vì đối tượng cố định của tôi không hoạt động như bản gốc, hai điểm hiển thị trên bản đồ của tôi nhưng đường màu tím giữa chúng bị thiếu.Không thể Deserialize GoogleMaps DirectionsResult Object
Sẽ đánh giá cao bất kỳ lời khuyên nào về kỹ thuật tuần tự hóa/hydrat hóa tốt hơn hoặc bất kỳ ý tưởng nào về việc thường xuyên sửa lỗi của tôi.
Cảm ơn
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);
}
}
}
Nếu có ai quan tâm, tôi cũng đã đăng truy vấn này lên diễn đàn bản đồ google: https://groups.google.com/d/topic/google-maps-js-api-v3/Ai1bZIVgfzo /thảo luận – sisdog