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
|
import { algoliasearch } from "algoliasearch";
import { info } from "autoprefixer";
import instantsearch from "instantsearch.js";
import { searchBox, hits, configure } from "instantsearch.js/es/widgets";
const MAPS_KEY = "AIzaSyCtS6CiOrG95FhroSUdDJJokItndcMkrgc";
const ALGOLIA_KEY = "9fb3db0222f7b5aef0e2b30791ee6201";
const INDEX_NAME = "pubfinder";
const client = algoliasearch("YSWWVAX5RB", ALGOLIA_KEY);
let markers = [];
const algolia_params = {
hitsPerPage: 1000,
};
// const search = instantsearch({
// indexName: INDEX_NAME,
// searchClient,
// // onStateChange({ uiState, setUiState }) {
// // setUiState(uiState);
// // },
// // probably want to set initial search state depending on the rectangle coords
// });
// cons
// search.addWidgets([
// searchBox({
// container: "#searchbox",
// }),
// hits({
// container: "#hits",
// }),
// configure({}),
// ]);
let map;
const initialPolygonCoords = [
{ lat: 51.555519, lng: -0.215208 },
{ lat: 51.526021, lng: -0.021665 },
{ lat: 51.454394, lng: -0.014601 },
{ lat: 51.425103, lng: -0.145876 },
{ lat: 51.514498, lng: -0.257455 },
];
const london_bounds = {
north: 51.532,
south: 51.478,
east: -0.072,
west: -0.16,
};
function removeMarkers() {
console.log("hi");
for (let i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
}
async function initMap() {
const position = { lat: 51.508616, lng: -0.125319 };
const { Map, Polygon, Rectangle, InfoWindow } =
await google.maps.importLibrary("maps");
const { AdvancedMarkerElement } = await google.maps.importLibrary("marker");
map = new Map(document.getElementById("map"), {
zoom: 4,
center: position,
mapId: "DEMO_MAP_ID",
restriction: {
latLngBounds: {
north: 51.74,
south: 51.27,
west: -0.51,
east: 0.23,
},
strictBounds: true,
},
});
const rectangle = new Rectangle({
bounds: london_bounds,
editable: true,
draggable: true,
});
rectangle.setMap(map);
rectangle.addListener("bounds_changed", async function () {
removeMarkers();
const { south, west, north, east } = rectangle.getBounds()?.toJSON();
const algolia_bounds = [north, east, south, west];
const res = await client.searchSingleIndex({
indexName: INDEX_NAME,
searchParams: {
...algolia_params,
insideBoundingBox: [algolia_bounds],
attributesToRetrieve: ["_geoloc", "name"],
},
});
res.hits.forEach((hit) => {
console.log("hitting again");
const marker = new AdvancedMarkerElement({
map: map,
position: {
lat: hit._geoloc.lat,
lng: hit._geoloc.lng,
},
});
const window = new InfoWindow({
content: hit.name,
});
marker.addListener("click", function () {
window.open({
anchor: marker,
map,
});
});
markers.push(marker);
});
});
}
initMap();
|