mogzzer

Untitled

Feb 29th, 2020
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. async showDirectionOnMap(placeId){
  2. const { userLatitude, userLongitude, wayPoints, markers } = this.state;
  3. try{
  4. let result = await axios.get(
  5. `https://maps.googleapis.com/maps/api/directions/json?key=${MY_API_KEY}&origin=${userLatitude},${userLongitude}&destination=place_id:${placeId}`
  6. );
  7.  
  8. console.log(`This is result.data`, result.data);
  9. let points = PolyLine.decode(
  10. result.data.routes[0].overview_polyline.points
  11. )
  12.  
  13. let latLng = points.map(point => ({
  14. latitude: point[0],
  15. longitude: point[1],
  16. }));
  17. this.setState({
  18. destination: placeId,
  19. destinationCoords: [...this.state.destinationCoords, latLng],
  20. wayPoints: [...wayPoints, latLng],
  21. markers: [...markers, latLng],
  22. });
  23. this.map.current.fitToCoordinates(latLng, {
  24. edgePadding:{
  25. top: 40,
  26. bottom: 40,
  27. left: 40,
  28. right: 40
  29. }
  30. });
  31. console.log('Lets see what happen')
  32. console.log(latLng);
  33. console.log(latLng.length)
  34. console.log('Hold on')
  35. console.log(points)
  36. console.log('This is wayPoints: ' + wayPoints)
  37. console.log('This is markers: ' + markers)
  38.  
  39. } catch(err){
  40. console.error(err);
  41. }
  42.  
  43. }
  44.  
  45. async getPlaceDetails(){
  46. const {destination} = this.state
  47. try{
  48. let result = await axios.get(
  49. `https://maps.googleapis.com/maps/api/place/details/json?key=${MY_API_KEY}&place_id=${destination}`
  50. );
  51. let locationDecoded = result["result"]["geometry"]["location"]
  52. this.setState({
  53. userLatitude: locationDecoded["lat"],
  54. userLongitude: locationDecoded["lng"]
  55. })
  56. console.log('This is another userLatitude: ', this.state.userLatitude)
  57. console.log('This is another userLongitude: ', this.state.userLongitude)
  58. }catch{(err) => {
  59. console.log(err)
  60. }}
  61. }
  62.  
  63.  
  64. onAddSearch(){
  65. this.getPlaceDetails(); // It doesn't excute :(
  66.  
  67. this.setState((state) => ({
  68. counter: state.counter + INCREMENT,
  69. numOfInput: [...state.numOfInput, state.counter],
  70. }));
  71.  
  72.  
  73. console.log('These are destinationCoords: ', this.state.destinationCoords)
  74. console.log('This is destination ', this.state.destination)
  75. console.log('This is: destinationCoords[0]: ')
  76. console.log(this.state.destinationCoords[0])
  77. console.log('Wait')
  78. console.log(this.state.destinationCoords[0][0])
  79. console.log(this.state.destinationCoords[0][0].latitude)
  80. console.log('These are markers: ', this.state.markers)
  81. console.log('This is userLatitude: ', this.state.userLatitude)
  82. console.log('This is userLongitude: ', this.state.userLongitude)
  83. // console.log(this.state.counter);
  84. // console.log(this.state.numOfInput);
  85. }
Advertisement
Add Comment
Please, Sign In to add comment