-
Notifications
You must be signed in to change notification settings - Fork 2
/
sc.js
119 lines (107 loc) · 3.23 KB
/
sc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// This example creates a 2-pixel-wide red polyline showing the path of William
// Kingsford Smith's first trans-Pacific flight between Oakland, CA, and
// Brisbane, Australia.
var LatLngArray = new google.maps.MVCArray();
var map;
var poly;
var reader;
var apiKey = 'ur_rd_k';
var placeIdArray = [];
var snappedCoordinates = [];
var bounds = new google.maps.LatLngBounds();
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {lat: 0, lng: -180},
mapTypeId: google.maps.MapTypeId.HYBRID
});
poly = new google.maps.Polyline({
strokeColor: '#FF0000',
strokeOpacity: 0,
strokeWeight: 2,
map: map
});
}
function readSingleFile(e) {
var file = e.target.files[0];
if (!file) {
return;
}
reader = new FileReader();
reader.onload = function(e) {
var contents = e.target.result;
displayContents(contents);
};
reader.readAsText(file);
}
function displayContents(content) {
//var el = document.getElementById('main');
//el.innerHTML = reader.responseText;
var fileText = content.split("\n");
for (var i = 0; i < fileText.length -1; i++) {
var split = fileText[i].split("\t");
LatLngArray.push(new google.maps.LatLng(split[0], split[1]));
addLatLng(new google.maps.LatLng(split[0], split[1]),i == 0 || i == fileText.length -2);
bounds.extend(new google.maps.LatLng(split[0], split[1]));
}
for (var i = 0; i <= poly.getPath().length/100 + 1 ; i++) {
var sliced = new google.maps.MVCArray();
for(var j = i*100; j < (i+1) * 100 && j < poly.getPath().length; j++){
sliced.push(poly.getPath().getAt(j));
}
if(sliced.getLength() > 0){
runSnapToRoad(sliced);
}
}
// map.setCenter(new google.maps.LatLng("31.256170","29.978059"));
map.setZoom(12); //constant zoom ratio because laziness
map.fitBounds(bounds);
}
//draws pins and marks start and end
function addLatLng(latlng,bola) {
poly.getPath().push(latlng);
if(bola){
// Add a new marker at the new plotted point on the polyline.
var marker = new google.maps.Marker({
position: latlng,
title: '#' + poly.getPath().getLength(),
map: map
});
}
}
function runSnapToRoad(path) {
var pathValues = [];
for (var i = 0; i < path.getLength(); i++) {
pathValues.push(path.getAt(i).toUrlValue());
}
$.get('https://roads.googleapis.com/v1/snapToRoads', {
interpolate: true,
key: apiKey,
path: pathValues.join('|')
}, function(data) {
processSnapToRoadResponse(data);
drawSnappedPolyline();
// getAndDrawSpeedLimits();
});
}
function processSnapToRoadResponse(data) {
snappedCoordinates = [];
placeIdArray = [];
for (var i = 0; i < data.snappedPoints.length; i++) {
var latlng = new google.maps.LatLng(
data.snappedPoints[i].location.latitude,
data.snappedPoints[i].location.longitude);
snappedCoordinates.push(latlng);
placeIdArray.push(data.snappedPoints[i].placeId);
}
}
function drawSnappedPolyline() {
var snappedPolyline = new google.maps.Polyline({
path: snappedCoordinates,
strokeColor: 'black',
strokeWeight: 3
});
snappedPolyline.setMap(map);
}
$(window).load(initMap);
document.getElementById('file-input').addEventListener('change', readSingleFile, false);