前提・実現したいこと
React+Redux+Laravelを使用して個人でアプリケーションを開発しています。
そこで、現在地を取得してGoogleMapsで表示させようと思っているのですが、なぜか現在地がでたらめな場所に表示されてしまいます。関西と関東ほどの誤差があります。
現在地の取得にはcomponentDidMount内でJS標準のGeolocation APIを使用して取得した緯度、経度をstateに保存して使用しています。GoogleMapsの表示に関してはgoogle-map-reactを使用しています。
どなたか解決方法のご教授お願いいたします。
※開発環境にはLaradocを使用しています。
※getGourmet関数に関してはホットペッパーAPIを叩いているだけですので今回は関係ないです。
該当のソースコード
JavaScript
1import React from 'react' 2import { Link } from 'react-router-dom' 3import { withStyles } from '@material-ui/core/styles' 4import GoogleMapReact from 'google-map-react' 5import * as actions from '../../actions' 6import { connect } from 'react-redux' 7import { bindActionCreators } from 'redux' 8 9import Card from '@material-ui/core/Card' 10import CardActions from '@material-ui/core/CardActions' 11import CardContent from '@material-ui/core/CardContent' 12import CardMedia from '@material-ui/core/CardMedia' 13import Button from '@material-ui/core/Button' 14import AddLocationIcon from '@material-ui/icons/AddLocation' 15import EmojiPeopleIcon from '@material-ui/icons/EmojiPeople' 16import Replay from '@material-ui/icons/Replay' 17 18 19const drawerWidth = 250 // Width for Drawer 20 21const styles = (theme) => ({ 22 root: { 23 [theme.breakpoints.up('md')]: { 24 width: `calc(100% - ${drawerWidth}px)`, 25 marginLeft: drawerWidth 26 }, 27 padding: 30, 28 }, 29 toolbar: theme.mixins.toolbar, // Minimum height of Toolbar 30 link: { 31 textDecoration: 'none' 32 }, 33 titleArea: { 34 margin: '0 auto', 35 display: 'flex', 36 justifyContent: 'center', 37 alignItems: 'center', 38 flexDirection: 'column' 39 }, 40 title: { 41 margin: '0 auto', 42 marginBottom: 20, 43 padding: 0 44 }, 45 btn: { 46 margin: 'auto', 47 textAlign: 'center', 48 }, 49 map: { 50 width: '90vmin', 51 height: '90vmin', 52 marginTop: 40, 53 marginBottom: 50, 54 margin: '0 auto' 55 }, 56 shopListTitle: { 57 textAlign: 'center' 58 }, 59 card: { 60 [theme.breakpoints.up('sm')]: { 61 width: '45%' 62 }, 63 width: '100%', 64 margin: 10, 65 }, 66 cardTitle: { 67 textDecoration: 'none', 68 color: 'black', 69 }, 70 infoTable: { 71 borderCollapse: 'collapse' 72 }, 73 info: { 74 border: 'solid 1px red', 75 padding: 5 76 }, 77 share: { 78 textDecoration: 'none', 79 color: '#bbb' 80 }, 81 more: { 82 textDecoration: 'none', 83 color: '#bbb' 84 }, 85 cards: { 86 display: 'flex', 87 flexWrap: 'wrap', 88 margin: '0 auto', 89 } 90}) 91 92const Me = () => <div><EmojiPeopleIcon /></div> 93const Pin = () => <div><AddLocationIcon /></div> 94 95class Result extends React.Component { 96 constructor(props) { 97 super(props) 98 this.state = { 99 lat: null, 100 lng: null, 101 shops: '' 102 } 103 } 104 105 render() { 106 const { classes } = this.props 107 const shops = Array.from(this.state.shops) 108 const center = { lat: this.state.lat, lng: this.state.lng } 109 110 // TODO: いいねボタン 111 return ( 112 <div className={classes.root}> 113 <div className={classes.toolbar} /> 114 <div className={classes.titleArea}> 115 <h2 className={classes.title}>周辺のお店</h2> 116 <Link to="/" className={classes.link}><Button className={classes.btn} startIcon={<Replay />} variant="outlined" color="primary">やり直す</Button></Link> 117 </div> 118 <div className={classes.map}> 119 <GoogleMapReact 120 bootstrapURLKeys={{ 121 key: '***********' 122 }} 123 center={center} 124 defaultZoom={14} 125 yesIWantToUseGoogleMapApiInternals 126 > 127 {/* 現在地 */} 128 <Me 129 lat={this.state.lat} 130 lng={this.state.lng} 131 /> 132 133 {/* 店にピンを挿す */} 134 {shops.map((shop, i) => ( 135 <Pin 136 key={i} 137 lat={shop.lat} 138 lng={shop.lng} 139 /> 140 ))} 141 </GoogleMapReact> 142 </div> 143 <h2 className={classes.shopListTitle}>お店一覧</h2> 144 <div className={classes.cards}> 145 {/* 店の情報を一覧表示 */} 146 {shops.map((shop, i) => ( 147 <Card className={classes.card} key={i}> 148 <CardMedia 149 className={classes.media} 150 image={shop.logo_image} 151 title="Contemplative Reptile" 152 /> 153 <CardContent> 154 <h2><a href={shop.urls.pc} target="_blank" className={classes.cardTitle}>{shop.name}</a></h2> 155 <p>ー {shop.genre.catch} ー</p> 156 <table className={classes.infoTable}> 157 <thead> 158 <tr><td className={classes.info}>アクセス</td><td className={classes.info}>{shop.access}</td></tr> 159 </thead> 160 <tbody> 161 <tr><td className={classes.info}>平均予算</td><td className={classes.info}>{shop.budget.average}</td></tr> 162 <tr><td className={classes.info}>飲み放題</td><td className={classes.info}>{shop.free_drink}</td></tr> 163 <tr><td className={classes.info}>食べ放題</td><td className={classes.info}>{shop.free_food}</td></tr> 164 <tr><td className={classes.info}>最大人数</td><td className={classes.info}>{shop.party_capacity}人</td></tr> 165 <tr><td className={classes.info}>駐車場</td><td className={classes.info}>{shop.parking}</td></tr> 166 <tr><td className={classes.info}>open</td><td className={classes.info}>{shop.open}</td></tr> 167 </tbody> 168 </table> 169 </CardContent> 170 <CardActions> 171 <Button size="small"> 172 <a href={shop.urls.pc} target="_blank" className={classes.more}>Show More</a> 173 </Button> 174 </CardActions> 175 </Card> 176 ))} 177 </div> 178 </div> 179 ) 180 } 181 182 componentDidMount() { 183 navigator.geolocation.getCurrentPosition(pos => { 184 this.setState({ 185 lat: pos.coords.latitude, 186 lng: pos.coords.longitude 187 }) 188 189 const http = axios.create({ 190 baseURL: 'http://localhost:8000/api/gourmet', 191 timeout: 2000, 192 }) 193 194 this.props.actions.httpRequest() 195 196 // TODO: 関数化? 197 const getGourmet = async () => { 198 try { 199 const response = await http.get('', { 200 params: { 201 range: 3, 202 order: 4, 203 lat: pos.coords.latitude, 204 lng: pos.coords.longitude, 205 party_capacity: `${this.props.QuestionsReducer.people}`, 206 budget: `${this.props.QuestionsReducer.budget}`, 207 free_food: `${this.props.AmountReducer.freeFood}`, 208 free_drink: `${this.props.AmountReducer.freeDrink}`, 209 genre: `${this.props.QuestionsReducer.genre}` 210 } 211 }) 212 this.setState({ 213 shops: response.data.results.shop 214 }) 215 console.log(response.data.results.shop) 216 } catch (error) { 217 console.log(error) 218 } 219 } 220 221 getGourmet() 222 223 this.props.actions.httpSuccess() 224 }, err => { 225 this.props.actions.httpFailure() 226 console.log(err) 227 }) 228 } 229} 230 231const mapStateToProps = state => ({ 232 QuestionsReducer: state.QuestionsReducer, 233 AmountReducer: state.AmountReducer, 234 HttpReducer: state.HttpReducer 235}) 236 237const mapDispatchToProps = dispatch => ({ 238 actions: bindActionCreators(actions, dispatch) 239}) 240 241const styledResult = withStyles(styles, { withTheme: true })(Result) 242 243export default connect(mapStateToProps, mapDispatchToProps)(styledResult) 244
回答1件
あなたの回答
tips
プレビュー