as a first-time beginner project, I decided to use google maps API to create a simple place input, return distance react project. I found a nice tutorial from youtube where I learned this. However, I run into a problem when I try to store {coordinates.lng} and {coordinates.lat} in a const to use it to calculate distance later using Google maps API. I tried creating its own add function and calling it but it doesn't work. Somewhere my understanding is flawed and I tried to pinpoint it but I really don't understand. How do I store the values of {coordinates.lat} and {coordinates.lng} ?
import React from "react";
import PlacesAutocomplete, {
geocodeByAddress,
getLatLng
} from "react-places-autocomplete";
export default function Itinerary() {
const [address, setAddress] = React.useState("");
const [coordinates, setCoordinates] = React.useState({
lat: null,
lng: null
});
const add = (coordinates) => {
const Latitude= {coordinates.lat};
console.log(Latitude);
};
const handleSelect = async value => {
const results = await geocodeByAddress(value);
const latLng = await getLatLng(results[0]);
setAddress(value);
setCoordinates(latLng);
};
return (
<div className="container">
<div className="row">
<div className="col-md-6 mt-5 mx-auto">
<PlacesAutocomplete
value={address}
onChange={setAddress}
onSelect={handleSelect}
>
{({ getInputProps, suggestions, getSuggestionItemProps, loading }) => (
<div>
<h1>Latitude: **{coordinates.lat}**</h1>
<h1>Longitude: **{coordinates.lng}**</h1>
<h1> {console.log({coordinates.lng}}</h1>
<input size="50" height="40"{...getInputProps({ placeholder: "Type address" })} />
{add(coordinates)}
<div>
{loading ? <div>...loading</div> : null}
{suggestions.map(suggestion => {
const style = {
backgroundColor: suggestion.active ? "#41b6e6" : "#fff"
};
return (
<div {...getSuggestionItemProps(suggestion, { style })}>
{suggestion.description}
</div>
);
})}
</div>
</div>
)}
</PlacesAutocomplete>
</div>
</div>
</div>
);
}