How to Grab information from an XMLHttp request and convert into an array.
up vote
0
down vote
favorite
I am currently working on an app and have gotten stuck. I have created a XMLHttp request and need to pull the information from it and put it into an array so I can create markers out of the request on a google map. The code is:
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';
//"https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
const Http = new XMLHttpRequest();
const url = "https://api.foursquare.com/v2/venues/search? ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
Http.open('GET', url)
Http.send();
Http.onreadystatechange=(e)=>{
console.log(Http.responseText)
}
var AllPlaces = [
]
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: 'AIzaSyD2BmXOGrCZQiAg5HJ6hU70BXc5v6Osr6M'
})(MapContainer)
I need to take in information I get from the XML request and put each object into the array I have at the top called AllPlaces I have tried with response and response text but have no idea how to loop through the JSON object that is returned.
javascript reactjs
add a comment |
up vote
0
down vote
favorite
I am currently working on an app and have gotten stuck. I have created a XMLHttp request and need to pull the information from it and put it into an array so I can create markers out of the request on a google map. The code is:
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';
//"https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
const Http = new XMLHttpRequest();
const url = "https://api.foursquare.com/v2/venues/search? ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
Http.open('GET', url)
Http.send();
Http.onreadystatechange=(e)=>{
console.log(Http.responseText)
}
var AllPlaces = [
]
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: 'AIzaSyD2BmXOGrCZQiAg5HJ6hU70BXc5v6Osr6M'
})(MapContainer)
I need to take in information I get from the XML request and put each object into the array I have at the top called AllPlaces I have tried with response and response text but have no idea how to loop through the JSON object that is returned.
javascript reactjs
1
Make sure you remove your API Key before sharing with other people :)
– squeekyDave
Nov 21 at 15:53
You could do a foreach loop through the response object. And then manually push each item into your array.
– Anthony Sobo
Nov 21 at 16:40
@AnthonySobo how would that look, I am trying to loop through with for each but it doesnt seem to work
– Hyunjin Kim
Nov 21 at 16:44
When you say doesn't work what exactly do you mean? Are you getting an error when you try to loop?
– Anthony Sobo
Nov 21 at 16:48
@AnthonySobo No, nothing gets logged to the console when I try to loop over it
– Hyunjin Kim
Nov 21 at 16:59
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am currently working on an app and have gotten stuck. I have created a XMLHttp request and need to pull the information from it and put it into an array so I can create markers out of the request on a google map. The code is:
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';
//"https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
const Http = new XMLHttpRequest();
const url = "https://api.foursquare.com/v2/venues/search? ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
Http.open('GET', url)
Http.send();
Http.onreadystatechange=(e)=>{
console.log(Http.responseText)
}
var AllPlaces = [
]
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: 'AIzaSyD2BmXOGrCZQiAg5HJ6hU70BXc5v6Osr6M'
})(MapContainer)
I need to take in information I get from the XML request and put each object into the array I have at the top called AllPlaces I have tried with response and response text but have no idea how to loop through the JSON object that is returned.
javascript reactjs
I am currently working on an app and have gotten stuck. I have created a XMLHttp request and need to pull the information from it and put it into an array so I can create markers out of the request on a google map. The code is:
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';
//"https://api.foursquare.com/v2/venues/search?ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
const Http = new XMLHttpRequest();
const url = "https://api.foursquare.com/v2/venues/search? ll=40.7589,-73.9851&query=food&radius=2000&categoryId=4d4b7105d754a06374d81259&client_id=INOHETWKCQHOZUPUATJJ503RCWMUXPFUOCJCHRN2LRADAZJO&client_secret=1ABUWB4S3QS41A50X1UJFHC4THA2YQGOBNOBUSRARLZ4DE5W&v=20201215&limit=25"
Http.open('GET', url)
Http.send();
Http.onreadystatechange=(e)=>{
console.log(Http.responseText)
}
var AllPlaces = [
]
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: 'AIzaSyD2BmXOGrCZQiAg5HJ6hU70BXc5v6Osr6M'
})(MapContainer)
I need to take in information I get from the XML request and put each object into the array I have at the top called AllPlaces I have tried with response and response text but have no idea how to loop through the JSON object that is returned.
javascript reactjs
javascript reactjs
asked Nov 21 at 15:51
Hyunjin Kim
173
173
1
Make sure you remove your API Key before sharing with other people :)
– squeekyDave
Nov 21 at 15:53
You could do a foreach loop through the response object. And then manually push each item into your array.
– Anthony Sobo
Nov 21 at 16:40
@AnthonySobo how would that look, I am trying to loop through with for each but it doesnt seem to work
– Hyunjin Kim
Nov 21 at 16:44
When you say doesn't work what exactly do you mean? Are you getting an error when you try to loop?
– Anthony Sobo
Nov 21 at 16:48
@AnthonySobo No, nothing gets logged to the console when I try to loop over it
– Hyunjin Kim
Nov 21 at 16:59
add a comment |
1
Make sure you remove your API Key before sharing with other people :)
– squeekyDave
Nov 21 at 15:53
You could do a foreach loop through the response object. And then manually push each item into your array.
– Anthony Sobo
Nov 21 at 16:40
@AnthonySobo how would that look, I am trying to loop through with for each but it doesnt seem to work
– Hyunjin Kim
Nov 21 at 16:44
When you say doesn't work what exactly do you mean? Are you getting an error when you try to loop?
– Anthony Sobo
Nov 21 at 16:48
@AnthonySobo No, nothing gets logged to the console when I try to loop over it
– Hyunjin Kim
Nov 21 at 16:59
1
1
Make sure you remove your API Key before sharing with other people :)
– squeekyDave
Nov 21 at 15:53
Make sure you remove your API Key before sharing with other people :)
– squeekyDave
Nov 21 at 15:53
You could do a foreach loop through the response object. And then manually push each item into your array.
– Anthony Sobo
Nov 21 at 16:40
You could do a foreach loop through the response object. And then manually push each item into your array.
– Anthony Sobo
Nov 21 at 16:40
@AnthonySobo how would that look, I am trying to loop through with for each but it doesnt seem to work
– Hyunjin Kim
Nov 21 at 16:44
@AnthonySobo how would that look, I am trying to loop through with for each but it doesnt seem to work
– Hyunjin Kim
Nov 21 at 16:44
When you say doesn't work what exactly do you mean? Are you getting an error when you try to loop?
– Anthony Sobo
Nov 21 at 16:48
When you say doesn't work what exactly do you mean? Are you getting an error when you try to loop?
– Anthony Sobo
Nov 21 at 16:48
@AnthonySobo No, nothing gets logged to the console when I try to loop over it
– Hyunjin Kim
Nov 21 at 16:59
@AnthonySobo No, nothing gets logged to the console when I try to loop over it
– Hyunjin Kim
Nov 21 at 16:59
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53415789%2fhow-to-grab-information-from-an-xmlhttp-request-and-convert-into-an-array%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
1
Make sure you remove your API Key before sharing with other people :)
– squeekyDave
Nov 21 at 15:53
You could do a foreach loop through the response object. And then manually push each item into your array.
– Anthony Sobo
Nov 21 at 16:40
@AnthonySobo how would that look, I am trying to loop through with for each but it doesnt seem to work
– Hyunjin Kim
Nov 21 at 16:44
When you say doesn't work what exactly do you mean? Are you getting an error when you try to loop?
– Anthony Sobo
Nov 21 at 16:48
@AnthonySobo No, nothing gets logged to the console when I try to loop over it
– Hyunjin Kim
Nov 21 at 16:59