2010-12-29 3 views
11

私はGoogleマップAPI v3.0を使用しており、DirectionsResultをデータベースに保存してから、後でマップで使用するために取得しています。私の問題は、自分のデータベースからJSON表現を取り除いて保存されたオブジェクトを再水和しようとすると、そのオブジェクトは単にJSONであり、オリジナルのメソッドとその構成オブジェクトの機能がないということです。だから、私はdumbaltテキストJSONを取り、すべてのLatLngとLatLngBoundオブジェクトを再構築することで再構築する修正ルーチンを作成しました。しかし、私の固定オブジェクトは元のように動作しないので、何かがまだ欠けている、2つのポイントは私の地図上に表示されますが、それらの間の紫色の線が欠落しています。GoogleマップのDeserializeできませんDirectionsResultオブジェクト

シリアル化/ハイドレーションのためのより良いテクニック、または私の修正ルーチンが欠落している可能性のあるアイデアに関するアドバイスをいただければ幸いです。

おかげ

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

誰もが興味を持っていた場合、私もGoogleマップのフォーラムにこのクエリをも掲載しました:https://groups.google.com/d/topic/google-maps-js-api-v3/Ai1bZIVgfzo /討論 – sisdog

答えて

5

あなたがLATにアクセスし、適切にLNGされていないコードの見た目からと思われます。 Googleマップのapiライブラリは縮小されています。変数名は、多くの場合、ランダムな文字セットに短縮されます。 xとyには、これらの変数を介してではなく、ゲッタを通じてアプローチするべきです。 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); 

へ:

緯度とLNGを取得するために、適切な推奨される方法は、次のようになります。

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 Restrictions against Data Export or Copying. 

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

作品、またはディスプレイ:ここではあなたがあなたのデータストレージをさらに行く前に見てみたいかもしれない制限があります。あなたは は、コピー、翻訳、変更、または ( が作成または データベースへの貢献を含む)派生作品を作成する、または公に明示的にこれらの 規約の下で許可さ などを除く任意の コンテンツまたはその一部を表示してはいけません。例えば、以下は、 が禁止されているものです。(i)サーバー側のマップタイルの変更 の作成。 (ii) 複数の静的マップ画像を連結して をまとめて、 より大きいマップを表示するには、Maps APIで許可されているものよりも大きい ドキュメント。 (iii)内容 に基づいてメーリングリスト リストまたはテレマーケティングリストを作成する。 (iv) の書き込み、または サードパーティのロケーションベースプラットフォーム またはサービスへのコンテンツのエクスポート、またはエクスポートすること。

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

プリフェッチ、キャッシュ、またはあなたが保存することができることを除いて、任意の コンテンツを保存:あなたの場合は[マップのAPI実装のパフォーマンス を改善する 目的のコンテンツの (I)限られた量を サービスの外部にある のコンテンツの使用を許可しないで、一時的に、安全に、 の方法で行うこと。 (ii) のコンテンツ識別子またはキー。 のMaps APIドキュメントで特に保存することができます。 たとえば、 の独立したデータベースを作成するためにコンテンツ を使用してはならない「の場所を。」

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

サービスをあなたや に ダウンロードや大量のフィードを大量に他の人へのアクセスを与える方法で、任意の コンテンツ( 数値緯度または経度 座標、画像、表示可能なマップ データ、または場所データ( ビジネスリスティングを含む)を含むがこれに限定されないコンテンツ。たとえば、 は、Maps APIに含まれるコンテンツ を使用するジオコーディングサービスの バッチを提供することはできません。

1

私は自分自身を書いたので、私は上記のコードを動作させることができませんでした。次の2つの関数は、DirectionsResultオブジェクトのシリアライズと逆シリアル化を行います。ただし、ルートをプロットするのに必要な最小限のデータのみをシリアル化します。返された逆シリアル化されたDirectionsResultに必要な機能がない場合は、必要なものを追加するためにコードを変更する必要があります。DirectionsResult object attributes

このコードを悪用しないでください。 Googleでは、特定の状況下でのみ、一時的に(つまり、30 calendar daysよりも長くなることはない)地図データを保存することができます。

//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; 
} 
関連する問題