This repository has been archived by the owner on Jan 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.js
108 lines (85 loc) · 3.05 KB
/
search.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
/*** Globals ***/
var points = [];
var calloutDelegate = {
calloutContentForAnnotation: function(annotation) {
var element = document.createElement("div");
element.className = "mk-standard";
var title = element.appendChild(document.createElement("div"));
title.className = "mk-title";
title.textContent = annotation.title;
var subtitle = element.appendChild(document.createElement("div"));
subtitle.className = "mk-subtitle";
subtitle.textContent = annotation.subtitle;
return element;
}
};
function addPoints(err, results) {
var index;
for(index = 0; index < results.places.length; index++) {
var pOptions = {
title: results.places[index].name,
subtitle: results.places[index].formattedAddress,
callout: calloutDelegate
};
var p = new mapkit.PinAnnotation(results.places[index].coordinate, pOptions);
//map.addAnnotation(p);
points.push(p);
}
//map.addAnnotations(points);
}
function goToOnMap(loc) {
console.log(loc);
var pinOptions = {
title: loc.item.label,
subtitle: loc.item.address,
selected: true,
callout: calloutDelegate
};
var pin = new mapkit.PinAnnotation(loc.item.coordinate, pinOptions);
map.setCenterAnimated(loc.item.coordinate);
map.addAnnotation(pin);
}
function autoComplete(err, results) {
var index;
var places = [];
console.log(results);
for(index = 0; index < results.results.length; index++) {
var item = {
label: results.results[index].displayLines[0],
address: results.results[index].displayLines[1],
coordinate: (results.results[index].coordinate != null ? results.results[index].coordinate : null)
};
places.push(item);
}
suggest(places);
}
function suggest(places) {
console.log("Got suggestions.");
$("#searchBox").autocomplete({
source: places,
select: function(event, ui) {
map.removeAnnotations(map.annotations);
goToOnMap(ui);
}
})
.autocomplete("instance")._renderItem = function (ul, item) {
console.log(item);
return $("<li></li>")
.append("<a><span class=\"autocompleteLabel\">" +
item.label +
"</span><br><span class=\"autocompleteSubtitle\">" +
(item.address != null ? item.address : "") +
"</span></a>")
.appendTo(ul);
};
}
var s = new mapkit.Search();
document.getElementById("searchBox").addEventListener("input", function() {
s.coordinate = map.center;
s.region = map.region;
if (document.getElementById("searchBox").value != "")
s.autocomplete(document.getElementById("searchBox").value, autoComplete);
});
//s.coordinate = new mapkit.Coordinate(37.331707, -122.029583);
//s.region = new mapkit.CoordinateRegion(new mapkit.Coordinate(37.331707, -122.029583), new mapkit.CoordinateSpan(0.01, 0.01));
//s.search("food", addPoints);