Getting Markers to Show up on a Google Map after Promise is fulfilled?
Ok so I am stuck at this point in my project, I have created a google Map and are able to use foursquare to get a bunch of location places and turn them into markers for my map, the problem is that the markers do not load initially and will only show up after the search feature is used, I know I am getting the information asynchronously but have no idea how to tell the code to load the markers as soon as the promise is fulfilled. here is the code.
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps react';
import axios from 'axios';
var AllPlaces = [
]
axios.get("https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=API&client_secret=API&v=20201215&limit=6").then(
response => {
response.data.response.venues.forEach(function(item){
AllPlaces.push(
{
name: item.categories[0].name.toLowerCase(),
lat: item.location.lat,
lng: item.location.lng
}
)
})
}
)
class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
query:'',
filteredPlaces:
};
markers =
onMarkerClick = (props, marker, e) => {
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
}
onLiClick = (i) =>{
this.setState({
showingInfoWindow: true,
activeMarker: this.markers[i],
selectedPlace: AllPlaces[i]
})
}
onMapClicked = (props) => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
})
}
}
CreateInputField = () => (
<input
placeholder = "Search Nearby Places"
onChange={(event) => this.setState({filteredPlaces: AllPlaces.filter(place => !place.name.startsWith((event.target.value).toLowerCase()))})}
/>
)
render() {
return (
<div className = 'map-container' role='application' style=
{{marginleft:'250px'}}>
<div>
<div className = 'navMenu'>
<div className = 'List'>
<h1 className = 'title'> Places to Eat
</h1>
{this.CreateInputField()}
</div>
<div className = 'PlaceList'>
<ol className='Places'>
{AllPlaces.map((arrayItem, index)=>
!this.state.filteredPlaces.includes(arrayItem) &&
<li
key = {index}
className='Place'
onClick={() => {this.onLiClick(index)}}
>{arrayItem.name}</li>
)}
</ol>
</div>
</div>
</div>
<Map google={this.props.google} zoom={14}
initialCenter = {{lat:40.7589, lng:-73.9851}}
onClick={this.onMapClicked}>
{AllPlaces.map((marker, i) =>
!this.state.filteredPlaces.includes(marker) &&
<Marker
ref={(e) => {if (e) this.markers[i] = e.marker}}
onClick={this.onMarkerClick}
title = {marker.name}
key = {i}
name={marker.name}
position =
{{lat:marker.lat,lng:marker.lng}}
/>
)}
<InfoWindow
onOpen={this.windowHasOpened}
onClose={this.windowHasClosed}
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}>
<div>
<h1>{this.state.selectedPlace.name}</h1>
</div>
</InfoWindow>
</Map>
</div>
);
}
}
export default GoogleApiWrapper({
apiKey: 'KEY'
})(MapContainer)
javascript reactjs
add a comment |
Ok so I am stuck at this point in my project, I have created a google Map and are able to use foursquare to get a bunch of location places and turn them into markers for my map, the problem is that the markers do not load initially and will only show up after the search feature is used, I know I am getting the information asynchronously but have no idea how to tell the code to load the markers as soon as the promise is fulfilled. here is the code.
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps react';
import axios from 'axios';
var AllPlaces = [
]
axios.get("https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=API&client_secret=API&v=20201215&limit=6").then(
response => {
response.data.response.venues.forEach(function(item){
AllPlaces.push(
{
name: item.categories[0].name.toLowerCase(),
lat: item.location.lat,
lng: item.location.lng
}
)
})
}
)
class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
query:'',
filteredPlaces:
};
markers =
onMarkerClick = (props, marker, e) => {
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
}
onLiClick = (i) =>{
this.setState({
showingInfoWindow: true,
activeMarker: this.markers[i],
selectedPlace: AllPlaces[i]
})
}
onMapClicked = (props) => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
})
}
}
CreateInputField = () => (
<input
placeholder = "Search Nearby Places"
onChange={(event) => this.setState({filteredPlaces: AllPlaces.filter(place => !place.name.startsWith((event.target.value).toLowerCase()))})}
/>
)
render() {
return (
<div className = 'map-container' role='application' style=
{{marginleft:'250px'}}>
<div>
<div className = 'navMenu'>
<div className = 'List'>
<h1 className = 'title'> Places to Eat
</h1>
{this.CreateInputField()}
</div>
<div className = 'PlaceList'>
<ol className='Places'>
{AllPlaces.map((arrayItem, index)=>
!this.state.filteredPlaces.includes(arrayItem) &&
<li
key = {index}
className='Place'
onClick={() => {this.onLiClick(index)}}
>{arrayItem.name}</li>
)}
</ol>
</div>
</div>
</div>
<Map google={this.props.google} zoom={14}
initialCenter = {{lat:40.7589, lng:-73.9851}}
onClick={this.onMapClicked}>
{AllPlaces.map((marker, i) =>
!this.state.filteredPlaces.includes(marker) &&
<Marker
ref={(e) => {if (e) this.markers[i] = e.marker}}
onClick={this.onMarkerClick}
title = {marker.name}
key = {i}
name={marker.name}
position =
{{lat:marker.lat,lng:marker.lng}}
/>
)}
<InfoWindow
onOpen={this.windowHasOpened}
onClose={this.windowHasClosed}
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}>
<div>
<h1>{this.state.selectedPlace.name}</h1>
</div>
</InfoWindow>
</Map>
</div>
);
}
}
export default GoogleApiWrapper({
apiKey: 'KEY'
})(MapContainer)
javascript reactjs
Please fix the indentation of your code.
– Andreas
Nov 22 at 18:49
add a comment |
Ok so I am stuck at this point in my project, I have created a google Map and are able to use foursquare to get a bunch of location places and turn them into markers for my map, the problem is that the markers do not load initially and will only show up after the search feature is used, I know I am getting the information asynchronously but have no idea how to tell the code to load the markers as soon as the promise is fulfilled. here is the code.
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps react';
import axios from 'axios';
var AllPlaces = [
]
axios.get("https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=API&client_secret=API&v=20201215&limit=6").then(
response => {
response.data.response.venues.forEach(function(item){
AllPlaces.push(
{
name: item.categories[0].name.toLowerCase(),
lat: item.location.lat,
lng: item.location.lng
}
)
})
}
)
class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
query:'',
filteredPlaces:
};
markers =
onMarkerClick = (props, marker, e) => {
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
}
onLiClick = (i) =>{
this.setState({
showingInfoWindow: true,
activeMarker: this.markers[i],
selectedPlace: AllPlaces[i]
})
}
onMapClicked = (props) => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
})
}
}
CreateInputField = () => (
<input
placeholder = "Search Nearby Places"
onChange={(event) => this.setState({filteredPlaces: AllPlaces.filter(place => !place.name.startsWith((event.target.value).toLowerCase()))})}
/>
)
render() {
return (
<div className = 'map-container' role='application' style=
{{marginleft:'250px'}}>
<div>
<div className = 'navMenu'>
<div className = 'List'>
<h1 className = 'title'> Places to Eat
</h1>
{this.CreateInputField()}
</div>
<div className = 'PlaceList'>
<ol className='Places'>
{AllPlaces.map((arrayItem, index)=>
!this.state.filteredPlaces.includes(arrayItem) &&
<li
key = {index}
className='Place'
onClick={() => {this.onLiClick(index)}}
>{arrayItem.name}</li>
)}
</ol>
</div>
</div>
</div>
<Map google={this.props.google} zoom={14}
initialCenter = {{lat:40.7589, lng:-73.9851}}
onClick={this.onMapClicked}>
{AllPlaces.map((marker, i) =>
!this.state.filteredPlaces.includes(marker) &&
<Marker
ref={(e) => {if (e) this.markers[i] = e.marker}}
onClick={this.onMarkerClick}
title = {marker.name}
key = {i}
name={marker.name}
position =
{{lat:marker.lat,lng:marker.lng}}
/>
)}
<InfoWindow
onOpen={this.windowHasOpened}
onClose={this.windowHasClosed}
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}>
<div>
<h1>{this.state.selectedPlace.name}</h1>
</div>
</InfoWindow>
</Map>
</div>
);
}
}
export default GoogleApiWrapper({
apiKey: 'KEY'
})(MapContainer)
javascript reactjs
Ok so I am stuck at this point in my project, I have created a google Map and are able to use foursquare to get a bunch of location places and turn them into markers for my map, the problem is that the markers do not load initially and will only show up after the search feature is used, I know I am getting the information asynchronously but have no idea how to tell the code to load the markers as soon as the promise is fulfilled. here is the code.
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps react';
import axios from 'axios';
var AllPlaces = [
]
axios.get("https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=API&client_secret=API&v=20201215&limit=6").then(
response => {
response.data.response.venues.forEach(function(item){
AllPlaces.push(
{
name: item.categories[0].name.toLowerCase(),
lat: item.location.lat,
lng: item.location.lng
}
)
})
}
)
class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
query:'',
filteredPlaces:
};
markers =
onMarkerClick = (props, marker, e) => {
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
}
onLiClick = (i) =>{
this.setState({
showingInfoWindow: true,
activeMarker: this.markers[i],
selectedPlace: AllPlaces[i]
})
}
onMapClicked = (props) => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
})
}
}
CreateInputField = () => (
<input
placeholder = "Search Nearby Places"
onChange={(event) => this.setState({filteredPlaces: AllPlaces.filter(place => !place.name.startsWith((event.target.value).toLowerCase()))})}
/>
)
render() {
return (
<div className = 'map-container' role='application' style=
{{marginleft:'250px'}}>
<div>
<div className = 'navMenu'>
<div className = 'List'>
<h1 className = 'title'> Places to Eat
</h1>
{this.CreateInputField()}
</div>
<div className = 'PlaceList'>
<ol className='Places'>
{AllPlaces.map((arrayItem, index)=>
!this.state.filteredPlaces.includes(arrayItem) &&
<li
key = {index}
className='Place'
onClick={() => {this.onLiClick(index)}}
>{arrayItem.name}</li>
)}
</ol>
</div>
</div>
</div>
<Map google={this.props.google} zoom={14}
initialCenter = {{lat:40.7589, lng:-73.9851}}
onClick={this.onMapClicked}>
{AllPlaces.map((marker, i) =>
!this.state.filteredPlaces.includes(marker) &&
<Marker
ref={(e) => {if (e) this.markers[i] = e.marker}}
onClick={this.onMarkerClick}
title = {marker.name}
key = {i}
name={marker.name}
position =
{{lat:marker.lat,lng:marker.lng}}
/>
)}
<InfoWindow
onOpen={this.windowHasOpened}
onClose={this.windowHasClosed}
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}>
<div>
<h1>{this.state.selectedPlace.name}</h1>
</div>
</InfoWindow>
</Map>
</div>
);
}
}
export default GoogleApiWrapper({
apiKey: 'KEY'
})(MapContainer)
javascript reactjs
javascript reactjs
asked Nov 22 at 18:35
Hyunjin Kim
173
173
Please fix the indentation of your code.
– Andreas
Nov 22 at 18:49
add a comment |
Please fix the indentation of your code.
– Andreas
Nov 22 at 18:49
Please fix the indentation of your code.
– Andreas
Nov 22 at 18:49
Please fix the indentation of your code.
– Andreas
Nov 22 at 18:49
add a comment |
1 Answer
1
active
oldest
votes
Do the async request inside the component itself, then put that new value to component state. React will know to rerender when state changes, so all you need to worry about is managing the state. Async / await syntax also makes it cleaner
e.g.
class MapContainer extends Component {
async componentWillMount() {
const axiosData = await axios
.get(
'https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=API&client_secret=API&v=20201215&limit=6'
)
.then(response =>
response.data.response.venues.map(v => ({
name: v.categories[0].name.toLowerCase(),
lat: v.location.lat,
lng: v.location.lng,
}))
);
this.setState({axiosData})
}
Im a bit confused here, I tried to add this request in the component but think I might be adding in the state incorrectly, based on this what would the new value in the component state be?
– Hyunjin Kim
Nov 22 at 23:26
nevermind I got it and it works perfectly, thank you very much
– Hyunjin Kim
Nov 22 at 23:44
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436555%2fgetting-markers-to-show-up-on-a-google-map-after-promise-is-fulfilled%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Do the async request inside the component itself, then put that new value to component state. React will know to rerender when state changes, so all you need to worry about is managing the state. Async / await syntax also makes it cleaner
e.g.
class MapContainer extends Component {
async componentWillMount() {
const axiosData = await axios
.get(
'https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=API&client_secret=API&v=20201215&limit=6'
)
.then(response =>
response.data.response.venues.map(v => ({
name: v.categories[0].name.toLowerCase(),
lat: v.location.lat,
lng: v.location.lng,
}))
);
this.setState({axiosData})
}
Im a bit confused here, I tried to add this request in the component but think I might be adding in the state incorrectly, based on this what would the new value in the component state be?
– Hyunjin Kim
Nov 22 at 23:26
nevermind I got it and it works perfectly, thank you very much
– Hyunjin Kim
Nov 22 at 23:44
add a comment |
Do the async request inside the component itself, then put that new value to component state. React will know to rerender when state changes, so all you need to worry about is managing the state. Async / await syntax also makes it cleaner
e.g.
class MapContainer extends Component {
async componentWillMount() {
const axiosData = await axios
.get(
'https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=API&client_secret=API&v=20201215&limit=6'
)
.then(response =>
response.data.response.venues.map(v => ({
name: v.categories[0].name.toLowerCase(),
lat: v.location.lat,
lng: v.location.lng,
}))
);
this.setState({axiosData})
}
Im a bit confused here, I tried to add this request in the component but think I might be adding in the state incorrectly, based on this what would the new value in the component state be?
– Hyunjin Kim
Nov 22 at 23:26
nevermind I got it and it works perfectly, thank you very much
– Hyunjin Kim
Nov 22 at 23:44
add a comment |
Do the async request inside the component itself, then put that new value to component state. React will know to rerender when state changes, so all you need to worry about is managing the state. Async / await syntax also makes it cleaner
e.g.
class MapContainer extends Component {
async componentWillMount() {
const axiosData = await axios
.get(
'https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=API&client_secret=API&v=20201215&limit=6'
)
.then(response =>
response.data.response.venues.map(v => ({
name: v.categories[0].name.toLowerCase(),
lat: v.location.lat,
lng: v.location.lng,
}))
);
this.setState({axiosData})
}
Do the async request inside the component itself, then put that new value to component state. React will know to rerender when state changes, so all you need to worry about is managing the state. Async / await syntax also makes it cleaner
e.g.
class MapContainer extends Component {
async componentWillMount() {
const axiosData = await axios
.get(
'https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=API&client_secret=API&v=20201215&limit=6'
)
.then(response =>
response.data.response.venues.map(v => ({
name: v.categories[0].name.toLowerCase(),
lat: v.location.lat,
lng: v.location.lng,
}))
);
this.setState({axiosData})
}
answered Nov 22 at 18:53
Cecil
21125
21125
Im a bit confused here, I tried to add this request in the component but think I might be adding in the state incorrectly, based on this what would the new value in the component state be?
– Hyunjin Kim
Nov 22 at 23:26
nevermind I got it and it works perfectly, thank you very much
– Hyunjin Kim
Nov 22 at 23:44
add a comment |
Im a bit confused here, I tried to add this request in the component but think I might be adding in the state incorrectly, based on this what would the new value in the component state be?
– Hyunjin Kim
Nov 22 at 23:26
nevermind I got it and it works perfectly, thank you very much
– Hyunjin Kim
Nov 22 at 23:44
Im a bit confused here, I tried to add this request in the component but think I might be adding in the state incorrectly, based on this what would the new value in the component state be?
– Hyunjin Kim
Nov 22 at 23:26
Im a bit confused here, I tried to add this request in the component but think I might be adding in the state incorrectly, based on this what would the new value in the component state be?
– Hyunjin Kim
Nov 22 at 23:26
nevermind I got it and it works perfectly, thank you very much
– Hyunjin Kim
Nov 22 at 23:44
nevermind I got it and it works perfectly, thank you very much
– Hyunjin Kim
Nov 22 at 23:44
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53436555%2fgetting-markers-to-show-up-on-a-google-map-after-promise-is-fulfilled%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please fix the indentation of your code.
– Andreas
Nov 22 at 18:49