我试图使用谷歌地图与位置脚本。我需要的是从.txt文件中加载位置。我需要得到这个
googleMapGenerator.options.locations = [... locations.txt]
从:locations.txt
中提取以下是带有工作演示的片段:https://codepen.io/clintabrown/pen/jEOmZK
以下是源代码:
Javscript
<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]
];
答案 0 :(得分:0)
以下是我如何使用Javascript加载文件的示例:https://jsfiddle.net/BrandonQDixon/dhvjz5wp/
以下功能将完成此操作。第一个参数是要读取的文件,第二个参数是完成时要执行的函数。这不能直接返回值,因为文件是异步读取的,这意味着实际的 getFileContents 脚本将在文件加载之前执行并终止。这为读者读完文件时设置了一个事件监听器,然后执行你在第二个参数 func 中指定的函数。在该函数中,您可以获取文件内容并将其提供给变量。确保您提供的函数具有文件的String内容的参数。
/**
* 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);
}