Google地图商店定位器未解析CSV

时间:2014-12-04 09:12:40

标签: javascript google-maps csv

我正在尝试修改Google商店定位器的示例,可在此处找到: https://googlemaps.github.io/js-store-locator/examples/panel.html。 但无论我尝试什么,它似乎都无法解析CSV。 我在Google Maps Store Locator - modify default example已阅读此问题,但我的CSV根本没有修改过。我甚至没有打开它来编辑它。为了清楚起见我没有修改 - 更改示例中的任何文件,我只是想看看它是如何工作的。可能是我的问题?到目前为止我已尝试过:

  • 下载解压缩并运行panel.html
  • 在不解压缩的情况下下载运行
  • 下载到不同的PC(没有excel安装)
  • 从xampp
  • 运行它
  • 上传到网络服务器

我还能尝试什么?我真的需要它工作,所以我可以修改它并在其上构建我的项目的一部分。 提前致谢

1 个答案:

答案 0 :(得分:0)

这是代码示例(来自此blog),它应该为您提供指导。由于您还没有发布您的代码段,因此很难说出它究竟是什么问题。

或者,您可以尝试使用此library。无论哪个有帮助。



<script type="text/javascript">
    // ref: http://stackoverflow.com/a/1293163/2343
    // This will parse a delimited string into an array of
    // arrays. The default delimiter is the comma, but this
    // can be overriden in the second argument.
    function CSVToArray( strData, strDelimiter ){
        // Check to see if the delimiter is defined. If not,
        // then default to comma.
        strDelimiter = (strDelimiter || ",");

        // Create a regular expression to parse the CSV values.
        var objPattern = new RegExp(
            (
                // Delimiters.
                "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

                // Quoted fields.
                "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

                // Standard fields.
                "([^\"\\" + strDelimiter + "\\r\\n]*))"
            ),
            "gi"
            );


        // Create an array to hold our data. Give the array
        // a default empty first row.
        var arrData = [[]];

        // Create an array to hold our individual pattern
        // matching groups.
        var arrMatches = null;


        // Keep looping over the regular expression matches
        // until we can no longer find a match.
        while (arrMatches = objPattern.exec( strData )){

            // Get the delimiter that was found.
            var strMatchedDelimiter = arrMatches[ 1 ];

            // Check to see if the given delimiter has a length
            // (is not the start of string) and if it matches
            // field delimiter. If id does not, then we know
            // that this delimiter is a row delimiter.
            if (
                strMatchedDelimiter.length &&
                strMatchedDelimiter !== strDelimiter
                ){

                // Since we have reached a new row of data,
                // add an empty row to our data array.
                arrData.push( [] );

            }

            var strMatchedValue;

            // Now that we have our delimiter out of the way,
            // let's check to see which kind of value we
            // captured (quoted or unquoted).
            if (arrMatches[ 2 ]){

                // We found a quoted value. When we capture
                // this value, unescape any double quotes.
                strMatchedValue = arrMatches[ 2 ].replace(
                    new RegExp( "\"\"", "g" ),
                    "\""
                    );

            } else {

                // We found a non-quoted value.
                strMatchedValue = arrMatches[ 3 ];

            }


            // Now that we have our value string, let's add
            // it to the data array.
            arrData[ arrData.length - 1 ].push( strMatchedValue );
        }

        // Return the parsed data.
        return( arrData );
    }

</script>
&#13;
&#13;
&#13;