翻译稿 联系客服

发布时间 : 星期六 文章翻译稿更新完毕开始阅读68d0e5d726fff705cc170afd

Getting Started with MapObjects Version 2.x in Visual C++

}

CMoRecordset recs(states.SearchExpression(expression));

//

// Beep if not found. Otherwise, show the first state // found by zooming in and flashing. //

if (recs.GetEof()) ::MessageBeep(MB_ICONQUESTION); else { CMoFields fields(recs.GetFields()); CMoField shapeField(fields.Item(COleVariant(TEXT(\ CMoPolygon shape(shapeField.GetValue().pdispVal); CMoRectangle r(shape.GetExtent()); r.ScaleRectangle(2); m_map.SetExtent(r); // zoom to the state m_map.Refresh(); // force redraw m_map.FlashShape(shape, 3); }

21

The search code first builds a simple SQL query expression using the text from IDC_SEARCHEDIT. Then the States layer is searched using the SearchExpression method with the result being a RecordSet object. If the value of the RecordSet's EOF property is False, the RecordSet is positioned on the first record that satisfies the search expression. In that case, the value of the Shape field is obtained for the first record. The Extent of the shape is scaled and then set to be the Extent of the map. The map is then explicitly redrawn using the Refresh method, and finally, the shape is flashed three times.

搜寻码首先建立一个简单的SQL查询表达式IDC_SEARCHEDIT使用文本。然后美国搜索SearchExpression层使用方法和结果是一个记录集对象。如果值的记录集的EOF财产是假的,记录的位置在第一条记录时,满足了搜索的表达。在这种情况下,价值的领域得到形状的第一张唱片。形状的程度,然后将缩减程度的地图。然后这个地图使用显式刷新方法,最后,其形状是闪过三次。

Test your changes

1. 2. 3. 4.

Build the application. Run the application.

Type the name of a state, for example Illinois, into the search field. Press the Search key.

测试你的改变

1。建立应用程序。 2。运行的应用。

3。一个国家的类型的名字,例如伊利诺斯州,在搜寻栏。 4。按搜索关键字。

Getting Started with MapObjects Version 2.x in Visual C++ 22

Getting Started with MapObjects Version 2.x in Visual C++ 23

Step Five: Displaying map layers based on scale

In this section you will add a new layer to your map and add code which controls whether or not that layer is visible at a given time.

Add another layer

1. Open IDD_EASYMAP_FORM in the resource editor.

2. Right-click the map to display the context menu. Click Properties to show the Map Control property

sheet.

3. Click the Control Tab and then click the Add button and locate the folder where the sample data is

stored.

4. Click the Counties.shp file and then click Open. 5. Click the Counties layer in the list to select it.

6. Click the down arrow to move the Counties layer below the USHigh layer. 7. Click Properties to change the color of the Counties layer. 8. Click OK to dismiss the Layer Properties dialog. 9. Close the property sheet.

If you run your application now you'll notice that every county in the US is displayed. At the full extent, there is no need to display that much detail; so in response to the BeforeLayerDraw event, you will selectively make the Counties and States layers visible or invisible depending on the current extent of the map.

Respond to the BeforeLayerDraw event

1. Use Class Wizard to add a IDC_MAP1 BeforeLayerDraw handler to CEasyMapView. Call it

OnBeforeLayerDrawMap1.

2. Add code to the OnBeforeLayerDrawMap1 procedure:

void CEasyMapView::OnBeforeLayerDrawMap1(short index, long hDC) {

//

// Calculate whether or not counties should be shown //

CMoRectangle extent(m_map.GetExtent());

CMoRectangle fullExtent(m_map.GetFullExtent());

BOOL showDetail = (extent.GetWidth() < (fullExtent.GetWidth() / 4.0));

//

// Set layer visiblility //

CMoLayers layers(m_map.GetLayers());

CMoMapLayer layer(layers.Item(COleVariant(index)));

switch (index) { // counties case 1: layer.SetVisible(showDetail); break; // states case 2: layer.SetVisible(!showDetail); break; } }

The value of the visible property of each layer is based on the current extent of the map. If the width of the current extent is less than or equal to one fourth of the full extent of the map, then the counties will be visible and the states will be invisible. As this code is executed in response to the BeforeLayerDraw event for each layer, the value of the visible field is calculated before drawing occurs.

Getting Started with MapObjects Version 2.x in Visual C++ 24

Test your changes

1. Build the application 2. Run the application

3. Zoom into New England and the Counties layer becomes visible.

4. Click the FullExtent button and the Counties are no longer visible.

Step Six: Adding a spatial query tool

In this section you will implement the spatial query tool. You will add code to your application that carries out the query and draws the results onto the map. When you click on the map using the spatial query tool, two searches are performed. The first search is a point proximity search on the USHigh layer. The point is obtained by converting the x and y coordinates of the event, which are in control units, into map units. If the first search is successful, the highway that is found is used as the input to the second search which is performed on the Counties layer. The result of the second search is stored in the variable, CEasyMapView::m_selection.

Add the query code

1. Use the ClassView “Add Member Variable...” menu to add a new private variable to

CEasyMapView (or simply edit MapView.h):

CMoRecordset m_selection;

2. Open mapview.cpp and go to the CEasyMapView::OnMouseDownMap1 method.