0

My JSON is:

var json = '{"name":"GeoFence","coordinate":[[1,2],[3,4],[5,6]]}',
obj = JSON.parse(json);
alert(obj.coordinate);

But i need to set the values of coordinates as follows:

var myTrip=[new google.maps.LatLng(1,2),new google.maps.LatLng(3,4),new google.maps.LatLng(5,6)];
1
  • Do you expect myTrip == obj.coordinate? Commented Jan 31, 2014 at 10:45

3 Answers 3

2

Traverse the coordinates and create a new object for each, and add that to myTrip:

var myTrip = [];

for(var i=0; i < obj.coordinate.length; i++) {
    myTrip.push(new google.maps.LatLng(obj.coordinate[i][0],obj.coordinate[i][1]))
}
Sign up to request clarification or add additional context in comments.

Comments

1

How about this:

var json = '{"name":"GeoFence","coordinate":[[1,2],[3,4],[5,6]]}',
var obj = JSON.parse(json);
var myTrip = [];
for (var i = 0; i < obj.coordinate.length; i++) {
    myTrip[i] = new google.maps.LatLng(obj.coordinate[i][0], obj.coordinate[i][1]);
}

Comments

1

You need to iterate over the collection of the coordinates:

var myTrip = getCoordinate ();

var getCoordinate = function(){
        var ret = [];

        for(var i = 0, length = coordinate.length; i < length; i++){
           ret.push(new google.maps.LatLng(coordinate[i][0], coordinate[i][1]))
        }

       return ret;
}

1 Comment

Code without explanation is not an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.