LazyPeon
LazyPeon

Reputation: 339

Gmaps LatLng doesn't read my data

Everything should be fine but its not. No console errors, nothing. Result of my object is displayed correctly in console and my marker is set up correctly if I manually type in coordinates.

However if I pass the data to LatLng with my object, zero results.

This in console returns "46.00,45.00":

var data = jQuery.parseJSON(data);
console.log(data[0].coords);

and this sets up my marker for the Google maps:

var latLng = new google.maps.LatLng(46.00,45.00);
var marker = new google.maps.Marker({
    position: latLng,
    map: map
});

BUT! If I write it like this nothing happens:

var data = jQuery.parseJSON(data);

var latLng = new google.maps.LatLng((data[0].coords));
var marker = new google.maps.Marker({
    position: latLng,
    map: map
});

Upvotes: 1

Views: 84

Answers (2)

GNi33
GNi33

Reputation: 4509

You're just passing one argument with data[0].coords as string, when you need to pass 2 arguments as numbers, as specified in the documentation:

LatLng(lat:number, lng:number, noWrap?:boolean)

You'll need to split up the coords variable before:

var coordinates = data[0].coords.split(",");
var latLng = new google.maps.LatLng(coordinates[0], coordinates[1]);

Upvotes: 5

Naeem Shaikh
Naeem Shaikh

Reputation: 15725

You are passing data.coords[0]object completely to new google.maps.LatLng

You should split the lat and lon and pass it as below.

var data = jQuery.parseJSON(data);

var latLng = new google.maps.LatLng(data[0].coords[0],data[0].coords[1]);
var marker = new google.maps.Marker({
    position: latLng,
    map: map
});

Upvotes: 1

Related Questions