如何引用对象内存储的对象?飞机位置坐标提取技术求助
Let's work through this issue—you're just a couple of small fixes away from getting the coordinates you need!
First, let's spot the most obvious mistake: you have a typo in your variable name. You assigned the position to aircraft.currentPosition, but when trying to extract the latitude, you wrote aircraft.currentPositon (missing an i in "Position"). That's why you're not getting any data—you're trying to access a property that doesn't exist.
Second, Leaflet's L.latLng object stores longitude under the lng property, not lon. Even if you fixed the typo, using .lon would still fail because that's not the correct property name for Leaflet's LatLng instances.
Correct Code Examples
Here's how you should extract the coordinates properly:
Using Property Access
// Fix the typo and use Leaflet's correct property names const lat = aircraft.currentPosition.lat; const lng = aircraft.currentPosition.lng; // Use lng instead of lon
Using Method Calls (Alternative)
Leaflet's LatLng object also provides getter methods if you prefer that approach:
const lat = aircraft.currentPosition.lat(); const lng = aircraft.currentPosition.lng();
Quick Debug Tip
If you're still having issues, add a console log to verify what aircraft.currentPosition actually contains:
console.log(aircraft.currentPosition);
This should show you a Leaflet LatLng object with lat and lng properties (or methods) if your initial assignment worked correctly. If it logs undefined, double-check that posUpdate.record[i].lat and posUpdate.record[i].lon have valid values when you assign them.
内容的提问来源于stack exchange,提问作者cpeddie

