0
import React, { Component } from "react";
import { Map, GoogleApiWrapper, Marker } from "google-maps-react";
import { MAPS_API_TOKEN, GEOCODE_API_TOKEN } from
"../Enviroments/dev";
import Geocode from "react-geocode";
const mapStyles = {
width: "100%",
height: "100%"
};
var labelIndex = 0;
// var description = "Pão";
function getLabelIndex() {
labelIndex++;
return labelIndex.toString();
}
Geocode.setApiKey(GEOCODE_API_TOKEN);
class SimpleMap extends Component {
constructor(props) {
super(props);
this.state = {
resultado: ""
};
this.getDetail = this.getDetail.bind(this);
}
getMarkers() {
if (typeof this.props.locations !== "undefined") {
return this.props.locations.map(location => (
<Marker
title={this.getDetail(location)}
label={getLabelIndex()}
key={location.time}
position={{
lat: location.latitude,
lng: location.longitude
}}
/>
));
}
}
getDetail(location) {
this.getDescription(location).then(result => {
console.log(result);
return result;
});
}
getDescription(location) {
var address = "Não foi possível carregar o endereço";
Geocode.fromLatLng(location.latitude, location.longitude).then(
status, response => {
if(status === 'OK')
address = response.results[0].formatted_address;
}
);
const formatedTime = this.getFormatedTime(location.time);
const description =
"Data: " +
formatedTime.date +
"\nHora: " +
formatedTime.horary +
"\nEndereço: " +
address;
return description;
}
getFormatedTime(time) {
const yyyy = time.slice(0, 4);
const MM = time.slice(4, 6);
const dd = time.slice(6, 8);
const hh = time.slice(8, 10);
const mm = time.slice(10, 12);
const ss = time.slice(12, 14);
const formatedTime = {
horary: hh + ":" + mm + ":" + ss,
date: dd + "/" + MM + "/" + yyyy
};
return formatedTime;
}
render() {
return (
<div>
<Map
google={this.props.google}
zoom={8}
style={mapStyles}
initialCenter={this.props.zoom_position}
center={this.props.zoom_position}
>
{(labelIndex = 0)}
{this.getMarkers()}
</Map>
</div>
);
}
}
export default GoogleApiWrapper({
apiKey: MAPS_API_TOKEN
})(SimpleMap);
value inside the getDetail function is ok, but where I call it and pass the data, it n shows the value when passing the mouse over the marker. What can be the mistake?
There are a lot of things wrong with this code. First you don’t use then when you use await, it becomes irrelevant. Then you need to have a state, because as it is manipulating many countries it is good that you have a render when the value is changed.So put your entire code, so that we can help. And lastly avoid using
var
.– Anderson Henrique
I put all the code, as you requested
– Emerson Vieira
had put a state to receive the result in getDetail, but whenever I passed the same, fall into a loop
– Emerson Vieira