Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Nhà phát triển ở Khu vực kinh tế Châu Âu (EEA)
Bạn có thể tạo các phép tính của riêng mình bằng TypeScript. Đoạn mã ở cuối trang này giúp bạn xác định xem việc lắp đặt tấm pin mặt trời có rẻ hơn về lâu dài hay không, hoặc bạn có nên tiếp tục thanh toán hoá đơn tiền điện như hiện tại hay không.
Sau đây là thông tin tổng quan về cách mã này xác định chi phí của tấm pin mặt trời.
Phần 1: Yêu cầu về hệ thống và cách thiết lập
Trước tiên, hãy xác định mức sử dụng điện và hoá đơn hiện tại:
Mỗi tháng bạn sử dụng bao nhiêu điện? (monthlyKwhEnergyConsumption)
Chi phí cho lượng điện đó là bao nhiêu? (energyCostPerKwh)
Tiếp theo, hãy nhập kế hoạch về hệ mặt trời của bạn:
Có bao nhiêu tấm pin? (panelsCount)
Các tấm pin này có công suất bao nhiêu? (panelCapacityWatts)
Chi phí lắp đặt là bao nhiêu? (installationCostPerWatt)
Hệ thống có chiết khấu nào không? (solarIncentives)
Phần 2: Tính toán
Dựa trên các giá trị đã nhập, mã này sẽ tính toán:
yearlyProductionAcKwh: Tổng lượng điện năng hàng năm mà các tấm pin mặt trời của bạn có thể tạo ra.
totalCostWithSolar: Chi phí điện trong nhiều năm khi sử dụng tấm pin mặt trời.
totalCostWithoutSolar: Chi phí điện trong nhiều năm khi không sử dụng tấm pin mặt trời.
Phần 3: Kết quả
Mã này cũng cho bạn biết những thông tin sau:
savings: Mức chênh lệch giữa chi phí khi có và không có tấm năng lượng mặt trời.
breakEvenYear: Số năm cho đến khi chi phí lắp đặt tấm pin mặt trời bằng với khoản tiết kiệm tiền điện.
Mã mẫu
// Solar configuration, from buildingInsights.solarPotential.solarPanelConfigsletpanelsCount=20;letyearlyEnergyDcKwh=12000;// Basic settingsletmonthlyAverageEnergyBill:number=300;letenergyCostPerKwh=0.31;letpanelCapacityWatts=400;letsolarIncentives:number=7000;letinstallationCostPerWatt:number=4.0;letinstallationLifeSpan:number=20;// Advanced settingsletdcToAcDerate=0.85;letefficiencyDepreciationFactor=0.995;letcostIncreaseFactor=1.022;letdiscountRate=1.04;// Solar installationletinstallationSizeKw:number=(panelsCount*panelCapacityWatts)/1000;letinstallationCostTotal:number=installationCostPerWatt*installationSizeKw*1000;// Energy consumptionletmonthlyKwhEnergyConsumption:number=monthlyAverageEnergyBill/energyCostPerKwh;letyearlyKwhEnergyConsumption:number=monthlyKwhEnergyConsumption*12;// Energy produced for installation life spanletinitialAcKwhPerYear:number=yearlyEnergyDcKwh*dcToAcDerate;letyearlyProductionAcKwh:number[]=[...Array(installationLifeSpan).keys()].map((year)=>initialAcKwhPerYear*efficiencyDepreciationFactor**year,);// Cost with solar for installation life spanletyearlyUtilityBillEstimates:number[]=yearlyProductionAcKwh.map((yearlyKwhEnergyProduced,year)=>{constbillEnergyKwh=yearlyKwhEnergyConsumption-yearlyKwhEnergyProduced;constbillEstimate=(billEnergyKwh*energyCostPerKwh*costIncreaseFactor**year)/discountRate**year;returnMath.max(billEstimate,0);// bill cannot be negative},);letremainingLifetimeUtilityBill:number=yearlyUtilityBillEstimates.reduce((x,y)=>x+y,0);lettotalCostWithSolar:number=installationCostTotal+remainingLifetimeUtilityBill-solarIncentives;console.log(`Cost with solar: $${totalCostWithSolar.toFixed(2)}`);// Cost without solar for installation life spanletyearlyCostWithoutSolar:number[]=[...Array(installationLifeSpan).keys()].map((year)=>(monthlyAverageEnergyBill*12*costIncreaseFactor**year)/discountRate**year,);lettotalCostWithoutSolar:number=yearlyCostWithoutSolar.reduce((x,y)=>x+y,0);console.log(`Cost without solar: $${totalCostWithoutSolar.toFixed(2)}`);// Savings with solar for installation life spanletsavings:number=totalCostWithoutSolar-totalCostWithSolar;console.log(`Savings: $${savings.toFixed(2)} in ${installationLifeSpan} years`);
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-10 UTC."],[[["\u003cp\u003eThis webpage provides TypeScript code to calculate and compare the long-term costs of using solar panels versus continuing with your current electricity bill.\u003c/p\u003e\n"],["\u003cp\u003eThe code considers factors such as electricity consumption, solar panel system specifications, installation costs, incentives, and energy cost inflation to project expenses over time.\u003c/p\u003e\n"],["\u003cp\u003eBy comparing the total cost with and without solar, you can determine the potential savings and the break-even point for your solar panel investment.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code requires inputting your specific energy usage, solar panel system details, and financial parameters to generate personalized results.\u003c/p\u003e\n"]]],["The provided TypeScript code calculates the long-term cost-effectiveness of installing solar panels. It takes inputs like monthly energy consumption, electricity cost, panel count, panel capacity, installation cost, and incentives. The code then computes the annual electricity generation from solar panels, the total electricity cost with and without solar over a set lifespan, the difference in savings between both options, and the break-even year where the savings match the investment cost.\n"],null,["# Calculate costs and savings in TypeScript\n\n**European Economic Area (EEA) developers** If your billing address is in the European Economic Area, effective on 8 July 2025, the [Google\n| Maps Platform EEA Terms of Service](https://cloud.google.com/terms/maps-platform/eea) will apply to your use of the Services. [Learn more](/maps/comms/eea/faq). In addition, certain content from the Solar API will no longer be returned. [Learn more](/maps/comms/eea/solar).\n\nYou can create your own calculations using TypeScript. The code at the bottom of\nthis page helps you determine whether it's cheaper in the long run to install\nsolar panels or continue paying your electricity bill as is.\n\nHere's a high-level breakdown of how the code determines solar panel costs.\n\nPart 1: System needs and setup\n------------------------------\n\nFirst, define your current electricity usage and bills:\n\n- How much electricity do you use each month? (`monthlyKwhEnergyConsumption`)\n- How much does that electricity cost? (`energyCostPerKwh`)\n\nNext, enter your solar system plans:\n\n- How many panels? (`panelsCount`)\n- How powerful are the panels? (`panelCapacityWatts`)\n- How much does installation cost? (`installationCostPerWatt`)\n- Any discounts on the system? (`solarIncentives`)\n\nPart 2: Calculations\n--------------------\n\nBased on the inputted values, the code calculates:\n\n- `yearlyProductionAcKwh`: The total annual electricity your solar panels can generate.\n- `totalCostWithSolar`: The cost of electricity over many years **with** solar panels.\n- `totalCostWithoutSolar`: The cost of electricity over many years **without** solar panels.\n\nPart 3: Results\n---------------\n\nThe code also tells you the following:\n\n- `savings`: The difference between the cost with and without solar panels.\n- `breakEvenYear`: How many years until the cost of solar panels equals the savings on electricity.\n\nExample code\n------------\n\n\n```typescript\n// Solar configuration, from buildingInsights.solarPotential.solarPanelConfigs\nlet panelsCount = 20;\nlet yearlyEnergyDcKwh = 12000;\n\n// Basic settings\nlet monthlyAverageEnergyBill: number = 300;\nlet energyCostPerKwh = 0.31;\nlet panelCapacityWatts = 400;\nlet solarIncentives: number = 7000;\nlet installationCostPerWatt: number = 4.0;\nlet installationLifeSpan: number = 20;\n\n// Advanced settings\nlet dcToAcDerate = 0.85;\nlet efficiencyDepreciationFactor = 0.995;\nlet costIncreaseFactor = 1.022;\nlet discountRate = 1.04;\n\n// Solar installation\nlet installationSizeKw: number = (panelsCount * panelCapacityWatts) / 1000;\nlet installationCostTotal: number = installationCostPerWatt * installationSizeKw * 1000;\n\n// Energy consumption\nlet monthlyKwhEnergyConsumption: number = monthlyAverageEnergyBill / energyCostPerKwh;\nlet yearlyKwhEnergyConsumption: number = monthlyKwhEnergyConsumption * 12;\n\n// Energy produced for installation life span\nlet initialAcKwhPerYear: number = yearlyEnergyDcKwh * dcToAcDerate;\nlet yearlyProductionAcKwh: number[] = [...Array(installationLifeSpan).keys()].map(\n (year) =\u003e initialAcKwhPerYear * efficiencyDepreciationFactor ** year,\n);\n\n// Cost with solar for installation life span\nlet yearlyUtilityBillEstimates: number[] = yearlyProductionAcKwh.map(\n (yearlyKwhEnergyProduced, year) =\u003e {\n const billEnergyKwh = yearlyKwhEnergyConsumption - yearlyKwhEnergyProduced;\n const billEstimate =\n (billEnergyKwh * energyCostPerKwh * costIncreaseFactor ** year) / discountRate ** year;\n return Math.max(billEstimate, 0); // bill cannot be negative\n },\n);\nlet remainingLifetimeUtilityBill: number = yearlyUtilityBillEstimates.reduce((x, y) =\u003e x + y, 0);\nlet totalCostWithSolar: number =\n installationCostTotal + remainingLifetimeUtilityBill - solarIncentives;\nconsole.log(`Cost with solar: $${totalCostWithSolar.toFixed(2)}`);\n\n// Cost without solar for installation life span\nlet yearlyCostWithoutSolar: number[] = [...Array(installationLifeSpan).keys()].map(\n (year) =\u003e (monthlyAverageEnergyBill * 12 * costIncreaseFactor ** year) / discountRate ** year,\n);\nlet totalCostWithoutSolar: number = yearlyCostWithoutSolar.reduce((x, y) =\u003e x + y, 0);\nconsole.log(`Cost without solar: $${totalCostWithoutSolar.toFixed(2)}`);\n\n// Savings with solar for installation life span\nlet savings: number = totalCostWithoutSolar - totalCostWithSolar;\nconsole.log(`Savings: $${savings.toFixed(2)} in ${installationLifeSpan} years`);https://github.com/googlemaps-samples/js-solar-potential/blob/1892b99f1a8e4d7254c8b4426cfe9c8d492108b2/src/routes/sections/SolarPotentialSection.svelte#L53-L108\n```\n| **Note:** Read the [guide](/maps/documentation/javascript/using-typescript) on using TypeScript and Google Maps.\n\n\u003cbr /\u003e"]]