问题
This what i want, each marker to be sharing the same position as the lat and lng as the heatmap, the markers are working perfectly Im having problems sending data towards my heatmap, it seems like The heatmap from google receives and latitude and longitude differently than the markers positions? any idea ? when i create an array of list of objects like this but i want my data read just like my markers is reading it. It receive M for latitude and N for longitude. If i put a it inside a wrapper like {this.props.policeCall.map(({ A, M, N }) => {
i get an error, when i make my heatmap position into position={{lat: M , lng:N}}
export class MapContainer extends Component {
state = {
showingInfoWindow: false, //Hides or the shows the infoWindow
activeMarker: {}, //Shows the active marker upon click
selectedPlace: {} //Shows the infoWindow to the selected place upon a marker
};
onMarkerClick = (props, marker, e) =>
this.setState({
selectedPlace: props,
activeMarker: marker,
showingInfoWindow: true
});
onClose = props => {
if (this.state.showingInfoWindow) {
this.setState({
showingInfoWindow: false,
activeMarker: null
});
}
};
render() {
return (
<Map
google={this.props.google}
zoom={14}
style={mapStyles}
initialCenter={{
lat: 32.71573699,
lng: -117.16108799
}}
>
{this.props.policeCall.map(({ A, M, N }) => {
return (
<Marker
onClick={this.onMarkerClick}
name={A}
position={{ lat: M, lng: N }}
/>
);
})}
{this.props.policeCall.map(({ A, M, N }) => {
return (
<HeatMap
gradient={gradient}
opacity={3}
position={{lat: M , lng:N}}
radius={30}
/>
);
})}
<InfoWindow
marker={this.state.activeMarker}
visible={this.state.showingInfoWindow}
onClose={this.onClose}
>
<div>
<h4>{this.state.selectedPlace.name}</h4>
</div>
</InfoWindow>
</Map>
);
}
}
This how my data looks from the json it is reading it M and N is the lat and lng how can inject that to my heatmap data
module.exports = [
{"A": "P17060024503", "B": "6/14/2017 21:54", "C": "4", "D": "10", "E": "", "F": "14TH", "G": "ST", "H": "10 14TH ST, San Diego, CA", "I": "1151", "J": "O", "K": "521", "L": "2", "M": "32.7054489", "N": "-117.1518696"},
{ "A": "P17030051227", "B": "3/29/2017 22:24", "C": "4", "D": "10", "E": "", "F": "14TH", "G": "ST", "H": "10 14TH ST, San Diego, CA", "I": "1016", "J": "A", "K": "521", "L": "2", "M": "32.7054544", "N": "-117.1467137"},
{ "A": "P17060004814", "B": "6/3/2017 18:04", "C": "7", "D": "10", "E": "", "F": "14TH", "G": "ST", "H": "10 14TH ST, San Diego, CA", "I": "1016", "J": "A", "K": "521", "L": "2", "M": "32.7053961", "N": "-117.1444185"},
{ "A": "P17030029336", "B": "3/17/2017 10:57", "C": "6", "D": "10", "E": "", "F": "14TH", "G": "ST", "H": "10 14TH ST, San Diego, CA", "I": "1151", "J": "OT", "K": "521", "L": "2", "M": "32.7054244", "N": "-117.1425917"},
{ "A": "P17030005412", "B": "3/3/2017 23:45", "C": "6", "D": "10", "E": "", "F": "15TH", "G": "ST", "H": "10 15TH ST, San Diego, CA", "I": "911P", "J": "CAN", "K": "521", "L": "2", "M": "32.7055067", "N": "-117.1405936"},
回答1:
According to source code of google-maps-react library, HeatMap
component expects positions
mandatory property instead of position
:
<HeatMap
gradient={gradient}
opacity={3}
position={{lat: M , lng:N}}
^^^^^^^^^^^^^^^^^^^^^^^^^^
radius={30}
/>
Given the provided data format, HeatMap
could be initialized like this:
class MapContainer extends React.Component {
render() {
const positions = data.map(item => { return { "lat": item.M, "lng": item.N}});
return (
<div className="map-container">
<Map
google={this.props.google}
className={"map"}
zoom={this.props.zoom}
initialCenter={this.props.center}
>
<HeatMap
gradient={gradient}
positions={positions}
opacity={1}
radius={20}
/>
</Map>
</div>
);
}
}
where data
stores your JSON data
And last but no least, make sure visualization
package is loaded (dependency to Google Heatmaps):
export default GoogleApiWrapper({
apiKey: "--YOUR-KEY-GOES-HERE--",
libraries: ["visualization"]
})(MapContainer);
Here is a demo (the example has been adapted from Google Maps documentation)
来源:https://stackoverflow.com/questions/55107150/react-google-maps-not-displaying-heatmap-lat-and-long-not-working-for-my-posit