At the moment I'm working on a project in which I'm supposed to show various customers worldwide on a map. I determine the coordinates of these customers using the Bing Maps API. But if I then want to display these customers on the map, I get the error for some that the coordinates are wrong. The coordinates in the database look correct and are returned to me by the Bing Maps API.
Now to the question: How do I catch this error so that the script doesn't crash because of it?
let map;
let searchManager;
let customers = <?= json_encode($elements); ?>;
let pins = [];
let iconURL = '';
function GetMap() {
var statusDropdownValue = parseInt($('#status').val());
map = new Microsoft.Maps.Map('#map', {
zoom: 1
});
$.each(customers, function(index, value) {
var customerLocation = new Microsoft.Maps.Location(value['latitude'], value['longitude']);
var pin = new Microsoft.Maps.Pushpin(customerLocation, {
icon: baseURL + 'assets/images/pin.png'
});
pin.metadata = {
id: value['id'],
customerName: value['customer_name'],
postCode: value['post_code'],
city: value['city'],
countryCode: value['country_code']
};
pins.push(pin);
});
Microsoft.Maps.loadModule("Microsoft.Maps.Clustering", function () {
clusterLayer = new Microsoft.Maps.ClusterLayer(pins);
map.layers.insert(clusterLayer);
});
Microsoft.Maps.loadModule('Microsoft.Maps.AutoSuggest', function () {
var manager = new Microsoft.Maps.AutosuggestManager({ map: map });
manager.attachAutosuggest('#search', '#search-container-search-bar', selectedSuggestion);
});
}
Error:
Uncaught Error: Invalid latitude
n https://r.bing.com/rp/iKR9c24bDSIz79-enVSnBoJvZx8.br.js:1
GetMap http://localhost/projects/BattermannTillery_HK_Locator/maps:101
each jQuery
GetMap http://localhost/projects/BattermannTillery_HK_Locator/maps:100
notifyMapReadyForBootstrap https://www.bing.com/api/maps/mapcontrol?callback=GetMap&setMkt=en-US&setLang=en&key=AlV57vXOvuSENqgwu6hnNKGhiLf85dbTMlnDY81z2cGq40L1xkMdXUVSkEhvqfvv:12
<anonymous> https://r.bing.com/rp/RENSVX2edu6CiHiu-aMi-GbtqbA.br.js:1
<anonymous> https://r.bing.com/rp/RENSVX2edu6CiHiu-aMi-GbtqbA.br.js:1