如何将文本文件的内容加载到javascript变量中?

如何将文本文件的内容加载到javascript变量中?,javascript,Javascript,我正在尝试使用谷歌地图与位置脚本。我需要的是使位置从.txt文件加载。我需要得到这个 googleMapGenerator.options.locations=[…locations.txt] 要从中提取:locations.txt 下面是一段演示代码: 以下是源代码: 沙文 <script src="googlemaps.min.js"></script> <script> /** * Plugin con

我正在尝试使用谷歌地图与位置脚本。我需要的是使位置从.txt文件加载。我需要得到这个

googleMapGenerator.options.locations=[…locations.txt]

要从中提取:locations.txt

下面是一段演示代码:

以下是源代码:

沙文

    <script src="googlemaps.min.js"></script>
    <script>
        /**
         *  Plugin config and init
         */

        // Config
            googleMapGenerator.options.mapLat = 40.77;
            googleMapGenerator.options.mapLng = -73.98;
            googleMapGenerator.options.mapZoom = 12;
            googleMapGenerator.options.markerIconType = 'numeric';
        googleMapGenerator.options.markerIconHexBackground = 'ff6600';
        googleMapGenerator.options.markerIconHexColor = '000000';
        googleMapGenerator.options.hasPrint = false;
        googleMapGenerator.options.locations = [
          ['Central Park', 'New York, NY', 'Central Park is an urban park in the central part of the borough of Manhattan, New York City . It was initially opened in 1857, on 778 acres of city-owned land.', 40.783121, -73.965366, 1],
          ['Times Square', 'Manhattan, NY 10036', 'Times Square is a major commercial intersection and a neighborhood in Midtown Manhattan, New York City, at the junction of Broadway and Seventh Avenue and stretching from West 42nd to West 47th Streets.', 40.759159, -73.985131, 2],
          ['Empire State Building', '350 5th Ave, New York, NY 10118', 'The Empire State Building is a 103-story skyscraper located in Midtown Manhattan, New York City, at the intersection of Fifth Avenue and West 34th Street.', 40.748704, -73.985707, 3],
          ['Metropolitan Museum of Art', '1000 5th Ave, New York, NY 10028', 'The Metropolitan Museum of Art, located in New York City, is the largest art museum in the United States and one of the ten largest in the world.', 40.779701, -73.963255, 4],
          ['Rockefeller Center', '45 Rockefeller Plaza, New York, NY 10111', 'Rockefeller Center is a complex of 19 commercial buildings covering 22 acres between 48th and 51st streets in New York City, United States.', 40.758980, -73.978685, 5],
          ['Museum of Modern Art', '11 W 53rd St, New York, NY 10019', 'The Museum of Modern Art is an art museum located in Midtown Manhattan in New York City between Fifth and Sixth Avenues.', 40.761656, -73.977601, 6]
        ];

/**
*插件配置和初始化
*/
//配置
googleMapGenerator.options.mapLat=40.77;
googleMapGenerator.options.mapng=-73.98;
googleMapGenerator.options.mapZoom=12;
googleMapGenerator.options.markerIconType='numeric';
googleMapGenerator.options.markerIconHexBackground='ff6600';
googleMapGenerator.options.markerIconHexColor='000000';
googleMapGenerator.options.hasPrint=false;
googleMapGenerator.options.locations=[
[“中央公园”、“纽约州纽约市”、“中央公园”是纽约市曼哈顿区中部的一个城市公园。它最初于1857年开放,占地778英亩,为城市所有土地。”,40.783121,-73.965366,1],
[“时代广场”、“纽约曼哈顿10036”、“时代广场是纽约市曼哈顿中城的一个主要商业交叉口和一个街区,位于百老汇和第七大道的交界处,从西42街延伸到西47街。”,40.759159,-73.985131,2],
[“帝国大厦”,“纽约州纽约市第五大道350号,邮编:10118”,“帝国大厦是一座103层的摩天大楼,位于纽约市曼哈顿中城,第五大道与西34街的交叉口”,40.748704,-73.985707,3],
[“大都会艺术博物馆”,纽约州纽约市第五大道1000号,纽约10028,“大都会艺术博物馆,位于纽约市,是美国最大的艺术博物馆,也是世界十大艺术博物馆之一”,40.779701,-73.963255,4],
[“洛克菲勒中心”,“纽约州纽约市洛克菲勒广场45号,纽约州10111”,“洛克菲勒中心是由19座商业建筑组成的综合体,占地22英亩,位于美国纽约市第48街和第51街之间。”40.758980,-73.978685,5],
[“现代艺术博物馆”,纽约州纽约市西53街11号,邮编:10019”,“现代艺术博物馆是一家艺术博物馆,位于纽约市曼哈顿中城第五大道和第六大道之间”,40.761656,-73.977601,6]
];

以下是我编写的一个示例,介绍如何使用Javascript加载文件:

下面的函数将完成此操作。第一个参数是要读取的文件,第二个参数是完成后要执行的函数。这不能直接返回值,因为文件是异步读取的,这意味着实际的getFileContents脚本将在加载文件之前执行并终止。当读取器读取完文件,然后执行第二个参数func中指定的函数时,设置事件侦听器。在该函数中,您可以获取文件内容并将其交给变量。确保提供的函数具有文件字符串内容的参数

/**
* This will read a file, then execute a function with the contents
* @param file to read
* @param function to execute upon load
*/
function getFileContents(file,func) {
    var reader = new FileReader();
    var contents;
    reader.onload = function(e) {
        func(reader.result);
    }
    reader.readAsText(file);
}

实际上这是一些代码,但没有显示您尝试过的代码。你能给我们提供一个剪下的-那么它更容易帮助你。可能重复谢谢你,这里是工作演示:所以你设法解决它自己?