Passing location coordinates to google maps as variable

google-maps, google-maps-api-3, javascript

Solution

In this example, you are passing two distinct numerical values into a constructor and then assigning the newly created object to wickedLocation:

var wickedLocation =  new google.maps.LatLng(44.767778, -93.2775);

In this example, you're passing a single string value into a constructor that requires two distinct numerical coordinates:

var wickedCoords = "44.767778, -93.2775";
var wickedLocation =  new google.maps.LatLng(wickedCoords);

The data types are both completely different.

With that said, if you want to represent a coordinate as a single object, you can do so like this:

var myHome = { "lat" : "44.767778" , "long" : "-93.2775" };

var yourHome = { "lat" : "23,454545" , "long" : "-92.12121" };

Then when you need to create the coords object from Google, you can pass the data in as individual arguments derived from a single object:

var wickedLocation =  new google.maps.LatLng( myHome.lat, myHome.long );

Problem

Anyone know why this will work: ``` var wickedLocation = new google.maps.LatLng(44.767778, -93.2775); ``` But this won't: ``` var wickedCoords = "44.767778, -93.2775"; var wickedLocation = new google.maps.LatLng(wickedCoords); ``` I tried passing the latitude and longitude as separate variables and that didn't do the trick either. How can I pass the coordinates successfully as a variable? Thanks!

Original source