タグ:Google Maps
<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=【取得したAPIキー】&sensor=false&v=3&language=js" charset="utf-8"></script>今回はGPSなどの位置センサーを使わないので、scnsorにはfalseを指定。 v=3はAPIのバージョン、言語(language)は日本語(ja)とします
<div id="map_canvas"></div> <div id="root_panel"></div>
html { height: 100%; }
body { height: 100%; margin:0; padding:0; }
#map_canvas { width: 70%; height:100%; float:left; }
#root_panel { width: 30%; height:100%; float:right; overflow: auto; }
var mayMap;
// マップの初期設定
function initialize() {
// Mapクラスのインスタンスを作成(緯度経度は池袋駅に設定)
var initPos = new google.maps.LatLng(0, 0);
// 地図のプロパティを設定(倍率、マーカー表示位置、地図の種類)
var myOptions = {
center: initPos,
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// #map_canva要素にMapクラスの新しいインスタンスを作成
myMap = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// 経由ルートと各条件を設定
var request = {
origin: "池袋駅", // 出発点
destination: "サンシャインシティ", // 目的地
travelMode: google.maps.DirectionsTravelMode.WALKING, // 交通手段の設定
waypoints: [{location: "コミックとらのあな 池袋店"}, {location: "アニメイトカフェ池袋店"}] // 経由地点
};
// DirectionsServiceオブジェクトを使用
var directions = new google.maps.DirectionsService();
directions.route(request, result_direction);
}
// 検索結果を出力
function result_direction(result, status) {
if(status == google.maps.DirectionsStatus.OK) {
var direvtionsPosition = new google.maps.DirectionsRenderer({ map: myMap });
direvtionsPosition.setDirections(result);
direvtionsPosition.setPanel(document.getElementById("root_panel"));
}
}
// ページ読み込み完了後、Googleマップを表示
google.maps.event.addDomListener(window, 'load', initialize);
| 種類 | 交通手段 |
|---|---|
| google.maps.DirectionsTravelMode.DRIVING | 車でのルート |
| google.maps.DirectionsTravelMode.BICYCLING | 自転車でのルート |
| google.maps.DirectionsTravelMode.TRANSIT | 公共機関を利用したルート |
| google.maps.DirectionsTravelMode.WALKING | 徒歩ルート |
{location: "○○公園"},
{location: "ローソン ○○店"}
また、路地裏にあるポイントなどを設定した場合にマーカーが付き、そのポイントまで到着させてから次のポイントへ目指すようなルートとなりますが
stopoverをfalseに指定することで、単に通り過ぎるだけで立ち寄ることのない経由地となり、この場合はマーカーは表示されません
{location: "○○公園", stopover: false}
