---
title: "MapXtreme2004代码 搜索指定点的最近图元（SearchNearst）"
author: "mmdev"
date: "2005-03-03T13:02:00Z"
tags: []
slug: "mapxtreme2004-searchnearst"
draft: false
lastmod: "2012-10-29T21:24:12Z"
id: 1708280
url: "https://mmdev.iteye.com/blog/1708280"
---

// 这里默认搜索离地图中心点最近的图元

DPoint ppt=MapControl1.Map.Center;

Map map = MapControl1.Map;

ISession session = MapInfo.Engine.Session.Current;

// 半径设置为5象素

Distance d =MapInfo.Mapping.SearchInfoFactory.ScreenToMapDistance(map, 5);

SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchNearest(ppt,map.GetDisplayCoordSys(),d);

// 搜索所有列

si.QueryDefinition.Columns = null;

// 搜到一个就停止搜索

(si.SearchResultProcessor as ClosestSearchResultProcessor).Options = ClosestSearchOptions.StopAtFirstMatch;

// 搜索到的图元放入DefaultSelection中

session.Catalog.Search("asicty79",si,session.Selections.DefaultSelection, ResultSetCombineMode.Replace);

// 得到FeatureCollection

MapInfo.Data.Table table = session.Catalog\["asicty79"];

IResultSetFeatureCollection fc = session.Selections.DefaultSelection\[table];

if (fc.Count==0)

Response.Write("没有搜索到任何图元！");

// 需要注意的是该图层必须设置为可以选择的 这样在地图上才能看出效果