室內地圖、機廠佈局、PCB 版圖、遊戲地圖等無經緯度概念的平面坐標場景,需使用 MapX 的 Non-Earth (米/英尺/自定義單位) 坐標系。關鍵在於 `CoordSys.Set` 必須顯式指定 `MapBounds` (MBR 範圍),否則地圖無法正確顯示與縮放。
int CMyGisView::OnCreate(LPCREATESTRUCT lpCreateStruct) {
if (CView::OnCreate(lpCreateStruct) == -1) return -1;
// 1. 建立 Map 控制項並載入地圖配置
m_ctrlMapX.Create(NULL, WS_VISIBLE, CRect(0,0,100,100), this, IDC_MAP);
m_ctrlMapX.SetGeoSet(_T("Mygst.gst"));
// 2. 定義非地球坐標系參數
CMapXRectangle rect;
CMapXCoordSys coordSys;
COleVariant vEmpty; // 選填參數佔位
if (!rect.CreateDispatch(rect.GetClsid()) ||
!coordSys.CreateDispatch(coordSys.GetClsid())) {
TRACE0("COM 物件創建失敗");
return -1;
}
try {
// 單位:米 (miUnitMeter=5, 亦可用 miUnitFoot=6 等)
VARIANT vUnit; vUnit.vt = VT_I4; vUnit.lVal = miUnitMeter;
// 關鍵:設定 MBR 範圍 (從 gst 或資料實際範圍獲取)
// 範例值:對應 gst 中 GEOSETMBRLOWERLEFT / GEOSETMBRUPPERRIGHT
rect.Set(-2165.54, 9841.999, 46782.42, 41026.85);
VARIANT vBounds; vBounds.vt = VT_DISPATCH;
vBounds.pdispVal = rect.m_lpDispatch;
vBounds.pdispVal->AddRef(); // 重要:避免過早釋放
// CoordSys.Set 參數順序 (參考 MapX 文檔):
// Type, Projection, Units, OriginLat, OriginLong, StdParallel1, StdParallel2,
// FalseEasting, FalseNorthing, ScaleFactor, Azimuth, LongitudeWrap, Bounds, Affine
coordSys.Set(0, vEmpty, vUnit, vEmpty, vEmpty, vEmpty,
vEmpty, vEmpty, vEmpty, vEmpty, vEmpty, vEmpty,
vBounds, vEmpty);
// 3. 應用至 Map 控制項
m_ctrlMapX.SetNumericCoordSys(coordSys.m_lpDispatch);
m_ctrlMapX.SetDisplayCoordSys(m_ctrlMapX.GetNumericCoordSys().m_lpDispatch);
}
catch (COleDispatchException* e) { e->ReportError(); e->Delete(); }
catch (COleException* e) { e->ReportError(); e->Delete(); }
return 0;
}
| 索引 | 參數 | 非地球坐標系典型值 |
|---|---|---|
| 0 | Type | 0 = Non-Earth |
| 1 | Projection | vEmpty (非地球無投影) |
| 2 | Units | miUnitMeter (5) / miUnitFoot (6) / miUnitCustom (100) |
| 3~11 | 投影參數 | vEmpty |
| 12 | Bounds (MBR) | **必填** CMapXRectangle 物件 |
| 13 | Affine | vEmpty (除非需仿射變換) |
// 使用 _variant_t 簡化 VARIANT 管理
_variant_t vUnit((long)miUnitMeter);
_variant_t vBounds(rect.m_lpDispatch); // 自動 AddRef/Release
coordSys.Set(0, vtMissing, vUnit, vtMissing, vtMissing, vtMissing,
vtMissing, vtMissing, vtMissing, vtMissing, vtMissing, vtMissing,
vBounds, vtMissing);
MapX 非地球坐標系配置的核心在於 正確設定 `CoordSys.Set` 的 `Units` 與 `Bounds` 兩個參數。本文提供經實測可用的 VC6 完整代碼,涵蓋 COM 物件創建、參數填充、異常處理與資源管理,可直接應用於室內 GIS、工業佈局等平面坐標專案。