2016-03-28 11 views
0

私は、ポイントの一方または両方に直接道路がなく、その近くにある2つのポイントの間に運転ルートを作成しようとしています。Google最寄りの道路への道順

たとえば、DirectionsServiceを使用してMoab、UT、Zion National Park、UT間のドライブラインを作成しようとすると、CENTERへの道がないため、Zero_Resultsが返されます(緯度、Googleから返されるlng)。ザイオンネーションパーク。 google.com/mapsで同じことをすると、モアブからシオン国立公園の東口までのドライブラインと、公園の中心(ピンが配置されている場所)までのドライブラインが表示されます。彼らはどのようにしてシオン国立公園に向かうべきかを決めましたか?

+1

はStackOverflowのへようこそ!あなたの質問には、どのようなアプローチ/アルゴリズムを試してみましたか?ここの人々は助けてうれしいですが、必ずしも研究/コーディングをしたいとは限りません。あなたがすでに試みた研究/アプローチを示すなら、あなたは素晴らしい応答を得るでしょう。 – Castaglia

+0

私はコードをしたくありません。私はこれを試してみるためにトンを書いた。私はもっ​​とこれを行う方法のアイデアを探しています。私が知る限り、DirectionsServiceにドライブを作成する際に最も近い道路を見つけるようAPIに指示する方法はありません。つまり、カスタムソリューションが必要です。私はまともな半径で近くの砂浜を使ってみましたが、実際にどのエンドポイントがZERO_RESULTSを返すのかはわかりません。私は逃したリクエストオプションがあるか、誰かがこれについてどうやって行くかについての良い考えがあるかどうかを判断しようとしています。そのためのコードは必要ありません。 –

答えて

1

reverse geocodeザイオン国立公園(37.2982022、-113.0263005)のジオコーダによって返された座標は、最初の結果が道路上の最も近い場所になります。

proof of concept fiddle

コードスニペット:

var geocoder; 
 
var directionsService = new google.maps.DirectionsService(); 
 
var directionsDisplay = new google.maps.DirectionsRenderer(); 
 

 
function initialize() { 
 
    var map = new google.maps.Map(
 
    document.getElementById("map_canvas"), { 
 
     center: new google.maps.LatLng(37.4419, -122.1419), 
 
     zoom: 13, 
 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
 
    }); 
 
    var start = "Moab, UT"; 
 
    directionsDisplay.setMap(map); 
 
    geocodeAddress("Zion National Park", start); 
 
} 
 

 
function geocodeAddress(address, start) { 
 
    var geocoder = new google.maps.Geocoder(); 
 
    geocoder.geocode({ 
 
    'address': address 
 
    }, function(results, status) { 
 
    if (status === google.maps.GeocoderStatus.OK) { 
 
     geocoder.geocode({ 
 
     'location': results[0].geometry.location 
 
     }, function(results, status) { 
 
     if (status === google.maps.GeocoderStatus.OK) { 
 
      calculateAndDisplayRoute(start, results[0].geometry.location) 
 
     } else { 
 
      window.alert('Reverse Geocode failed due to: ' + status); 
 
     } 
 
     }); 
 
    } else { 
 
     alert('Geocode was not successful for the following reason: ' + status); 
 
    } 
 
    }); 
 
} 
 

 
function calculateAndDisplayRoute(start, end) { 
 
    directionsService.route({ 
 
    origin: start, 
 
    destination: end, 
 
    travelMode: google.maps.TravelMode.DRIVING 
 
    }, function(response, status) { 
 
    if (status === google.maps.DirectionsStatus.OK) { 
 
     directionsDisplay.setDirections(response); 
 
    } else { 
 
     window.alert('Directions request failed due to ' + status); 
 
    } 
 
    }); 
 
} 
 
google.maps.event.addDomListener(window, "load", initialize);
html, 
 
body, 
 
#map_canvas { 
 
    height: 100%; 
 
    width: 100%; 
 
    margin: 0px; 
 
    padding: 0px 
 
}
<script src="https://maps.googleapis.com/maps/api/js"></script> 
 
<div id="map_canvas"></div>

関連する問題