こんにちは
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
あなたの回答
tips
プレビュー