• ベストアンサー

Google maps API V3 ルート検索

Google maps API V3 のルート検索で、出発地と目的地のマーカー(アイコン)を独自のものにするには、どのように記述すればよろしいでしょうか? デフォルトの設定では、出発地が“A”、目的地が“B”となっています。 以下、テストサンプルです。 http://itohiki119.iinaa.net/bousai/test.html よろしくお願い致します。

noname#245585
noname#245585

質問者が選んだベストアンサー

  • ベストアンサー
  • my--
  • ベストアンサー率89% (91/102)
回答No.3

>>No.2 お礼 ※関数の外(グローバルコード)に記述 // ラインオブジェクト var lineObj = new google.maps.Polyline({ strokeColor: '#00FF00', strokeOpacity: 0.5, strokeWeight: 5 }); // 出発地マーカーオブジェクト var startMarker = new google.maps.Marker({ icon: 'http://itohiki119.iinaa.net/bousai/icon/F_red_kage.png' }); // 目的地マーカーオブジェクト var endMarker = new google.maps.Marker({ icon: 'http://maps.google.co.jp/mapfiles/ms/icons/firedept.png' }); function func(response) { var routes = response.routes[0]; lineObj.setPath(routes.overview_path); lineObj.setMap(map); var legs = routes.legs[0]; startMarker.setPosition(legs.start_location); startMarker.setMap(map); endMarker.setPosition(legs.end_location); endMarker.setMap(map); map.fitBounds(routes.bounds); // 追記(自動的に...) } // 終了タグ直前にでも置く </script>

noname#245585
質問者

お礼

出来ました! \(^o^)/ ありがとうございました。m(_ _)m 感謝!感謝!です。m(_ _)m

その他の回答 (2)

  • my--
  • ベストアンサー率89% (91/102)
回答No.2

>>No.1 お礼 // ラインオブジェクト var lineObj = new google.maps.Polyline({ strokeColor: '#000', strokeOpacity: 0.5, strokeWeight: 5 }); // 出発地マーカーオブジェクト var startMarker = new google.maps.Marker({ icon: 'start.png' }); // 目的地マーカーオブジェクト var endMarker = new google.maps.Marker({ icon: 'end.png' }); function func(response) { var routes = response.routes[0]; lineObj.setPath(routes.overview_path); lineObj.setMap(map); var legs = routes.legs[0]; startMarker.setPosition(legs.start_location); startMarker.setMap(map); endMarker.setPosition(legs.end_location); endMarker.setMap(map); } ... directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { func(response); } }); ...

noname#245585
質問者

お礼

早速のご回答ありがとうございます。 修正したのですが、やはり連続して検索すると(住所を入力して、まず最初に検索した後、“車”を“歩き”にしたり、支部を変更すると…)ラインが重なってしまいます。 私の記述がおかしいのかもしれません。 それと、修正前は検索結果が画面の大きさに合わせて自動的に拡大、縮小できていたものが、出来なくなっています。 本当に厚かましいお願いで申し訳ないですが、マップの記述を下記にアップしましたので、修正箇所を示して頂けないでしょうか。 http://itohiki119.iinaa.net/bousai/test.txt あとすこしのところで大きな壁にぶち当たってしまっています。 よろしくお願い致します。m(_ _)m

noname#245585
質問者

補足

すみません。m(_ _)m どうもアップ出来ていないようです。 こちらでどうでしょうか。 http://itohiki119.iinaa.net/bousai/test03.js よろしくお願い致します。

  • my--
  • ベストアンサー率89% (91/102)
回答No.1

