質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

0回答

2564閲覧

google-map-reactとDirectionsAPIを利用して、経路表示をさせたいが、DirectionsRendererやDirectionsServiceのメソッドが呼び出せない

kazoot

総合スコア12

Google API

Googleは多種多様なAPIを提供していて、その多くはウェブ開発者向けのAPIです。それらのAPIは消費者に人気なGoogleのサービス(Google Maps, Google Earth, AdSense, Adwords, Google Apps,YouTube等)に基づいています。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2019/07/18 11:48

こんにちは
ReactおよびTypeScriptで、google-map-reactとDirectionsAPIを利用して、経路表示をさせようと試みています。
いろいろな情報を参考にして、DirectionsServiceとDirectionsRendererを利用することで、経路表示が可能になることは理解できたのですが、
RendererやServiceの利用箇所で

TypeError: renderer.setMap is not a function

というエラーが発生して関数を呼び出せずにいて困っています。

仮にこの部分をコメントアウトしたとしても、他の関数利用箇所で同様に
××× is not a function
というエラーが発生します。

console.logでmapやmapsの値を出力するとundefinedではなくて中身がいろいろ出力されているので値がわたってきていないというわけではないようです。

何か解決策はありそうでしょうか?
仮説でも大変ありがたいので回答よろしくお願いします。

TypeScript

1import React, {Component} from 'react'; 2import GoogleMapReact, {Coords} from "google-map-react"; 3 4const directoins = (args: any, places: Coords) => { 5 const map = args.map; 6 const maps = args.maps; 7 8// この場所において出力はされる 9 console.log(map) 10 console.log(maps) 11 let request = { 12 origin: maps.LatLng(35.681382, 139.766084), 13 destination: maps.LatLng(35.681382, 139.766085), 14 travelMode: maps.DirectionsTravelMode.WALKING, 15 }; 16 17 let directionsService = maps.DirectionsService; 18 let renderer = maps.DirectionsRenderer; 19 20// 以下の箇所に置いて該当エラーメッセージが発生している 21 renderer.setMap(map) 22 23 directionsService.route(request, function (result: any, status: any) { 24 if (status == maps.DirectionsStatus.OK) { 25 renderer.setDirections(result); 26 } 27 }); 28}; 29 30class SimpleMap extends Component { 31 state = { 32 lat: null, 33 lng: null, 34 zoom: 14 35 }; 36 37 componentDidMount() { 38 navigator.geolocation.getCurrentPosition((position) => { 39 this.setState({ 40 lat: position.coords.latitude, 41 lng: position.coords.longitude 42 }); 43 }, 44 (err) => { 45 console.log(err); 46 }) 47 } 48 49 buildCords(lat: number, lng: number): any { 50 return { 51 lat, 52 lng 53 } 54 } 55 56 render() { 57 if (this.state.lat == null || this.state.lng == null) { 58 return null 59 } 60 61 const center = this.buildCords(convertNullToInt(this.state.lat), convertNullToInt(this.state.lng)) 62 return ( 63 <div style={{height: '100vh', width: '100%'}}> 64 <GoogleMapReact 65 bootstrapURLKeys={{key: "hogehoge"}} 66 defaultCenter={center} 67 defaultZoom={this.state.zoom} 68 yesIWantToUseGoogleMapApiInternals={true} 69 onGoogleApiLoaded={(args: { map: any, maps: any }) => directoins(args, center)}> 70 </GoogleMapReact> 71 </div> 72 ); 73 } 74} 75 76export default SimpleMap; 77 78function convertNullToInt(n: number | null) { 79 if (n == null) { 80 return 0 81 } 82 83 return n 84} 85

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問