[英]How do import a CSV file into the javascript for a webapp so that i can place address markers on a google maps canvas?
So I am a bit new to javascript and the Google maps API's. 因此,我对javascript和Google Maps API有点陌生。 Currently I can place a marker given a single hard coded address variable called codeAddress.
目前,我可以给一个标记一个给定的硬编码地址变量codeAddress来放置标记。
codeAddress("99362");
function codeAddress(address) {
geocoder.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
The problem is that I need to be able to place multiple markers based on location and need to be able to parse address data from a CSV file or from an sql database. 问题是我需要能够根据位置放置多个标记,并且需要能够从CSV文件或sql数据库解析地址数据。
thanks for the help. 谢谢您的帮助。
I've used the jquery-csv plugin to process csv files. 我使用了jquery-csv插件来处理csv文件。
If you can give more specifics on where this csv file is coming from, and how it will be loaded, I can give info on how you can convert this into a javascript array. 如果您可以提供有关此csv文件的来源以及如何加载的更多详细信息,请提供有关如何将其转换为javascript数组的信息。 Will this file be on the server already?
该文件已经在服务器上了吗? If so, then it would make more sense to do some server side processing to read the file.
如果是这样,那么进行一些服务器端处理来读取文件将更有意义。
The following shows how to modify your code to work with an array of multiple addresses: 下面显示了如何修改代码以使用多个地址的数组:
var addresses = ["99362", "01010", "12345"];
jQuery.each(addresses, function(index, address) {
geocoder.geocode({
'address': address
}, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
// we only need to center the map the first time
if (index == 0)
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.