2012-01-03 10 views
0

走行距離表示。ルートXを原点からのキロメートル

ユーザーが場所と距離を入力します。

ユーザが入力した距離の半径と、ユーザの位置を中心点として、円を重ねることができます。

ユーザーが設定した距離で、原点を中心に4つの基点(N、S、E、W)を設定して、それらの点のルートを描くことができるので、点Bは点Aから100KMです。マップされたルートは、例えば、道路に沿って145kmです。

道路沿線を正確に100km表示できますか?

進捗状況を更新するように編集しました。

答えて

0

最後にこれを解決し、分かち合うと思いました。

したがって、ユーザーは場所と距離を指定します。我々は100Kmと言うでしょう。

コードは、原点の基点100Km N、S、E、Wを見つけ、各点までのルートを解決します。ルートの解決が成功した場合、結果には原点から目的地までの点の配列が含まれます。

 directionsService.route({ 
      origin: start, 
      destination: end, 
      travelMode: google.maps.DirectionsTravelMode.DRIVING 
      }, function(result) { 
      renderDirections(result); 
      }); 

    //don't use the google api DirectionsRender() to draw the route. 
    //instead - result holds an array of lat/long points that make up the entire route. Lets say it's latlong[123] 
    //iterate through that array, getting a distance from point A to latlong[0], A to latlong[1], etc... 
    //when that distance is >= user supplied distance, STOP, and draw a polyline from point A through the latlong points in the array latlong[78] 

function computeTotalDistance(result) { 
    var total = 0; 
    var myroute = result.routes[0]; 
if(myroute) 
{ 
//create a LatLon from the Starting point 
    var objGeo = new LatLon(Geo.parseDMS(myroute.overview_path[0].Qa), Geo.parseDMS(myroute.overview_path[0].Ra)); 

    //call get distance from the starting point to each other point in the array 
    //each subsequent point should be a longer distance 
    var arrPointsToDraw =[]; 
    for(var i = 1; i<=myroute.overview_path.length-1;i++) 
    { 
     try 
     { 
      var objGeo2 = new LatLon(Geo.parseDMS(myroute.overview_path[i].Qa), Geo.parseDMS(myroute.overview_path[i].Ra)); 
     } 
     catch(err) 
     { 
      alert(err.description); 
     } 
     //here, total = kilometers 
     total = objGeo.distanceTo(objGeo2,3); 

     //add coordinates to our array of points that we are going to draw 
     arrPointsToDraw.push(new google.maps.LatLng(objGeo2._lat, objGeo2._lon)); 

      if(parseInt(total) > parseInt(distance.value)) 
      { 
       arrPointsToDraw.pop();//remove the last element of the array 
       break; 
      } 
    } 

     //at this point, arrPointsToDraw[] contains the lat/long points that are closest to our distance 
     //without going over 
     lines[lines.length] = new google.maps.Polyline({ 
         path: arrPointsToDraw, 
         strokeColor: '#1589FF', 
         strokeOpacity: 1.0, 
         strokeWeight: 3 
        }); 

     lines[lines.length-1].setMap(map); 
    }//end if(myRoute) 


} 

このコードは、関数の2つの素晴らしいコレクションの使用はhere

を見つけます
関連する問題