How to filter results and update a google map
Last Query from me about this,
I have done everything I need to at this point except for one thing, no mater what I try I cant seem to get the search to show items on the list and markers on the map.
Ive tried to use filter but can only get the list to narrow down when the input field has something. I cant seem to get the markers and List to change.
Here is the code I have so far
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';
var AllPlaces = [
{
"name" : "Pizza",
"lat": "40.7589",
"lng":"-73.9851",
},
{
"name" : "Cookies",
"lat": "40.7690",
"lng":"-73.9952",
},
{
"name" : "Bagels",
"lat": "40.7489",
"lng":"-73.9751",
}
]
class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
query:'',
};
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"
/>
)
findPlaces = () => (
<ol className='Places'>
{AllPlaces.map((arrayItem, index)=>
<li
key = {index}
className='Place'
onClick={() => {this.onLiClick(index)}}
>{arrayItem.name}</li>
)}
</ol>
);
render() {
return (
<div className = 'map-container' style=
{{marginleft:'250px'}}>
<div>
<div className = 'sideMenu'>
<div className = 'List'>
<h1 className = 'title'> Places to Eat
</h1>
{this.CreateInputField()}
</div>
<div className = 'PlaceList'>
{this.findPlaces()}
</div>
</div>
</div>
<Map google={this.props.google} zoom={14}
initialCenter = {{lat:40.7589, lng:-73.9851}}
onClick={this.onMapClicked}>
{AllPlaces.map((marker, i) =>
<Marker
ref={(e) => {if (e) this.markers[i] =
e.marker}}
onClick={this.onMarkerClick}
title = {marker.name}
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: 'AIzaSyC21SntdNn1vCb5VOAujCPIM7a9p5XkvRs'
})(MapContainer)
I have been stuck on this for about 2 weeks now and just need a little guidance on it.
javascript reactjs
add a comment |
Last Query from me about this,
I have done everything I need to at this point except for one thing, no mater what I try I cant seem to get the search to show items on the list and markers on the map.
Ive tried to use filter but can only get the list to narrow down when the input field has something. I cant seem to get the markers and List to change.
Here is the code I have so far
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';
var AllPlaces = [
{
"name" : "Pizza",
"lat": "40.7589",
"lng":"-73.9851",
},
{
"name" : "Cookies",
"lat": "40.7690",
"lng":"-73.9952",
},
{
"name" : "Bagels",
"lat": "40.7489",
"lng":"-73.9751",
}
]
class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
query:'',
};
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"
/>
)
findPlaces = () => (
<ol className='Places'>
{AllPlaces.map((arrayItem, index)=>
<li
key = {index}
className='Place'
onClick={() => {this.onLiClick(index)}}
>{arrayItem.name}</li>
)}
</ol>
);
render() {
return (
<div className = 'map-container' style=
{{marginleft:'250px'}}>
<div>
<div className = 'sideMenu'>
<div className = 'List'>
<h1 className = 'title'> Places to Eat
</h1>
{this.CreateInputField()}
</div>
<div className = 'PlaceList'>
{this.findPlaces()}
</div>
</div>
</div>
<Map google={this.props.google} zoom={14}
initialCenter = {{lat:40.7589, lng:-73.9851}}
onClick={this.onMapClicked}>
{AllPlaces.map((marker, i) =>
<Marker
ref={(e) => {if (e) this.markers[i] =
e.marker}}
onClick={this.onMarkerClick}
title = {marker.name}
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: 'AIzaSyC21SntdNn1vCb5VOAujCPIM7a9p5XkvRs'
})(MapContainer)
I have been stuck on this for about 2 weeks now and just need a little guidance on it.
javascript reactjs
Maybe you should hide your api key? I know it's not an answer but just a safety measure.
– squeekyDave
Nov 14 '18 at 21:42
add a comment |
Last Query from me about this,
I have done everything I need to at this point except for one thing, no mater what I try I cant seem to get the search to show items on the list and markers on the map.
Ive tried to use filter but can only get the list to narrow down when the input field has something. I cant seem to get the markers and List to change.
Here is the code I have so far
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';
var AllPlaces = [
{
"name" : "Pizza",
"lat": "40.7589",
"lng":"-73.9851",
},
{
"name" : "Cookies",
"lat": "40.7690",
"lng":"-73.9952",
},
{
"name" : "Bagels",
"lat": "40.7489",
"lng":"-73.9751",
}
]
class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
query:'',
};
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"
/>
)
findPlaces = () => (
<ol className='Places'>
{AllPlaces.map((arrayItem, index)=>
<li
key = {index}
className='Place'
onClick={() => {this.onLiClick(index)}}
>{arrayItem.name}</li>
)}
</ol>
);
render() {
return (
<div className = 'map-container' style=
{{marginleft:'250px'}}>
<div>
<div className = 'sideMenu'>
<div className = 'List'>
<h1 className = 'title'> Places to Eat
</h1>
{this.CreateInputField()}
</div>
<div className = 'PlaceList'>
{this.findPlaces()}
</div>
</div>
</div>
<Map google={this.props.google} zoom={14}
initialCenter = {{lat:40.7589, lng:-73.9851}}
onClick={this.onMapClicked}>
{AllPlaces.map((marker, i) =>
<Marker
ref={(e) => {if (e) this.markers[i] =
e.marker}}
onClick={this.onMarkerClick}
title = {marker.name}
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: 'AIzaSyC21SntdNn1vCb5VOAujCPIM7a9p5XkvRs'
})(MapContainer)
I have been stuck on this for about 2 weeks now and just need a little guidance on it.
javascript reactjs
Last Query from me about this,
I have done everything I need to at this point except for one thing, no mater what I try I cant seem to get the search to show items on the list and markers on the map.
Ive tried to use filter but can only get the list to narrow down when the input field has something. I cant seem to get the markers and List to change.
Here is the code I have so far
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';
var AllPlaces = [
{
"name" : "Pizza",
"lat": "40.7589",
"lng":"-73.9851",
},
{
"name" : "Cookies",
"lat": "40.7690",
"lng":"-73.9952",
},
{
"name" : "Bagels",
"lat": "40.7489",
"lng":"-73.9751",
}
]
class MapContainer extends Component {
state = {
showingInfoWindow: false,
activeMarker: {},
selectedPlace: {},
query:'',
};
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"
/>
)
findPlaces = () => (
<ol className='Places'>
{AllPlaces.map((arrayItem, index)=>
<li
key = {index}
className='Place'
onClick={() => {this.onLiClick(index)}}
>{arrayItem.name}</li>
)}
</ol>
);
render() {
return (
<div className = 'map-container' style=
{{marginleft:'250px'}}>
<div>
<div className = 'sideMenu'>
<div className = 'List'>
<h1 className = 'title'> Places to Eat
</h1>
{this.CreateInputField()}
</div>
<div className = 'PlaceList'>
{this.findPlaces()}
</div>
</div>
</div>
<Map google={this.props.google} zoom={14}
initialCenter = {{lat:40.7589, lng:-73.9851}}
onClick={this.onMapClicked}>
{AllPlaces.map((marker, i) =>
<Marker
ref={(e) => {if (e) this.markers[i] =
e.marker}}
onClick={this.onMarkerClick}
title = {marker.name}
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: 'AIzaSyC21SntdNn1vCb5VOAujCPIM7a9p5XkvRs'
})(MapContainer)
I have been stuck on this for about 2 weeks now and just need a little guidance on it.
javascript reactjs
javascript reactjs
asked Nov 14 '18 at 21:37
Hyunjin KimHyunjin Kim
173
173
Maybe you should hide your api key? I know it's not an answer but just a safety measure.
– squeekyDave
Nov 14 '18 at 21:42
add a comment |
Maybe you should hide your api key? I know it's not an answer but just a safety measure.
– squeekyDave
Nov 14 '18 at 21:42
Maybe you should hide your api key? I know it's not an answer but just a safety measure.
– squeekyDave
Nov 14 '18 at 21:42
Maybe you should hide your api key? I know it's not an answer but just a safety measure.
– squeekyDave
Nov 14 '18 at 21:42
add a comment |
1 Answer
1
active
oldest
votes
Try this, I've created an array inside state to store the filtered out locations. When you type it filters and updates the locations. :)
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';
var AllPlaces = [
{
"name" : "Pizza",
"lat": "40.7589",
"lng":"-73.9851",
},
{
"name" : "Cookies",
"lat": "40.7690",
"lng":"-73.9952",
},
{
"name" : "Bagels",
"lat": "40.7489",
"lng":"-73.9751",
}
]
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))})}
/>
)
render() {
return (
<div className = 'map-container' style=
{{marginleft:'250px'}}>
<div>
<div className = 'sideMenu'>
<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}
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: 'AIzaSyC21SntdNn1vCb5VOAujCPIM7a9p5XkvRs'
})(MapContainer)
youre saving my life right now haha, I tried in the code but when I type anything it removes all the markers and clears the whole list, this is def a step in the right direction here is the source github.com/JustJin28/react-map-the-one-that-will-pass im going to keep trying at it, I really appreciate the help, if I could take you out for a drink I would
– Hyunjin Kim
Nov 14 '18 at 22:39
Edit, it does show but cant account for capital letters so if I start by typing lowercase it will get rid of all the markers
– Hyunjin Kim
Nov 14 '18 at 22:52
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%2f53309098%2fhow-to-filter-results-and-update-a-google-map%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
Try this, I've created an array inside state to store the filtered out locations. When you type it filters and updates the locations. :)
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';
var AllPlaces = [
{
"name" : "Pizza",
"lat": "40.7589",
"lng":"-73.9851",
},
{
"name" : "Cookies",
"lat": "40.7690",
"lng":"-73.9952",
},
{
"name" : "Bagels",
"lat": "40.7489",
"lng":"-73.9751",
}
]
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))})}
/>
)
render() {
return (
<div className = 'map-container' style=
{{marginleft:'250px'}}>
<div>
<div className = 'sideMenu'>
<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}
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: 'AIzaSyC21SntdNn1vCb5VOAujCPIM7a9p5XkvRs'
})(MapContainer)
youre saving my life right now haha, I tried in the code but when I type anything it removes all the markers and clears the whole list, this is def a step in the right direction here is the source github.com/JustJin28/react-map-the-one-that-will-pass im going to keep trying at it, I really appreciate the help, if I could take you out for a drink I would
– Hyunjin Kim
Nov 14 '18 at 22:39
Edit, it does show but cant account for capital letters so if I start by typing lowercase it will get rid of all the markers
– Hyunjin Kim
Nov 14 '18 at 22:52
add a comment |
Try this, I've created an array inside state to store the filtered out locations. When you type it filters and updates the locations. :)
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';
var AllPlaces = [
{
"name" : "Pizza",
"lat": "40.7589",
"lng":"-73.9851",
},
{
"name" : "Cookies",
"lat": "40.7690",
"lng":"-73.9952",
},
{
"name" : "Bagels",
"lat": "40.7489",
"lng":"-73.9751",
}
]
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))})}
/>
)
render() {
return (
<div className = 'map-container' style=
{{marginleft:'250px'}}>
<div>
<div className = 'sideMenu'>
<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}
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: 'AIzaSyC21SntdNn1vCb5VOAujCPIM7a9p5XkvRs'
})(MapContainer)
youre saving my life right now haha, I tried in the code but when I type anything it removes all the markers and clears the whole list, this is def a step in the right direction here is the source github.com/JustJin28/react-map-the-one-that-will-pass im going to keep trying at it, I really appreciate the help, if I could take you out for a drink I would
– Hyunjin Kim
Nov 14 '18 at 22:39
Edit, it does show but cant account for capital letters so if I start by typing lowercase it will get rid of all the markers
– Hyunjin Kim
Nov 14 '18 at 22:52
add a comment |
Try this, I've created an array inside state to store the filtered out locations. When you type it filters and updates the locations. :)
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';
var AllPlaces = [
{
"name" : "Pizza",
"lat": "40.7589",
"lng":"-73.9851",
},
{
"name" : "Cookies",
"lat": "40.7690",
"lng":"-73.9952",
},
{
"name" : "Bagels",
"lat": "40.7489",
"lng":"-73.9751",
}
]
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))})}
/>
)
render() {
return (
<div className = 'map-container' style=
{{marginleft:'250px'}}>
<div>
<div className = 'sideMenu'>
<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}
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: 'AIzaSyC21SntdNn1vCb5VOAujCPIM7a9p5XkvRs'
})(MapContainer)
Try this, I've created an array inside state to store the filtered out locations. When you type it filters and updates the locations. :)
import React, { Component } from 'react';
import { Map, InfoWindow, Marker, GoogleApiWrapper
} from 'google-maps-react';
var AllPlaces = [
{
"name" : "Pizza",
"lat": "40.7589",
"lng":"-73.9851",
},
{
"name" : "Cookies",
"lat": "40.7690",
"lng":"-73.9952",
},
{
"name" : "Bagels",
"lat": "40.7489",
"lng":"-73.9751",
}
]
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))})}
/>
)
render() {
return (
<div className = 'map-container' style=
{{marginleft:'250px'}}>
<div>
<div className = 'sideMenu'>
<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}
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: 'AIzaSyC21SntdNn1vCb5VOAujCPIM7a9p5XkvRs'
})(MapContainer)
answered Nov 14 '18 at 22:10
Smarticles101Smarticles101
697417
697417
youre saving my life right now haha, I tried in the code but when I type anything it removes all the markers and clears the whole list, this is def a step in the right direction here is the source github.com/JustJin28/react-map-the-one-that-will-pass im going to keep trying at it, I really appreciate the help, if I could take you out for a drink I would
– Hyunjin Kim
Nov 14 '18 at 22:39
Edit, it does show but cant account for capital letters so if I start by typing lowercase it will get rid of all the markers
– Hyunjin Kim
Nov 14 '18 at 22:52
add a comment |
youre saving my life right now haha, I tried in the code but when I type anything it removes all the markers and clears the whole list, this is def a step in the right direction here is the source github.com/JustJin28/react-map-the-one-that-will-pass im going to keep trying at it, I really appreciate the help, if I could take you out for a drink I would
– Hyunjin Kim
Nov 14 '18 at 22:39
Edit, it does show but cant account for capital letters so if I start by typing lowercase it will get rid of all the markers
– Hyunjin Kim
Nov 14 '18 at 22:52
youre saving my life right now haha, I tried in the code but when I type anything it removes all the markers and clears the whole list, this is def a step in the right direction here is the source github.com/JustJin28/react-map-the-one-that-will-pass im going to keep trying at it, I really appreciate the help, if I could take you out for a drink I would
– Hyunjin Kim
Nov 14 '18 at 22:39
youre saving my life right now haha, I tried in the code but when I type anything it removes all the markers and clears the whole list, this is def a step in the right direction here is the source github.com/JustJin28/react-map-the-one-that-will-pass im going to keep trying at it, I really appreciate the help, if I could take you out for a drink I would
– Hyunjin Kim
Nov 14 '18 at 22:39
Edit, it does show but cant account for capital letters so if I start by typing lowercase it will get rid of all the markers
– Hyunjin Kim
Nov 14 '18 at 22:52
Edit, it does show but cant account for capital letters so if I start by typing lowercase it will get rid of all the markers
– Hyunjin Kim
Nov 14 '18 at 22:52
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.
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%2f53309098%2fhow-to-filter-results-and-update-a-google-map%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
Maybe you should hide your api key? I know it's not an answer but just a safety measure.
– squeekyDave
Nov 14 '18 at 21:42