Rahn, you need to hook the MouseUp event in whatever event you are using to load your map object as noted below....
Basically I followed how MapPoint snags its own result set after you double click an area on the map.....I sort of did this by accident. I was WriteLining the amount of results I received from the ObjectsFromPoint function, and I just happened to notice the results were the same amount of results as a double click returned.
Then I noticed the first from last item was ALWAYS the region in every case. Without fail. So, I grabbed that item in the collection of locations that are returned from ObjectsFromPoint and that is always the state!
Pretty neat eh?
(Feel free to use this code, please leave this header within!)
Code:
///Jason Pont
///4/10/03
///How to get a state from clicking anywhere on a map:
_______________________________
private class Form1()
{
private Map mUSA;
private void WhateverYouStartYourMapWith ()
{
mUSA.MouseUp += new MapPoint._MapEvents_MouseUpEventHandler(MouseUp);
}
private new void MouseUp(int button, int shift, int x, int y)
{
try
{
///Create a result set, and a default location
/// for comparison below
FindResults oRes;
//default oLoc to any valid location you want
MapPoint.Location oLoc = (Location)mUSA.Find("12234234 Broadway Street, New York, New York, 10024");;
//Grab all points of interest near the user's click
oRes = mUSA.ObjectsFromPoint(x, y);
//used in the loop below to check which result you are on
int resultCounter = 0;
//display count for your peace of mind
Debug.WriteLine (oRes.Count);
//move through the collection
foreach (object oResultObject in oRes)
{
//is this object a Location object?
if (oResultObject is Location)
{
//does this result set have more than 1 result?
//if not, this is your region (oceans act like this)
if (oRes.Count == 1)
{
//oLoc.Select();
oLoc = (Location)oResultObject;
//show the region in the title bar
///THIS IS WHERE YOU GET THE STATE NAME FROM!!!!
///Whoever needs the State name, pass it from the
///"oLoc" object's Region property to wherever
///you need it to go, and you're done!
frmMapPoint.ActiveForm.Text = oLoc.Name;
break;
}
//more than 1 result....1st to last will be the region
if (resultCounter == oRes.Count - 2)
{
oLoc = (Location)oResultObject;
//oLoc.Select();
//show the region in the title bar
///THIS IS WHERE YOU GET THE STATE NAME FROM!!!!
///Whoever needs the State name, pass it from the
///"oLoc" object's Region property to wherever
///you need it to go, and you're done!
frmMapPoint.ActiveForm.Text = oLoc.Name.ToString();
frmMapPoint.ActiveForm.Text = oLoc.StreetAddress.ToString();
//Debug.WriteLine(oLoc.StreetAddress.Street.ToString());
break;
}
}
//increment which result you are currently on
resultCounter++;
}
}
catch {};
}//end of mouseup
}//end of form1