简体   繁体   English

比较 JavaScript 中两个值的值

[英]Comparing values from two values in JavaScript

I have two csv files, one that contains route information, which will be filled with lines of data for each id where by each line contains multiple stations.我有两个 csv 文件,一个包含路线信息,其中将填充每个 id 的数据行,其中每行包含多个站点。 An example一个例子

id,route
abc,stationA:stationB:stationC

so the route field is delimited by : .所以路由字段由:分隔。

the other csv, contains each station and the latitude and longitudes like so:另一个 csv,包含每个站点以及纬度和经度,如下所示:

stationcode,station name, latitude, longitude 
1,stationA, 44.968046, -94.420307

so basically what I want to do is to find out the latitudes and longitudes the user was on on their route.所以基本上我想做的是找出用户在他们的路线上的纬度和经度。 So basically I will read the user's route to have stations A, B and C, and then I want to retrieve the latitude and longitudes from the second csv.所以基本上我会读取用户的路线以拥有 A、B 和 C 站,然后我想从第二个 csv 中检索纬度和经度。

Below is what I have at the moment:以下是我目前所拥有的:

 $(document).ready(function () {
            var markers = []; // define global array in script tag so you can use it in whole page
            var infowindows = [];

            var lat;
            var lng;

            var myCenter = new google.maps.LatLng(1.3000, 103.8000);
            var mapProp = {
                center: myCenter,
                zoom: 6,
                minZoom: 6,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControl: true
            };
            //google map object
            var map = new google.maps.Map(document.getElementById("map"), mapProp);

            //change event of input tag where type=file and  id=filename
            // mrt input
            $("#filename").change(function (e) {

                var ext = $("input#filename").val().split(".").pop().toLowerCase();

                if ($.inArray(ext, ["csv"]) == -1) {
                    alert('Upload CSV');
                    return false;
                }

                if (e.target.files != undefined) {

                    var reader = new FileReader();
                    reader.onload = function (e) {

                        var csvval = e.target.result.split("\n");
                        var csvvalue;
                        var infowindow = new google.maps.InfoWindow();

                        for (var i = 0; i < csvval.length; i++) {
                            markers[i] = [];

                            csvvalue = csvval[i].split(",");
                            markers[i][0] = csvvalue[0]; //id

                            lat = csvvalue[2]; //latitude
                            lng = csvvalue[3]; //longitude
                            var stationname = csvvalue[1];
                            var stationcode = csvvalue[0];


                            markers[i][1] = new google.maps.Marker({
                                position: new google.maps.LatLng(lat, lng),
                                map: map
                            });


                        }

                    };
                    reader.readAsText(e.target.files.item(0));
                }

                return false;

            });

            // actual input csv data

            $("#csvname").change(function (e) {

                var ext = $("input#csvname").val().split(".").pop().toLowerCase();

                if ($.inArray(ext, ["csv"]) == -1) {
                    alert('Upload CSV');
                    return false;
                }

                if (e.target.files != undefined) {

                    var reader = new FileReader();
                    reader.onload = function (e) {

                        var val = e.target.result.split("\n");
                        var value;
                        var infowindow = new google.maps.InfoWindow();

                        for (var i = 0; i < val.length; i++) {
                            value = val[i].split(",");

                            // extract the route information (start and stop station
                            var route = value[1];

                            // split route 
                            var routeStations = route.split(":");

                            // compare station name to get lat and lng


                        }

                    };
                    reader.readAsText(e.target.files.item(0));
                }

                return false;

            });

        });

My problem is that I have no idea how to retrieve the latitude and longitudes from the second csv file (station coordinates) using the station name of from the csv with the route data.我的问题是我不知道如何使用来自 csv 的站名和路线数据从第二个 csv 文件(站坐标)中检索纬度和经度。

As RobG described in the comments:正如 RobG 在评论中所描述的:

When parsing the stations CSV file (ie in the callback of $("#filename").change ), at the point where you initialize the station marker, also record the data in a mapping object.在解析站 CSV 文件时(即在$("#filename").change的回调中),在您初始化站标记的点上,还将数据记录在映射对象中。

Then when parsing your route CSV file (ie in the callback of $("#csvname").change ), simply refer to your mapping object to retrieve the stations coordinates.然后在解析您的路线 CSV 文件时(即在$("#csvname").change的回调中),只需参考您的映射对象即可检索站点坐标。

Result:结果:

// Global variables.
var stationsCoordinates = {};

$("#filename").change(function (e) {

    // some code

    markers[i][1] = new google.maps.Marker(/* bla bla */);

    // I do not know if you need stationname or stationcode
    stationsCoordinates[stationcode] = {
        lat: lat,
        lng: lng
    }

});

$("#csvname").change(function (e) {

    // some code

    var routeStations = route.split(":");

    var station;

    for (var i = 0; i < routeStations.length; i += 1) {
        station = stationsCoordinates[routeStations[i]];

        // Do something with that station.
        // Use station.lat and station.lng
    }

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM