Gaston Velarde
Gaston Velarde

Reputation: 229

Generate a google map link with directions using latitude and longitude

Good day,

I'm trying to create a google map link that will redirect to a map with directions. I will pass the coordinates in the link. Here is my code:

var latDes = this.items[id].LongitudeWD;
var longDes = this.items[id].LatitudeWD;
var url = "https://www.google.com/maps/dir/?api=1";
var origin = "origin=" + tempLatitude + "," + tempLongitude;
var destination = "&destination=" + latDes + "," + longDes;
var newUrl = new URL(url + origin + destination);

var win = window.open(newUrl, '_blank');
 win.focus();

Here is the sample link that it is currently producing https://www.google.com/maps/dir/?api=1origin=34.1030032,-118.41046840000001&destination=-118.368152,34.059808

As you can see it does not have read the coordinates that I'm passing I want something like this

https://www.google.com/maps/dir/?api=1&origin=760+West+Genesee+Street+Syracuse+NY+13204&destination=314+Avery+Avenue+Syracuse+NY+13204

but if possible instead of using the full address can it be with latitude longitude?

Upvotes: 15

Views: 25726

Answers (1)

Pradip
Pradip

Reputation: 516

Seems like you missed '&' just before the 'origin' parameter. Also destination parameter have values, seems like not correct. It should be like

https://www.google.com/maps/dir/?api=1&origin=34.1030032,-118.41046840000001&destination=34.059808,-118.368152

Upvotes: 38

Related Questions