... directionsService.route(request, function(response, status) { if (status == google.maps.DirectionsStatus.OK) { //directionsDisplay.setDirections(response); var routes = response.routes[0]; // ラインを引く new google.maps.Polyline({ map: map, path: routes.overview_path, // ルート座標 strokeColor: '#000', strokeOpacity: 0.5, strokeWeight: 5 }); var legs = routes.legs[0]; // 出発地マーカー new google.maps.Marker({ map: map, position: legs.start_location, // 出発地座標 icon: 'start.png' // 画像パス }); // 目的地マーカー new google.maps.Marker({ map: map, position: legs.end_location, // 目的地座標 icon: 'end.png' // 画像パス }); } }); ... google.maps.PolylineOptions object specification https://developers.google.com/maps/documentation/javascript/reference?hl=ja#PolylineOptions google.maps.MarkerOptions object specification https://developers.google.com/maps/documentation/javascript/reference?hl=ja#MarkerOptions

noname#245585
質問者

お礼

ご回答ありがとうございます。 早速教えて頂いたものを追記したところ、見事に出来ました! しかしながら、新たな問題が・・・ ルート検索を連続で行なった場合、最初のルートが消えずに新たにラインやアイコンが現れてしまいます。 恐れ入りますが、この件についての追記はどうすればいいでしょうか。 すみません。よろしくお願い致します。

関連するQ&A

  • google maps api v3のルート検索

    google maps api v3のルート検索でA,Bマーカーでなく、 オリジナルのマーカーにてドラッグ&ドロップマウスで移動可能なルート検索を javascriptで作るやり方が知りたいです。

    • ベストアンサー
    • AJAX
  • Google maps API V3 検索ボックス

    Google maps API V3 で、kmlファイルを利用した地図(http://itohiki119.iinaa.net/bousai/test.html) を作成しました。 この地図に検索ボックスを追記したいのですが、上手くいきません。 お知恵をお貸し下さい。m(_ _)m 一応、http://code.google.com/p/gmaps-api-v3-googlebar/downloads/list からダウンロード (1) gmaps-api-v3-googlebarから入手したファイルを解凍。 (2) その中のjGoogleBarV3.jsとdefault.cssを適当な位置に配置。 (3) scriptとlinkを追加。 してみたのですが、ダメでした。 もともとの、htmlは、下記の通りです。 _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ <html lang="ja"> <head> <meta charset="UTF-8"> <title>避難所マップ</title> <meta name="viewport" content="initial-scale=1.0, user-scalable=yes" /> <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=true&language=ja"></script> <script type="text/javascript"> function initialize() { var initPos = new google.maps.LatLng(34.794267, 134.717400); var myOptions = { zoom : 15, center : initPos, mapTypeId : google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); var kmlOptions = {preserveViewport: true} var ctaLayer = new google.maps.KmlLayer('http://itohiki119.iinaa.net/bousai/test.kml', kmlOptions); ctaLayer.setMap(map); } </script> </head> <body onload="initialize()"> <div id="map_canvas" style="width: 100%; height: 100%;"><br /></div> </body> </html> _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_ よろしくお願い致します。

  • Google Maps APIのfitBounds

    Google Maps APIのfitBoundsメソッドを以下のページを参考に導入してみました。 http://ma-bank.com/item/1415 上記のページのスクリプトはマーカーがオリジナルのものになっており、これをGoogle Mapのデフォルトのマーカーにしたいのですが、私はJavaScriptとGoogle Maps APIのいずれも初心者であり、その方法が分かりません。 ご教示願えませんでしょうか? よろしくお願いします。

  • Google Maps API V3

    Google Maps API V3 でXMLのデータがかなり多いので XMLから取り出すデータをgoogleマップで表示するデータのみ 取り出してマーカを表示したい場合どのようにすればいいのでしょうか

    • ベストアンサー
    • AJAX
  • Google maps API

    Google maps APIを使ってみたいと思っています。Googleのページを見ましたが英語で書いてあって良くわかりません。サンプルみたいなのも書いてありましたが良くわかりませんでした。 日本語で書いてあってわかりやすくしてあって説明してあるページとかを知っている方がいらっしゃいましたらご教授下さい。よろしくです。 _(_ _)_

  • Google Maps API ルート検索

    Google maps API V3でルート検索をする場合、 <input type="submit" value="検索" onclick="searchRoute()"> として、検索ボタンを押さないとルートが表示されないようにしたいのですが、検索する住所が ”ある市”(市町村)に限られている場合、あらかじめ検索窓に”○○市”と表示させておき、”○○市”の後に町名や番地を入力して、それから検索ボタンを押すことで初めて地図上にルートを表示させたいのですが、記述をどうすればよいか悩んでおります。 <input type="text" size="20" id="toAddress" name="to" value="○○市"> とすると、いきなり”○○市”の代表地点までのルートが表示されてしまいます。 よろしくお願い致します。

  • google maps api ルート線変更

    Google Maps JavaScript API v3 でルート検索で表示されるポリラインの色や太さを変更したいのですが行き詰ってます。 ネットで調べてますと、polylineOptionのstrokeColorやstrokeWeightなどで変更できるような事は書いてあるのですが、どうやってもうまくいきません。 Googleデベロッパーで紹介されています下記のルートサービスのひな形をもとにいろいろやっているのですが、どこにどのように記述すればいいのでしょうか。 ------------------------------------------------ var directionsDisplay; var directionsService = new google.maps.DirectionsService(); var map; function initialize() { directionsDisplay = new google.maps.DirectionsRenderer(); var chicago = new google.maps.LatLng(41.850033, -87.6500523); var mapOptions = { zoom:7, mapTypeId: google.maps.MapTypeId.ROADMAP, center: chicago } map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); directionsDisplay.setMap(map); } function calcRoute() { var start = document.getElementById("start").value; var end = document.getElementById("end").value; var request = { origin:start, destination:end, travelMode: google.maps.TravelMode.DRIVING }; directionsService.route(request, function(result, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(result); } }); } --------------------------------------------------

  • google maps apiについて

    現在HPをリニューアルしています。 旧サイトと新サイトは同じサーバー、同じドメインを使用しています。 旧サイトのデータはFTP内にはありません。 所在地マップに、GoogleMapを使用しています。 そこで、下記のようなエラーが出ます。 このウェブサイトには別の Google Maps API キーが必要です。新しいキーは http://code.google.com/apis/maps/signup.html で作成できます。 Google Maps API サーバーでリクエストが拒否されました。リクエスト内で指定した「sensor」パラメータには「true」または「false」のいずれかを指定する必要があります。 次に上記で指定されている http://code.google.com/apis/maps/signup.html にて、URLを指定して、APIを生成ボタンを押す 次にJavaScript Maps API の例で表示された <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true_or_false&amp;key=*********************************" type="text/javascript"></script> をエラーが表示されるページに貼り付ける ここまでの作業をおこなっております。 ただ、これで正しいのかが分かりません。 詳しい方からのご助言頂けますでしょうか?

  • GoogleMapsでルート内の座標を認識したい

    Google Maps API で目的地を設定しそれまでのルートを表示することはできるのですが、そのルート上に数メートル間隔で座標をマーカーなどで表示させたり、100m毎くらいに座標を認識したいのですが可能でしょうか? 可能であればAPIとどういったソースなのか教えてください。 よろしくお願いします。

  • Google Maps APIについて質問です

    こんにちは。 現在大学の研究の一環で、Google Maps APIについて学習しております。 APIに関して全くの初心者ですが、質問があります。 今回必要としている機能として、羊ケ丘展望台から札幌駅へ向かう2ルートを地図上に表示させたいのですが 下記に示すようなものが現在できております。 ですが、まだ一つのルートしか表示できません。 このルートをそのままに、同じ出発地と到着地で(43.034047,141.359536)を経由するルートを赤いラインで追加したいと思っているのですが、 この場合はどういったコードになるのでしょうか? かなりいろいろ調べてみましたが、初心者ゆえなかなか上手くできません。 どなたか知識をお持ちの方がいらっしゃいましたら、ご助力頂きたく存じます。 何卒宜しくお願い致します。 <html> <head> <title>Google Maps V3</title> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" /> <style type="text/css"> v\:* {behavior:url(#default#VML);} html, body {width: 100%; height: 100%} body {top:0px;left:0px;margin-top: 0px; margin-right: 0px; margin-left: 0px; margin-bottom: 0px} </style> <script src="http://maps.google.com/maps/api/js?sensor=false"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> <script> google.maps.event.addDomListener(window, 'load', function() { var mapObj; var lng = 139.8131925612688; var lat = 35.73369469149347; var mapOptions = { zoom: 11, center: null, mapTypeId: google.maps.MapTypeId.ROADMAP, scaleControl: true }; mapObj = new google.maps.Map(document.getElementById('gmap'), mapOptions); // ルートを表示するマップを設定 var directionsRenderer = new google.maps.DirectionsRenderer(); directionsRenderer.setMap(mapObj); // 開始地点と終了地点、ルーティングの種類の設定 var request = { origin: "羊ケ丘展望台", destination: "札幌駅", travelMode: google.maps.DirectionsTravelMode.DRIVING }; // ルート検索を行う var directionsService = new google.maps.DirectionsService(); directionsService.route(request, function(result, status) { if (status == google.maps.DirectionsStatus.OK) { directionsRenderer.setDirections(result); } }); // Monitor the window resize event and let the map know when it occurs if (window.attachEvent) { window.attachEvent("onresize", function() {this.map.onResize()} ); } else { window.addEventListener("resize", function() {this.map.onResize()} , false); } }); </script> </head> <body> <div id="gmap" style="top:0px;left:0px;width: 100%; height: 100%;"> </div> </body> </html>

専門家に質問してみよう