Reputation: 666
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!
Upvotes: 9
Views: 24597
Reputation: 6203
If you are getting the coordinates as a string, in the format you showed in your example, you could do this:
var b=wickedCoords.split(",");
var wickedLocation=new google.maps.LatLng(parseFloat(b[0]), parseFloat(b[1]));
Upvotes: 7
Reputation: 34038
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 );
Upvotes: 16
Reputation: 2196
I believe you can use strings to define the coordinates, but you need two (one for each parameter) not one.
wickedLat = '44.767778';
wickedLon = '-93.2775';
wickedLocation = new google.maps.LatLng(wickedLat, wickedLon);
If not either use floats directly or parse your strings into floats with parseFloat()
Upvotes: 2