-1

I have JSON data:

"geometry":{"type":"Point","coordinates":[95.9174,3.8394,59]},"id":"us10002b0v"

I need to extract each value in coordinates which is comma separated. In PHP I would do extract(",",$geometry[coordinates]);. Is there any possible in JavaScript to do so?

Source Json from here : http://earthquake.usgs.gov/earthquakes/feed/geojsonp/2.5/week

This is my code :

google.maps.event.addDomListener(window, 'load', function() {
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    center: { lat: 7.8, lng: 98.3},
    zoom: 4,
    styles: mapStyle
  });

  map.data.setStyle(styleFeature);

    infowindow = new google.maps.InfoWindow({
        content: '<div class = "corp" style="width: 260px; height: 200px">' + '</div>'
    })


//InfoWindow
var infowindow = new google.maps.InfoWindow();
google.maps.event.addListener(map,'click',function() {
        infowindow.close();
});
map.data.addListener('click', function(event) {
    var place = event.feature.getProperty('place');
    var mag = event.feature.getProperty('mag');
    var depth = event.feature.getProperty('geometry');//I need the depth from this line which is arrayed
    var link = event.feature.getProperty('url');
    var jsonTime = event.feature.getProperty('time');
    var humanTime = new Date('jsonTime');


    infowindow.setContent('<div><h3>'+place+'</h3><p>Mag: '+mag+'<br />Depth '+depth+'<br />Time : '+humanTime+'<br /><a href="'+link+'" target="_blank">More</a></p></div>');
    infowindow.setPosition(event.feature.getGeometry().get());
    infowindow.setOptions({pixelOffset: new google.maps.Size(0,-30)});
    infowindow.open(map);
});
Wilf
  • 2,297
  • 5
  • 38
  • 82

2 Answers2

6

Yes, you can get them as array values:

var my_json = '{"geometry":{"type":"Point","coordinates":[95.9174,3.8394,59]},"id":"us10002b0v"}';
var my_obj = JSON.parse(my_json);
var lat = my_obj.geometry.coordinates[0];
var lng = my_obj.geometry.coordinates[1];
console.log( lat ); //95.9174
console.log( lng ); //3.8394
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
antyrat
  • 27,479
  • 9
  • 75
  • 76
  • 2
    You are starting with a javascript object not with a JSON string. You need to parse the JSON first. – bhspencer May 26 '15 at 14:01
  • @bhspencer it depends on how OP get his JSON. If this is hardcoded in JS file he don't need to call `parse` method. Just add brackets around his code. – antyrat May 26 '15 at 14:03
  • 2
    The OP says they have JSON. There is no JSON in your answer just an object literal. – bhspencer May 26 '15 at 14:03
  • @antyrat I've got : `Uncaught SyntaxError: Unexpected token u`. Please suggest. – Wilf May 26 '15 at 14:06
  • @Wilf here http://jsfiddle.net/y1otpozr/ you will see example of getting all coordinates ( in browser console ) from URL you have provided. – antyrat May 26 '15 at 14:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/78812/discussion-between-antyrat-and-wilf). – antyrat May 26 '15 at 14:29
  • @Wilf I don't see there any arrayed data. Are you sure you use correct JSON? Or try to log just `console.log( event )` maybe your depth data is stored in other property. – antyrat May 26 '15 at 14:42
  • @antyrat the result is the same as `console(event.feature);` Have you checked this original json that I updated? Here => http://earthquake.usgs.gov/earthquakes/feed/geojsonp/2.5/week – Wilf May 26 '15 at 14:46
  • @Wilf yes and there is no problems with that JSON. I've wrote jsFiddle example for you http://jsfiddle.net/y1otpozr/ in console you will see all coords array values. Problem seems to be in other place in your code. – antyrat May 26 '15 at 14:49
  • I nearly pulled all my hair off :( There's no such example on the net :S – Wilf May 26 '15 at 15:05
  • @Wilf move to the chat, I'm waiting you there. – antyrat May 26 '15 at 15:06
2

You could use

var myObject = JSON.parse("<your-json-string>");

This will parse your JSON into a JavaScript object which you can traverse in the usual way.

bhspencer
  • 13,086
  • 5
  • 35
  • 44