I need to find the distance between two places. I have two coordinates, each of them receives its lat and lng. One of the routes is fixed, and I express it as follows:
let gps1 = new google.maps.LatLng(15.5039, -88.0139);
But to get the second coordinate I get it from an object dynamically:
let lat = this.bookingService.bookingModel.loc = attentionSchedule.place.loc.coordinates[0]
let lng = this.bookingService.bookingModel.loc = attentionSchedule.place.loc.coordinates[1]
And then I just "concatenate" or "one" the Lat and Lng creating a new coordinate with the google lib something like this:
let gps2 = new google.maps.LatLng(lng, lat)
Now that I have it gps1
and I gps2
would only have to calculate the distance between both coordinates using the geometry of the google lib to autocalculate
let distanceinMetro = google.maps.geometry.spherical.computeDistanceBetween(gps1, gps2);
console.log('distancia', distanceinMetro);
But I get the following error:
How could I get the distance between both coordinates correctly? Am I looping through the object incorrectly to get the coordinates? I would really appreciate any suggestion or solution please, thanks in advance.
I leave the object that I am looping and from which I get the coordinates from gps2
https://github.com/IzliaB/json/blob/main/coordenadas%20json.json
The error indicates that
geometry
it is not defined, so I think you are not adding the library when loading the google maps api, this is an example of how to add the library