实现矢量图层动态显示移动目标

事实上,就是利用计时器,闪烁目标。

void CmapwingisTest3View::OnTimerTracking()

{

    m_map.put_Projection(PROJECTION_NONE );

    m_map.put_GrabProjectionFromData(TRUE );

    m_map.put_DisableWaitCursor(true );

    CString filename1 =  "E:\mapwingis\MapViewer\data\buildings.shp";//图ª?层?数ºy据Y

    CString filename2 =  "E:\mapwingis\MapViewer\data\roads.shp";

    CString filename3 =  "E:\mapwingis\MapViewer\data\path.shp";

    if (!::PathFileExists(filename1) || !::PathFileExists(filename2) || !::PathFileExists(filename3))

    {

        AfxMessageBox("Couldn't find the files (buildings.shp, roads.shp, path.shp): " );

    }

    else

    {

        mapWindow::IShapefilePtr sf;

        sf.CreateInstance(__uuidof(mapWindow::Shapefile));

        sf->Open(_bstr_t(filename1),false );

        m_map.AddLayer( sf,TRUE) ;

        sf.CreateInstance(__uuidof(mapWindow::Shapefile));

        sf->Open(_bstr_t(filename2),false );

        m_map.AddLayer( sf,TRUE) ;

        mapWindow::ILabelsPtr labels=sf->GetLabels( );//?

        labels->PutFontSize(6);

        labels->PutFontBold(true);

        labels->PutFontOutlineVisible(false);

        labels->PutFontOutlineWidth(1);

        mapWindow::ILabelCategoryPtr cat;

        labels->AddCategory("Red");

        labels->Generate( "[Name]", lpLongestSegement, false);

        IUtilsPtr utils ;//= new Utils();

        utils.CreateInstance(__uuidof(mapWindow::Utils));

     

        ILinePatternPtr pattern ;//= new LinePattern();

        pattern.CreateInstance(__uuidof(mapWindow::LinePattern));

        pattern->AddLine( utils->ColorByName( Brown ), 10.0f, dsSolid);

        pattern->AddLine( utils->ColorByName( Yellow ), 9.0f, dsSolid);

        IShapeDrawingOptionsPtr drawingoption;

        drawingoption.CreateInstance(__uuidof(mapWindow::ShapeDrawingOptions));

        drawingoption->PutLinePattern( pattern );

        drawingoption->PutUseLinePattern(true );

        sf->PutDefaultDrawingOptions( drawingoption );

        m_map.AddLayer(sf, true);

        sf.CreateInstance(__uuidof(mapWindow::Shapefile));

        sf->Open(_bstr_t(filename3),NULL );

        m_map.AddLayer(sf, true);

        long ShapeIndex =0;

        m_path = sf->GetShape( ShapeIndex );

        m_map.SetMapUnits( umMeters );

        m_map.put_CurrentScale( 5000.0 );

         

        SetTimer(1,250,NULL);

    }

}

void CmapwingisTest3View::OnTimer(UINT_PTR nIDEvent)//计数器

{

    // moves car a step further

    m_distance += m_step;

    double Length=0.0;

    m_path->get_Length( &Length );

    if (m_distance > Length)

        m_distance = Length - m_distance;

    //calculating the current position (x2, y2)

    double distance = 0.0;

    double x1, x2, y1, y2;

    int numPoints = m_path->GetnumPoints();

    x1= x2 = y1 = y2 = 0.0;

    for (long i = 1; i < numPoints; i++)//根据路径计算当前位置

    {

        m_path->GetXY(i,&x2, &y2);

        m_path->GetXY(i-1,&x1, &y1);

        double val = sqrt(pow(x2 - x1, 2.0) + pow(y2 - y1, 2.0));

        if (distance + val > m_distance)

        {

            double ratio = (m_distance - distance) / val;

            x2 = x1 + (x2 - x1) * ratio;

            y2 = y1 + (y2 - y1) * ratio;

            //distance += val * ratio;

            break;

        }

        if (distance + val < m_distance)

        {

            distance += val;

        }

        else

        {

            break;

        }

    }

    DrawPosition(x2, y2);

    CFormView::OnTimer(nIDEvent);

}

void  CmapwingisTest3View::DrawPosition(double x, double y)//// Displays the point in the current position 显示

{

    m_map.ClearDrawings();

    IExtents *ext = m_map.GetMaxExtents(); 

    double xMin,xMax,yMin,yMax;

    double pxX,   pxY;

    ext->get_xMin(&xMin );

    ext->get_xMax(&xMax );

    ext->get_yMin(&yMin );

    ext->get_yMax(&yMax );

    if (x < xMin || x > xMax || y < yMin || y > yMax)

    {(http://www.amjmh.com/v/)

        double width = (xMax - xMin) / 2.0;

        double height = (yMax - yMin) / 2.0;

        ext->SetBounds( x - width, y - height, 0.0, x + width, y + height, 0.0);

        m_map.put_Extents( ext );  

    }

    if (m_count % 2 == 0)//偶数显示,实现闪烁效果

    {

        int handle = m_map.NewDrawing(dlScreenReferencedList);

        pxX = 0.0;

        pxY = 0.0;

        m_map.ProjToPixel(x, y, &pxX, &pxY);//坐标转换

        m_map.DrawCircleEx(handle, pxX, pxY, 5.0, 255, true);//画圆圈

    }

    m_count++;

    return;

}

原文地址:https://www.cnblogs.com/ly570/p/11491436.html