如何使用谷歌地图API与Greasemonkey的读取地址表,跟踪路线? [英] how to use the google maps api with greasemonkey to read a table of addresses and trace the route?
问题描述
有一个网站,持有巴士路线我的城市(阿雷格里港 - 巴西)在表
There is a website which holds the buses routes for my city (Porto Alegre - Brazil) in a table.
例如:<一href=\"http://www.eptc.com.br/EPTC_Itinerarios/Cadastro.asp?Linha=255-0&Tipo=I&Veiculo=1&Sentido=0&Logradouro=0&Action=Itiner%E1rio\"相对=nofollow> EPTC
使用 Greasemonkey的加载德的谷歌地图API ,我还想展示在screeen的右上角一个固定的地图。
Using Greasemonkey to load de Google Maps API, id like to show a fixed map at the top right corner of the screeen.
该脚本必须从表中带去多余的空格阅读街名(有很多人),并绘制在正确的顺序的路线。
The script must read the street names from the table, strip de extra spaces (there are a lot of them), and draw the route in the right order.
推荐答案
下面是Greasemonkey的兼容脚本来完成任务:的 http://userscripts.org/scripts/show/116339
Here is the Greasemonkey compatible script to accomplish the task: http://userscripts.org/scripts/show/116339
要点:
-1-通过每个小区找到表和回路获取内容搜索
2-去除所有多余的空格和保存文本在一个数组结果
3创建两个div,一个接一个(否则位置将不是固定的)结果里面
4-的DIV追加到该页面并调用API结果
5谷歌必须用unsafeWindow(谷歌= unsafeWindow.google
)
1- find the table and loop through each cell to get the content
2- strip all the extra white spaces and save the text in an array
3- create two DIVs, one inside the other (otherwise the position will not be fixed)
4- append the DIVs to the page and call the API
5- "google" must be called with unsafeWindow (google = unsafeWindow.google
)
API_js_callback = "http://maps.google.com/maps/api/js?sensor=false®ion=BR&callback=initialize";
var script = document.createElement('script');
script.src = API_js_callback;
var head = document.getElementsByTagName("head")[0];
(head || document.body).appendChild(script);
if (document.getElementsByTagName('TABLE')[0] != null) {
var Tabela_1 = document.getElementsByTagName('TABLE')[0];
var Tabela_1_cel = Tabela_1.getElementsByTagName('TD');
var Tabela_1_lin = Tabela_1.getElementsByTagName('TR');
}
if (document.getElementsByTagName('TABLE')[1] != null) {
var Tabela_2 = document.getElementsByTagName('TABLE')[1];
var Tabela_2_cel = Tabela_2.getElementsByTagName('TD');
var Tabela_2_lin = Tabela_2.getElementsByTagName('TR');
}
var DIVmapa = document.createElement('div');
DIVmapa.id = 'DIVmapa';
DIVmapa.style.border = '2px coral solid';
DIVmapa.style.cursor = 'pointer';
DIVmapa.style.display = '';
DIVmapa.style.height = '75%';
DIVmapa.style.margin = '1';
DIVmapa.style.position = 'fixed';
DIVmapa.style.padding = '1';
DIVmapa.style.right = '1%';
DIVmapa.style.top = '1%';
DIVmapa.style.width = '30%';
DIVmapa.style.zIndex = '99';
var DIVinterna = document.createElement('div');
DIVinterna.id = 'DIVinterna';
DIVinterna.style.height = '100%';
DIVinterna.style.width = '100%';
DIVinterna.style.zIndex = '999';
if (Tabela_1 || Tabela_2) {
document.body.appendChild(DIVmapa);
DIVmapa.appendChild(DIVinterna);
}
initialize = setTimeout(function () {
google = unsafeWindow.google;
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
var PortoAlegre = new google.maps.LatLng(-30.034176,-51.229212);
var myOptions = {
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: PortoAlegre
}
map = new google.maps.Map(document.getElementById("DIVinterna"), myOptions);
directionsDisplay.setMap(map);
function calcRoute() {
var start = Tabela_1_cel[1].getElementsByTagName('B')[0].innerHTML.replace(/\s{2,}/g, ' ') + ' Porto Alegre';
var end = Tabela_1_cel[10].getElementsByTagName('B')[0].innerHTML.replace(/\s{2,}/g, ' ') + ' Porto Alegre';
var waypts = [];
//for (var i=1; i<Tabela_1_cel.length; i++) {
for (var i=2; i<10; i++) {
ponto_1 = Tabela_1_cel[i].getElementsByTagName('B')[0].innerHTML;
semespacos_1 = ponto_1.replace(/\s{2,}/g, ' ') + ' Porto Alegre';
waypts.push({location: semespacos_1, stopover: true});
}
var request = {
origin: start,
destination: end,
waypoints: waypts,
optimizeWaypoints: false,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {directionsDisplay.setDirections(response)};
//alert(status);
});
}
calcRoute();
}, 1000);
尽情享受吧! :)
Enjoy! :)
这篇关于如何使用谷歌地图API与Greasemonkey的读取地址表,跟踪路线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!