Skip to content

Commit c4bd0cd

Browse files
authored
Merge pull request #187 from dubiety/users/charlie/fix-runtime-error
Fix sample errors
2 parents cdf6c36 + 2158510 commit c4bd0cd

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

Samples/REST Services/Fill Address Form with Autosuggest/Fill Address Form with Autosuggest.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@
8686

8787
function getExpiration(jwtToken) {
8888
// Decode the JWT token to get the expiration timestamp
89-
const json = atob(jwtToken.split(".")[1]); - const decode = JSON.parse(json);
89+
const json = atob(jwtToken.split(".")[1]);
90+
const decode = JSON.parse(json);
9091

9192
// Return the milliseconds until the token needs renewed
9293
// Reduce the time until renew by 2 minutes to avoid using an expired token
@@ -102,14 +103,16 @@
102103
// Create a repeating timeout that will renew the SAS token
103104
// This timeout must be cleared once the TokenCredential object is no longer needed
104105
// If the timeout is not cleared the memory used by the TokenCredential will never be reclaimed.
105-
const renewToken = async () => { - try {
106+
const renewToken = async () => {
107+
try {
106108
console.log("Renewing token");
107109
token = await getSasToken();
108110
tokenRenewalTimer = setTimeout(renewToken, getExpiration(token));
109-
} catch (error) {
110-
console.log("Caught error when renewing token"); - console.log("Caught error when renewing token");
111+
} catch (error) {
112+
console.log("Caught error when renewing token");
111113
clearTimeout(tokenRenewalTimer);
112-
throw error; - }
114+
throw error;
115+
}
113116
}
114117
tokenRenewalTimer = setTimeout(renewToken, getExpiration(token));
115118
}

Samples/REST Services/Simple REST Geocoding Request/Simple REST Geocoding Request.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
<script>
1616
//Your Azure Maps Active Directory details for autheniticating the service.
17-
var tokenService = 'https://samples.azuremaps.com/api/GetAzureMapsSASToken';
17+
var tokenService = 'https://samples.azuremaps.com/api/GetAzureMapsSasToken';
1818

1919
var token;
2020

Samples/Services Module/Methods for geocoding multiple addresses/Methods for geocoding multiple addresses.html

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
<link href="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.css" rel="stylesheet" />
1818
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.js"></script>
1919

20+
<!-- Add a reference to the Azure Maps Services Module JavaScript file. -->
21+
<script src="https://atlas.microsoft.com/sdk/javascript/service/2/atlas-service.min.js"></script>
22+
2023
<!-- Add a reference to the Azure Maps Rest Helper JavaScript file. -->
2124
<script src="https://samples.azuremaps.com/lib/azure-maps/azure-maps-helper.min.js"></script>
2225

@@ -145,6 +148,12 @@
145148
//Add a layer for rendering data.
146149
map.layers.add(new atlas.layer.BubbleLayer(datasource));
147150
});
151+
152+
//Use MapControlCredential to share authentication between a map control and the service module.
153+
var pipeline = atlas.service.MapsURL.newPipeline(new atlas.service.MapControlCredential(map));
154+
155+
//Create an instance of the SearchURL client.
156+
searchURL = new atlas.service.SearchURL(pipeline);
148157
}
149158

150159
var searchMethod;
@@ -207,10 +216,8 @@
207216
var requestUrl = 'https://{azMapsDomain}/search/address/batch/sync/json?api-version=1.0';
208217
var res = await processPostRequest(requestUrl, body);
209218

210-
var r = await res.json();
211-
212219
//Process the response.
213-
processBatchResponse(r);
220+
processBatchResponse(res);
214221

215222
//Done.
216223
endSearch();
@@ -222,12 +229,13 @@
222229
var requestUrl = 'https://{azMapsDomain}/search/address/batch/json?api-version=1.0';
223230
var res = await processPostRequest(requestUrl, body);
224231

225-
//Ge tthe batch status URL from the "location" response header.
232+
//Get the batch status URL from the "location" response header.
226233
var batchStatusUrl = res.headers.get('location');
234+
var signedRequest = await signRequest(requestUrl);
227235

228-
//Check the statuc of the batch job.
236+
//Check the status of the batch job.
229237
var res2 = await fetch(batchStatusUrl, {
230-
headers: new Headers(requestParams.headers)
238+
headers: new Headers(signedRequest.headers)
231239
});
232240

233241
//Check to see if batch process if complete.

Static/lib/azure-maps/azure-maps-helper.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ async function processPostRequest(url, body) {
3333
throw new Error(`Network response was not ok: ${response.status} ${response.statusText}`);
3434
}
3535

36+
// If the response body is empty, return the response directly to avoid JSON parsing errors.
37+
// This can happen if the request was successful but there is no content to return.
38+
// e.g., 204 No Content or 202 Accepted
39+
if (response.status === 204 || response.status === 202) {
40+
return response;
41+
}
42+
3643
return response.json();
3744
}
3845

Static/lib/azure-maps/azure-maps-helper.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)