-1

I had developed a google map function in which on call a google map is drawn according to the data is passed

Example:

function DrawMap(data){
   //code to draw google map
}

data was passed in following format

['Bondi Beach', -33.890542, 151.274856],

But data was came from php so by default i am sending data like

var defaultLocation = [
    <?php
        foreach ($db_data as $mvalue) {
            echo "[ '".$mvalue['vTripStatus']."',".$mvalue['vLatitude'].",".$mvalue['vLongitude']." ],";
        } 
    ?>
];

But i have made an event on click of button which performs an ajax call so in ajax the data was returned as string in following format (when i console.log())

[[ 'Active',23.0123022,72.5033961 ],[ 'Arrived',23.0088,72.4823 ],[ 'On Going Trip',22.9938,72.4713 ], ]

so this format isnt accepted by google map so it do not load the map.

i tried $.parseJSON and JSON.parse() too So how i can convert it like

enter image description here

TarangP
  • 2,711
  • 5
  • 20
  • 41

2 Answers2

0

Looks like the values PHP is outputting are strings. Try:

var defaultLocation = JSON.parse(`
    <?php
        $arr = [];
        foreach ($db_data as $mvalue) {
            $arr[] = [ $mvalue['vTripStatus'], $mvalue['vLatitude'], $mvalue['vLongitude'] ];
        }
        echo json_encode($arr);
    ?>
`);
ack_inc
  • 1,015
  • 7
  • 13
0

Ok i read this Can you use a trailing comma in a JSON object?

The problem is single quote and an extra comma which is in

[
  [ 'Active',23.0123022,72.5033961 ],
  [ 'Arrived',23.0088,72.4823 ],
  [ 'On Going Trip',22.9938,72.4713 ],
]

It cause problem while parsing the json data. so if you use an string in json data you must have to compulsary use Double quote(") instead single quote(")

and as well extra words and symbol's are also not allowed in json data.

TarangP
  • 2,711
  • 5
  • 20
  • 41