2つの場所間のルートと距離を示すGoogleマップ
-
22-07-2019 - |
質問
Googleマップを使用している人々の位置を表示するプロファイルWebサイトで作業しています。
Googleマップを実装しました。現在、表示している人がどこに住んでいて、どこに住んでいるかが表示されます。
コードはこちら:
var map = null;
var geocoder = null;
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
map.addControl(new GLargeMapControl());
map.addControl(new GScaleControl());
map.addControl(new GMapTypeControl());
geocoder = new GClientGeocoder();
}
}
function showAddresses(address1,address2) {
if (geocoder) {
geocoder.getLatLng(
address1,
function(point) {
if (!point) {
alert(address1 + " not found");
} else {
map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
marker.openInfoWindowHtml(address1);
}
}
);
geocoder.getLatLng(
address2,
function(point) {
if (!point) {
alert(address2 + " not found");
} else {
map.setCenter(point, 13);
var marker = new GMarker(point);
map.addOverlay(marker);
}
}
);
}
}
ただし、2つの場所が離れており、マップ全体に収まらない場合は、ズームレベルを変更します。修正方法がわかりません。
次のステップは、マップに2つのポイント間の視覚的なルートとそのルートをたどる際の距離を表示させたいということです。Googleマップサイトで試してみたところ、この機能があることがわかりました。実装方法に関するドキュメントが見つかりません。
または、Googleマップに移動し、準備されたページを提供するハイパーリンクを作成する方が良いでしょうか?それも私にはわからない。
解決
やったことはないが、APIでGDirectionsを見た:
http://code.google.com/apis/maps /documentation/reference.html#GDirections
探しているもののように見えます。
他のヒント
すべてのポイントをポリゴンに追加します。ポリゴンからLatLongBoundsを取得します。これを使用して、ズームレベルを導出できます。
private function setMapZoomLevelBasedOnPlottedPoints(polygonPoints:Array):void
{
var pointBounds:LatLngBounds = getLatLongBounds(polygonPoints);
_map.setCenter(pointBounds.getCenter());
_map.setZoom(_map.getBoundsZoomLevel(pointBounds) - 1);
}
private function getLatLongBounds(polygonPoints:Array):LatLngBounds
{
for(var i:Number; i < polygonPoints; i++)
{
polygonPoints.push(polygonPoints[i] as LatLong);
}
var polygon:IPolygon = new Polygon(polygonPoints);
return polygon.getLatLngBounds();
}
APIにあります。
で見つけることができますroute site:http://code.google.com/apis/maps
たとえば、「運転手の道順」を示す1つのページがあります。 Flashのオーバーレイで。フラッシュ外でマップを実行する場合、APIは少し異なりますが、基本コードは同じです。 http://code.google.com/apis/maps/documentation/ flash / services.html
MapQuestでより多くの作業を行ったため、Google Maps APIを使用しています。