-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathdemo.js
More file actions
158 lines (130 loc) · 5.11 KB
/
demo.js
File metadata and controls
158 lines (130 loc) · 5.11 KB
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
/**
* Geocode the address in the input text field and center the map on success.
*/
function geocode() {
const address = document.getElementById('geocodeAddress').value;
/*
* A full list of available request parameters can be found in the Geocoder API documentation.
* See: http://developer.here.com/rest-apis/documentation/geocoder/topics/resource-geocode.html
*/
const geocodingParameters = {
q: address
};
if (!address) {
return;
}
geocodingService.geocode(
geocodingParameters,
onSuccess,
onError
);
}
/**
* This function will be called once the Geocoder REST API provides a response
* @param {Object} result A JSON object representing the location(s) found.
*
* See: http://developer.here.com/rest-apis/documentation/geocoder/topics/resource-type-response-geocode.html
*/
function onSuccess(result) {
const locations = result.items;
addLocationsToMap(locations);
addLocationsToPanel(locations);
}
/**
* This function will be called if a communication error occurs during the JSON-P request
* @param {Object} error The error message received.
*/
function onError(error) {
alert('Can\'t reach the remote server');
}
/**
* Boilerplate map initialization code starts below
*/
// Step 1: initialize communication with the platform
// In your own code, replace variable window.apikey with your own apikey
const platform = new H.service.Platform({
apikey: window.apikey
});
const defaultLayers = platform.createDefaultLayers();
// Step 2: initialize a map - this map is initially centered over California
const map = new H.Map(document.getElementById('map'),
defaultLayers.vector.normal.map,{
center: {lat: 37.376, lng: -122.034},
zoom: 15,
pixelRatio: window.devicePixelRatio || 1
});
// add a resize listener to make sure that the map occupies the whole container
window.addEventListener('resize', () => map.getViewPort().resize());
const locationsContainer = document.getElementById('panel');
// Step 3: make the map interactive
// MapEvents enables the event system
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
const behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
// Create the default UI components
const ui = H.ui.UI.createDefault(map, defaultLayers);
// Create an instance of the Geocoding and Search service
const geocodingService = platform.getSearchService();
/**
* Creates a series of list items for each location found, and adds it to the panel.
* @param {Object[]} locations An array of locations as received from the
* H.service.GeocodingService
*/
function addLocationsToPanel(locations){
const nodeOL = document.createElement('ul');
nodeOL.style.fontSize = 'small';
nodeOL.style.marginLeft ='5%';
nodeOL.style.marginRight ='5%';
for (let i = 0; i < locations.length; i++) {
const location = locations[i],
li = document.createElement('li'),
divLabel = document.createElement('div'),
address = location.address,
position = location.position;
let content = '<strong style="font-size: large;">' + address.label + '</strong></br>';
content += '<strong>houseNumber:</strong> ' + address.houseNumber + '<br/>';
content += '<strong>street:</strong> ' + address.street + '<br/>';
content += '<strong>district:</strong> ' + address.district + '<br/>';
content += '<strong>city:</strong> ' + address.city + '<br/>';
content += '<strong>postalCode:</strong> ' + address.postalCode + '<br/>';
content += '<strong>county:</strong> ' + address.county + '<br/>';
content += '<strong>country:</strong> ' + address.countryName + '<br/>';
content += '<strong>position:</strong> ' +
Math.abs(position.lat.toFixed(4)) + ((position.lat > 0) ? 'N' : 'S') +
' ' + Math.abs(position.lng.toFixed(4)) + ((position.lng > 0) ? 'E' : 'W') + '<br/>';
divLabel.innerHTML = content;
li.appendChild(divLabel);
nodeOL.appendChild(li);
}
locationsContainer.replaceChildren(nodeOL);
}
/**
* Creates an instance of H.map.Marker for each location found and adds them to the map.
* @param {Object[]} locations An array of locations as received from the
* H.service.GeocodingService
*/
function addLocationsToMap(locations) {
const group = new H.map.Group();
if (!locations || locations.length === 0) {
alert('Address not found');
return;
}
// Remove all the pre-existent objects from the map
map.removeObjects(map.getObjects());
// Add a marker for each location found
for (let i = 0; i < locations.length; i += 1) {
const location = locations[i];
marker = new H.map.Marker(location.position);
marker.label = location.address.label;
group.addObject(marker);
}
// Add the locations group to the map
map.addObject(group);
// Position the map according to the number of locations
if (group.getObjects().length > 1) {
map.getViewModel().setLookAtData({bounds: group.getBoundingBox()});
} else {
map.getViewModel().setLookAtData({bounds: group.getBoundingBox(), zoom: 13});
}
}
// Send the initial geocoding request
geocode();