I have created a function that loops through a set of returned JSON. I then need to display the latlong data pulled from JSON inside google.maps.LatLng(). Thing is when I try to do this I just get null returned and I can’t figure out why.
Here is my js function
function addNew(json) {
if(json.length > 0) {
for(var i = 0; i < json.length; i++) {
var callback = function(object) {
return function(results, status) {
var location = new google.maps.LatLng(object.latlong);
var image = '/img/markers/' + object.energyrating + '.png';
var marker = new google.maps.Marker({
map: map,
position: location,
icon: image
});
};
}(json[i]);
geocoder.geocode({ 'address': address }, callback);
}
} else {
alert("Sorry, no data was found.");
}
};
However if I just return object.latlong on it’s own it will work fine. It just doesn’t like when it’s inside LatLng(). Is it because it has already been set before the JSON is loaded in?
So your object.latlong looks like ‘51.5381031, 0.0495182’, which is a string. You need to pass through two numbers to the google.maps.LatLng() function. It should be simple to do this, I’d suggest use .split() to turn it into an array of two elements.