emil.c
emil.c

Reputation: 2018

Unexpected LatLng set in google maps

I have this line of code in my javascript:

console.log(google.maps.LatLng(49.220341,17.649555));

And console returns object with google maps Lat and Lng, which is what I expected, but:

Then I'm trying to do this:

var start = "49.220341,17.649555";
console.log(google.maps.LatLng(start));

But console returns empty object with NaN values.

Upvotes: 0

Views: 515

Answers (2)

Dave Lasley
Dave Lasley

Reputation: 6252

You are passing a string of the lat/lng pair, but google.maps.LatLng expects two strings representing the pair. You will need to use two variables:

var lat = 49.220341;
var lng = 17.649555;
console.log(google.maps.LatLng( lat , lng ));

or an array:

var start = [49.220341,17.649555];
console.log(google.maps.LatLng(start[0],start[1]));

I prefer the use of an object, however:

var start = ['lat':49.220341,
             'lng':17.649555];
console.log(google.maps.LatLng(start['lat'],start['lng']));

Upvotes: 0

mattsbox
mattsbox

Reputation: 91

Dave is right, the function will only take two arguments, not one. If you want to store them in a string like that, you could always call

var start = "49.220341,17.649555";
console.log(google.maps.LatLng(start.substring(0,start.indexOf(",")),start.substring(start.indexOf(",")+1)));

or

var start = "49.220341,17.649555";
var startArr=start.split(",");
console.log(google.maps.LatLng(startArr[0],startArr[1]));

Both of these separate the string around the comma.

Upvotes: 1

Related Questions