2017-10-26 5 views
-1

.csvのデータを使用してアニメーションのGoogleマップを作成しようとしています。パスは時間の経過とともに移動する必要があります。また、テキストボックスに入力された時間までのパスを見つける必要があります。だから私の現在のコードでは、テキストボックスに期間を入力するので、テキストボックスに入力された特定の期間値までのパスではなく、フルパスを取得します。私はこれらのリンクを使ってコードを作った:https://github.com/duncancumming/maps/blob/master/animatedPaths/animated%20csv.htmlAnimation of Google Maps Polyline with respect to time increase.csvを使用して時間に関してアニメーション化されたGoogleマップを生成できません。

以下は私のコードです。 Plsはどこに間違っているのか教えてくれます。ありがとう、事前にたくさん!おそらくdrawline()の "timetill"の値は未定義になっていますが、わかりません。

<!DOCTYPE html> 
<html> 
<head> 
<title>Animated path via a csv file</title> 
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
<meta charset="utf-8"> 
<style> 
html, body, #map, #wrapper { 
    height: 100%; 
    margin: 0px; 
    padding: 0px; 
    position: relative; 
} 

#over_map { 
position: absolute; 
top: 10px; 
left: 40%; 
z-index: 99; 
} 


</style> 
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> 
<script type="text/javascript" 
src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"> 
</script> 
<script> 
    var allCoords = []; 
    var map, route, marker; 

    function getCoords() { 
     $.ajax({ 
      url: 'subject1.csv', 
      dataType: 'text', 
      success: function(data) { 
       var lines = data.split(/[\r\n]+/); 

       lines.forEach(function(line){ 
        allCoords.push(line.split(',')); 
       }); 
       var bounds = new google.maps.LatLngBounds(); 
       allCoords.forEach(function(coords){ 
       bounds.extend(new google.maps.LatLng(coords[0], coords[1])); 
       }); 

       map.fitBounds(bounds); 

       drawLine(); 
      } 
     }); 
    } 

    function drawLine(timetill) { 
     console.log(timetill) 
     route = new google.maps.Polyline({ 
      path: [], 
      geodesic : true, 
      strokeColor: '#FF0000', 
      strokeOpacity: 0.7, 
      strokeWeight: 2, 
      editable: false, 
      map:map 
     }); 

     marker = new google.maps.Marker({ 
      map: map, 
      icon: "http://maps.google.com/mapfiles/ms/micons/blue.png" 
     }); 

     for (var i = 0; i < allCoords.length; i++) { 
      if(timetill!=undefined && timetill!="" && 
    timetill<allCoords[i].time){ 
      break; 
      } 
      window.setTimeout(updatePath, 50 * i, allCoords[i]); 
     } 
    } 
    function updatePath(coords) { 
     console.log(coords); 
     var latLng = new google.maps.LatLng(coords[0], coords[1]); 
     route.getPath().push(latLng); 
     marker.setPosition(latLng); 
    } 

    function initialize() { 
     map = new google.maps.Map(document.getElementById("map"), { 
      center: {lat: 30.6010548, lng: -96.3534677}, 
      zoom: 24, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 

     getCoords(); 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 

    function plotTill(value){ 
     console.log(value) 
    route.setMap(null) 
    marker.setMap(null); 
    drawLine(value) 
    } 


    </script> 

    </head> 
    <body> 

    <div id="wrapper"> 
    <div id="map"></div> 

    <div id="over_map"> 
    <input type="text" placeholder="Enter second ex: 2" id="search_box" 
    onchange="plotTill(this.value)" /> 
    </div> 
    </div> 

    </body> 
    </html> 

マイ.csvファイルは、ローカルマシンに保存されており、これが私の.csvファイルのlooks-がどのようである:3列目は時間のためです。

30.6011525,-96.3546702,1 
30.6011525,-96.3546703,2 
30.6011525,-96.3546703,3 
30.6011525,-96.3546703,4 
30.6011525,-96.3546703,5 
30.6011525,-96.3546703,6 
30.6011525,-96.3546703,7 
30.6011525,-96.3546703,8 
30.6011525,-96.3546703,9 
30.6011525,-96.3546703,10 
30.6011525,-96.3546703,11 
+0

@krishnarあなたはこれを見て、私に知らせてください。 –

+0

私の解決策を見てください – krishnar

+0

@krishnarあなたの助けをありがとう、あなたのソリューションは素晴らしい作品!また、私は2つの疑問を持っています:(a)コードから何が欠けているか教えてください。それは時間のコラムですか? (b)なぜ、このコードまたはハードコードされた値を持つものがMAMPまたはXAMPで実行されないのですか?(一度localhost:8888 // foldername/filename.htmlを指定すると) –

答えて

0

大丈夫ですが、ここでは私があなたが望んでいるものとほとんど同じだと思う実例があります。私は5ミリ秒にラインの各更新の間の時間を減らしました。

function getCoords() { 
     $.ajax({ 
      url: 'toyotav2.csv', 
      dataType: 'text', 
      success: function(data) { 
       var lines = data.split(/[\r\n]+/); 

       lines.forEach(function(line){ 
        allCoords.push(line.split(',')); 
       }); 

       setUpLine(); 
      } 
     }); 
    } 

    function setUpLine() { 
     route = new google.maps.Polyline({ 
      path: [], 
      geodesic : true, 
      strokeColor: '#FF0000', 
      strokeOpacity: 0.7, 
      strokeWeight: 2, 
      editable: false, 
      map:map 
     }); 

     marker = new google.maps.Marker({ 
      map: map, 
      position: {lat: 0, lng: 0}, 
      visible: false, 
      icon: "https://maps.google.com/mapfiles/ms/micons/blue.png" 
     }); 
    } 

    function drawLine(timetill) {  
     for (var i = 0; i < allCoords.length; i++) { 
      if (timetill < allCoords[i][2]){ 
       break; 
      } 
      window.setTimeout(updatePath, 5 * i, allCoords[i]); 
     } 
    } 

    function updatePath(coords) { 
     var latLng = new google.maps.LatLng(coords[0], coords[1]); 
     route.getPath().push(latLng); 
     marker.setPosition(latLng); 
    } 

    function initialize() { 
     map = new google.maps.Map(document.getElementById("map"), { 
      center: {lat: 30.6010548, lng: -96.3534677}, 
      zoom: 18, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }); 

     getCoords(); 
    } 
    google.maps.event.addDomListener(window, 'load', initialize); 

    function plotTill(value) { 
     route.setPath([]); 
     marker.setVisible(true); 
     drawLine(parseInt(value, 10)); 
    } 

いくつかの重要な違い:

あなたはallCoords[i].timeに言及しないが、どこにもtimeは座標allCoordsの2次元アレイ上に作成されますと呼ばれるプロパティをしています。その時の値を参照するために、あなただけの3番目の要素を取得するためにallCoords[i][2]を指定する必要があります...

[ 
    [30.6011525,-96.3546702,1], 
    [30.6011525,-96.3546703,2], 
] 

:あなたが持っていることのように見えるの配列です。

マーカーの作成時にpositionを指定していませんでした。私はちょうど{0,0}にそれを設定しましたが、またvisibleプロパティをfalseに指定しましたが、行の描画を開始するときにvisibleに設定しました。

入力フィールドから値を読み取ると、テキスト値として取得されます。私はparseInt()を使って、それが整数に変換されていることを確認します。そうでなければ、 "16"のような値がtimetill<allCoords[i].timeのような比較のために使われるリスクがあります。従って"16" < 2であり、一方、16 > 2である。 parseIntを使用して、文字列ではなく数値を取得していることを確認します。

マーカとポリラインのマップをnullに設定する代わりに、パスをクリアするだけです。

また、drawLine関数を分割して、マーカーとポリラインをajaxレスポンスの直後に作成するようにしましたが、ユーザー入力に応じて線の描画を開始するだけです。

関連する問題