目前我正在做一个项目,我应该在地图上向世界各地的各种客户展示。我使用 Bing Maps API 确定这些客户的坐标。但是如果我想在地图上显示这些客户,我会得到一些坐标错误的错误。数据库中的坐标看起来正确,并由 Bing 地图 API 返回给我。
现在的问题是:我如何捕获这个错误,以便脚本不会因此而崩溃?
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); }); }
错误:
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
我将执行以下操作来调试此问题:
Location
value['latitude']
value['longitude']
parseFloat
(parseFloat(value['latitude'])
latitude
longitude