我是閃電組件的新手,我正在嘗試開發將在帳戶頁面上顯示的小冊子地圖組件。當我第一次點擊帳戶記錄時,我收到「地圖已經初始化」的錯誤。如果我刷新頁面,它加載正常。然後,如果我點擊並返回到相同的帳戶,它的作品。但是,點擊另一個帳戶會引發與以前相同的錯誤。我試着動態地添加地圖容器,並試圖刪除地圖容器。任何建議都會很棒。帳戶頁面上閃電組件的小冊子地圖
謝謝!
這裏是所有的作品的代碼
controller.js
({
jsLoaded: function(component, event, helper) {
var recType = component.get("v.branch.fields.RecordTypeId.value");
var action = component.get("c.getBranch");
action.setParams({'recID' : component.get("v.recordId"),
'recordTypeID' : recType});
action.setCallback(this,function(response){
var state = response.getState();
if(state === "SUCCESS"){
var branch = response.getReturnValue();
helper.buildmap(component,branch);
}
});
$A.enqueueAction(action);
},
//future code goes here
})
helper.js:
({
loadAllClients: function(component,map){
var action = component.get("c.findAll");
var recType = component.get("v.branch.fields.RecordTypeId.value");
action.setParams({'recID' : component.get("v.recordId"),
'recordTypeID' : recType});
action.setCallback(this,function(response){
var state = response.getState();
console.log(state);
var objects = response.getReturnValue();
if(state === "SUCCESS"){
if(objects != null){
for (var i=0; i<objects.length; i++) {
var singleRec = objects[i];
var url = '/' + singleRec.recID;
var popup = '<a href=' + url + '>' +singleRec.Name+'</a>';
var latLng = [singleRec.Latitude, singleRec.Longitude];
L.marker(latLng, {account: singleRec}).addTo(map)
.bindPopup(popup);
}
}
}
});
$A.enqueueAction(action);
},
buildmap: function (component,branch){
var lat = branch.BillingLatitude;
var long = branch.BillingLongitude;
var map = new L.map('map');
map.setView(new L.LatLng(lat,long), 8);
setTimeout(function() {
L.AwesomeMarkers.Icon.prototype.options.prefix = 'fa';
var redMarker = L.AwesomeMarkers.icon({icon : 'home',markerColor: 'red'});
var radius = 80467.2;
L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}',
{
attribution: 'Tiles © Esri'
}).addTo(map);
L.marker([lat,long],{icon:redMarker}).addTo(map)
.bindPopup(branch.Name);
L.circle([lat,long], radius).addTo(map);
});
this.loadAllClients(component,map);
},//future code goes here
})
組件:
<aura:component implements="flexipage:availableForAllPageTypes,force:hasRecordId" access="global" controller="AccountController">
<aura:attribute name="branch" type="account"/>
<aura:attribute name="recordId" type="Id" />
<aura:attribute name="accounts" type="object[]" />
<ltng:require styles="/resource/leaflet/leaflet.css" />
<ltng:require styles="/resource/fontawesome/font-awesome-4.7.0/css/font-awesome.min.css"/>
<ltng:require styles="http://code.ionicframework.com/ionicons/1.5.2/css/ionicons.min.css"/>
<ltng:require styles="/resource/leaflet/leaflet.awesome-markers.css"/>
<ltng:require scripts="/resource/leaflet/leaflet.js,/resource/leaflet/leaflet.awesome-markers.js"
afterScriptsLoaded="{!c.jsLoaded}" />
<force:recordData aura:id="branchservice"
recordId="{!v.recordId}"
targetRecord="{!v.branch}"
fields="Name,RecordTypeId" />
<div id="map"></div>
</aura:component>
我的情況下,設置版本回到39是更衣室的服務,但沒有奏效。我試圖找到地圖,並刪除它「var map = component.get(」v.map「); if(map){map.remove();} - 但它似乎像component.get永遠不會返回地圖,然後在下面幾行下面我嘗試新的L.map,它會失敗並返回錯誤 – Psymn