return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - Get data from KML file using node.js - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get data from KML file using node.js - Stack Overflow

programmeradmin1浏览0评论

Is it possible to get location data from KML file based on the latitude and longitude? When I am using npm node-geocoder that time I am getting a result which Google provided. But here I am having KML file from this file I need to get the result. Please guide me to get a result from KML file.

Below: the code I am using getting data from geocoder API.

var NodeGeocoder = require('node-geocoder');
var options = {
  provider: 'google',
  // Optional depending on the providers
  httpAdapter: 'https',
  formatter: 'json'
};

var geocoder = NodeGeocoder(options);

var kmllatitude =req.body.latitude;
var kmllong =req.body.longitude;


geocoder.reverse({lat:kmllatitude, lon:kmllong}, function(err, res) {
    console.log(err,"!!!!!!!!");
    console.log(res,"####");
});

Is it possible to get location data from KML file based on the latitude and longitude? When I am using npm node-geocoder that time I am getting a result which Google provided. But here I am having KML file from this file I need to get the result. Please guide me to get a result from KML file.

Below: the code I am using getting data from geocoder API.

var NodeGeocoder = require('node-geocoder');
var options = {
  provider: 'google',
  // Optional depending on the providers
  httpAdapter: 'https',
  formatter: 'json'
};

var geocoder = NodeGeocoder(options);

var kmllatitude =req.body.latitude;
var kmllong =req.body.longitude;


geocoder.reverse({lat:kmllatitude, lon:kmllong}, function(err, res) {
    console.log(err,"!!!!!!!!");
    console.log(res,"####");
});
Share Improve this question edited Oct 18, 2017 at 6:21 wscourge 11.3k17 gold badges63 silver badges86 bronze badges asked Oct 18, 2017 at 5:47 user3239284user3239284 711 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I'm supposing you downloaded the KML file from your Google Location History.

Since KML uses a tag-based structure with nested elements and attributes, based on the XML standard, you can use the read-xml package to obtain the data from your KML file.

This is what your KML file should be like:

<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis/kml/2.2' xmlns:gx='http://www.google./kml/ext/2.2'>
    <Document>
        <Placemark>
            <open>1</open>
            <gx:Track>
                <altitudeMode>clampToGround</altitudeMode>
                <when>2018-01-18T23:48:28Z</when>
                <gx:coord>-16.9800841 32.6660673 0</gx:coord>
                <when>2018-01-18T23:45:06Z</when>
                            ...
                <when>2013-12-05T09:03:41Z</when>
                <gx:coord>-16.9251961 32.6586912 0</gx:coord>
            </gx:Track>
        </Placemark>
    </Document>
</kml>

Then I convert the XML text to Javascript object/JSON text. You don't have to do this step, but for me, it's easier to do and to explain. You can do that by using the xml-js package.

Another thing you have to do is to split the value of this tag <gx:coord>-16.9251961 32.6586912 0</gx:coord> since you have first the longitude and then the latitude, inside the same tag.

var fs = require('fs'),
    path = require('path'),
    xmlReader = require('read-xml');

var convert = require('xml-js');

// If your file is located in a different directory than this javascript 
// file, just change the directory path.
var FILE = path.join(__dirname, './history.kml'); 

xmlReader.readXML(fs.readFileSync(FILE), function(err, data) {
    if (err) {
        console.error(err);
    }

    var xml = data.content;
    var result = JSON.parse(convert.xml2json(xml, {pact: true, spaces: 4}));

      // If your KML file is different than the one I provided just change 
      // result.kml.Document.Placemark['gx:Track']['gx:coord'].
      // As you can see it is similar with the KML file provided.
      for(var i = 0; i < result.kml.Document.Placemark['gx:Track']['gx:coord'].length; i++){
         var results = result.kml.Document.Placemark['gx:Track']['gx:coord'][i]._text;

         // As I said before you have to split the returned value.
         var coordinates = results.split(" ");
         var longitude = coordinates[0];
         var latitude = coordinates[1];
         console.log("lat/long: " + latitude + ", " + longitude);
      }
});

Hope it could help you!

发布评论

评论列表(0)

  1. 暂无评论