Using Javascript to extract the Latitude and Longitude from a kml field in a Google fusion table

1k Views Asked by At

I have a google fusion table that I am inserting data into that contains kml. I need to query (or extract) the latitude and longitude from the kml column. When I query the table I get data returned in this format:

{
"kind": "fusiontables#sqlresponse",
"columns": [
"description",
"name",
"geometry"
],
"rows": [
  [
 "\u003cimg alt=\"The North Fields\" class=\" tall\" src=\"https://d15mj6e6qmt1na.cloudfront.net/files/images/1501/0978/The_North_Fields_small.JPG\" style=\"float:left; padding: 0 3px 3px\" /\u003e\n            by ctipp\u003cbr/\u003e\n            \u003ca href=\"http://audioboom.com/boos/3260713-coastal-lagoon\"\u003eVisit on audioboom.com\u003c/a\u003e\n            \u003chr style=\"clear:both\"/\u003e",
 "Coastal Lagoon",
 {
  "geometry": {
 "type": "Point",
 "coordinates": [
  -0.749484,
  50.7627,
  0.0
 ]
}
}
]
]
}

the above data is read into a javascript variable using a callback function and i need to know the correct syntax for extracting the latitude and longitude (ie -0.749484, 50.7627)

I've got this far:

success: function(data) {
        var rows = data['rows'];
        var desc = rows[0][0];
        var name = rows[0][1];

but I'm stuck on the geometry field...

1

There are 1 best solutions below

0
On BEST ANSWER
success: function(data) {
  var rows = data['rows'];
  var desc = rows[0][0];
  var name = rows[0][1];
  var latitude = rows[0][2].geometry.coordinates[1]; // KML is longitude, latitude
  var longitude = rows[0][2].geometry.coordinates[0];

var data = {
  "kind": "fusiontables#sqlresponse",
  "columns": [
    "description",
    "name",
    "geometry"
  ],
  "rows": [
    [
      "\u003cimg alt=\"The North Fields\" class=\" tall\" src=\"https://d15mj6e6qmt1na.cloudfront.net/files/images/1501/0978/The_North_Fields_small.JPG\" style=\"float:left; padding: 0 3px 3px\" /\u003e\n            by ctipp\u003cbr/\u003e\n            \u003ca href=\"http://audioboom.com/boos/3260713-coastal-lagoon\"\u003eVisit on audioboom.com\u003c/a\u003e\n            \u003chr style=\"clear:both\"/\u003e",
      "Coastal Lagoon", {
        "geometry": {
          "type": "Point",
          "coordinates": [-0.749484,50.7627,0.0]
        }
      }
    ]
  ]
};

function initialize() {
  var infowindow = new google.maps.InfoWindow();
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 16,
    center: {
      lat: 50.7627,
      lng: -0.749484
    }
  });
  var latLng = new google.maps.LatLng(data.rows[0][2].geometry.coordinates[1],
    data.rows[0][2].geometry.coordinates[0]);
  var marker = new google.maps.Marker({
    position: latLng,
    map: map
  });
  google.maps.event.addListener(marker, 'click', function(evt) {
    infowindow.setContent("name: " + data.rows[0][1] + "<br>desc: " + data.rows[0][0]);
    infowindow.open(map, marker);
  });
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map {
  height: 100%;
  width: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>