-
Notifications
You must be signed in to change notification settings - Fork 0
/
FinalTest.html
171 lines (156 loc) · 4.16 KB
/
FinalTest.html
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Roads API Demo</title>
<style>
html, body, #map {
height: 100%;
margin: 0px;
padding: 0px
}
#panel {
position: absolute;
top: 5px;
left: 50%;
margin-left: -180px;
z-index: 5;
background-color: #fff;
padding: 5px;
border: 1px solid #999;
}
#bar {
width: 240px;
background-color: rgba(255, 255, 255, 0.75);
margin: 8px;
padding: 4px;
border-radius: 4px;
}
#autoc {
width: 100%;
box-sizing: border-box;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>>
<script
src="https://maps.googleapis.com/maps/api/js?libraries=drawing,places"></script>
<script>
var data = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-73.97056102752686, 40.74811853855757],
[-73.96944522857666, 40.74953279908402],
[-73.96798610687256, 40.75135341202851],
[-73.97109746932983, 40.75314146550602],
[-73.97472381591797, 40.75346656097219],
[-73.97573232650755, 40.75182481261268],
[-73.97721290588379, 40.750768220446936],
[-73.97757768630981, 40.74940275339479]
]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-73.96279335021973, 40.75512452312348],
[-73.96549701690674, 40.75109332751696],
[-73.96974563598633, 40.74719193776603],
[-73.9726209640503, 40.74481848035928],
[-73.97279262542723, 40.74137193935539]
]
}
}, {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-74.00107383728026, 40.75931800754126],
[-73.99519443511963, 40.757270059827206],
[-73.99536609649658, 40.75453936473234],
[-73.99888515472412, 40.75304393655622]
]
}
}]
};
var apiKey = 'Your_Api_key';
var map;
var snappedCoordinates = [];
var coords;
var subArray;
var placeIdArray = [];
var finalArray = [];
function initMap() {
var mapOptions = {
zoom: 13,
center: {
lat: 40.749481,
lng: -73.974290
}
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
map.data.addGeoJson(data);
var lines = data.features;
for (var i = 0; i < lines.length; i++) {
coords = lines[i].geometry.coordinates;
// console.log("initial array: ", coords.toString());
subArray = [coords.toString()];
// console.log('sub array: ', subArray);
runSnapToRoad(subArray);
}
}
function runSnapToRoad(path) {
// var pathValues = [];
// console.log(path);
// for (var i = 0; i < finalArray.length; i++) {
// pathValues.push(finalArray.getAt(i).toUrlValue());
//}
$.get('https://roads.googleapis.com/v1/snapToRoads', {
interpolate: true,
key: apiKey,
path: path.join('|')
}, function(data) {
processSnapToRoadResponse(data);
drawSnappedPolyline();
});
}
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);
polylines.push(snappedPolyline);
}
$(window).load(initMap);
//console.log(JSON.stringify(data, null, 2));
</script>
</head>
<body>
<div id="map"></div>
<div id="bar">
<p class="auto"><input type="text" id="autoc"/></p>
<p><a id="clear" href="#">Click here</a> to clear map.</p>
</div>
</body>
</html>