Get Maps Current Extent

Topics: SharpMap v0.9 / v1.x
Feb 18, 2008 at 6:00 PM
I am wondering how I can get the maps current extent:


I am trying to make mouse over events for external data that has Lat and Long information. How I am putting them on the map requires me to know the MINX, MAXX, MINY, MAXY values. The following is my code:

Dim oSqlCon As New System.Data.SqlClient.SqlConnection
Dim sConnSTr As String
'Dim tMaxX, tMaxY, tMinX, tMinY As String
MAXX = myMap.Envelope.Max.X
MINX = myMap.Envelope.Min.X
MAXY = myMap.Envelope.Max.Y
MINY = myMap.Envelope.Min.Y
'tMaxX = MAXX.ToString
'tMaxY = MAXY.ToString
'tMinX = MINX.ToString
'tMinY = MINY.ToString

sConnSTr = "server=;uid=sa;pwd=Maps1234;database=MapData"
oSqlCon.ConnectionString = sConnSTr
Dim oSqlCom As New System.Data.SqlClient.SqlCommand
oSqlCom.CommandType = CommandType.StoredProcedure
oSqlCom.CommandText = "dbo.SelectIncidentsDemo"
oSqlCom.Connection = oSqlCon
Dim osqlParam(4) As System.Data.SqlClient.SqlParameter
' Dim iretPointID As Integer
oSqlCom.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Xmin", MINX))
oSqlCom.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Ymin", MINY))
oSqlCom.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Xmax", MAXX))
oSqlCom.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Ymax", MAXY))

Dim oSqlRdr As System.Data.SqlClient.SqlDataReader
oSqlRdr = oSqlCom.ExecuteReader()

Dim strLiteral As String
Dim strTemp As String
Dim strTemp2 As String

strLiteral = ""
strTemp = ""
strTemp2 = ""

If Not oSqlRdr Is Nothing Then
If oSqlRdr.HasRows Then
'Response.Write("<table cellspacing='0'>")
While oSqlRdr.Read

strLiteral = strLiteral & "<img src=""images/icon.png"" onmouseover=""this.TSTATIC=true;this.TSTICKY=1;this.TFONTCOLOR='#000000';this.TOFFSETX=-7;this.TOFFSETY=-7;this.TWIDTH=200;this.T_BGCOLOR='#ffffd1';return escape(' " & oSqlRdr("Descr") & " <BR><BR><FONT face=Tahoma size=1 >Click to close </FONT>')"" style=""Z-INDEX: 403; LEFT: " & GetImageX(MAXX, MINX, oSqlRdr("Longit")) & "px; POSITION: absolute; TOP: " & GetImageY(MAXY, MINY, oSqlRdr("Lat")) & "px"">"
'Me.Controls(1).Controls.AddAt(Me.Controls(1).Controls.Count, New LiteralControl(strLiteral))

End While

End If
End If
Me.Controls(1).Controls.AddAt(Me.Controls(1).Controls.Count, New LiteralControl(strLiteral))

Functions required:

Public Function GetImageY(ByVal rNorth As String, ByVal rSouth As String, ByVal YCorrd As String) As String

Dim oImageY As String
Dim distance As Double
Dim mapHeight As Double
Dim distanceRatio As Double
Dim ImageHeight As Double
Dim imagey As Double
Dim imgHeight As String
imgHeight = imgMap.Height.Value
distance = Math.Abs(Convert.ToDouble(YCorrd) - Convert.ToDouble(rNorth))
mapHeight = Convert.ToDouble(rNorth) - Convert.ToDouble(rSouth)
distanceRatio = distance / mapHeight
ImageHeight = Convert.ToDouble(imgHeight) '(456)
imagey = (ImageHeight * distanceRatio) + Convert.ToInt32(104)
oImageY = Convert.ToInt32(imagey)
Return oImageY

End Function

Public Function GetImageX(ByVal rEast As String, ByVal rWest As String, ByVal XCorrd As String) As String

Dim oImageX As String
Dim distance As Double
Dim mapWidth As Double
Dim distanceRatio As Double
Dim ImageWidth As Double
Dim imagex As Double
Dim imgWidth As String
imgWidth = imgMap.Width.Value
distance = Math.Abs(Convert.ToDouble(XCorrd) - Convert.ToDouble(rWest))
mapWidth = Convert.ToDouble(rEast) - Convert.ToDouble(rWest)
distanceRatio = distance / mapWidth
ImageWidth = Convert.ToDouble(imgWidth)
imagex = (ImageWidth * distanceRatio) + Convert.ToInt32(80)
oImageX = Convert.ToInt32(imagex)
Return oImageX

End Function

Basically what my code does is converts Lat and long into pixals and it is able to place them on the map based on how large the image is and its distance from the top and left parts of the browser. I also use walter zorns mouse tips

It seems like a lot just to get custom mouse over events but I have been able to get it to work on other projects in which I used ArcIMS 9.1...

I really need someones help on this one....
Feb 19, 2008 at 5:22 PM
What about using Map.WorldToImage()?

Here is an example which might help: Transform between image coordinates and world coordinates.
Feb 19, 2008 at 5:55 PM
OK, well after reading my message, I realized I answered it in the question itself.... The code that I posted enables mouse over events on the map...Basically what it is doing is converting Lat/long into pixals and then it is placing the icons and mouseover events on the map by calculating the distance of the image button from the top and the left of the browser and the size of the image.

The code also demostrates how to access a stored procedure in SQL 2005 (I can post a sample if anyone likes). Although this is a lot of coding to accomplish a simple task, it does allow flexibility and can enable a developer to create dynamic points.

I have one more question; it maybe a stupid one though....

How can I create an event that will enable a user to drag the map into position (pan). The default requires the user to click on the map multiple times...

Thank you for all of your help

Feb 20, 2008 at 1:00 PM
Edited Feb 20, 2008 at 1:09 PM

How can I create an event that will enable a user to drag the map into position (pan). The default requires the user to click on the map multiple times...

I'm not sure if I totally understand the problem, but if you are talking about having all of the layers displayed you want to use the Map.ZoomToExtents function. If you are taking about having to click on the map to give it focus before being able to use the different tools (pan, zoom, etc) if you add a MouseEnter event on the map image and do imgMap.Focus(), that should save a click.
Feb 20, 2008 at 1:51 PM
Hi thomas1948,

By your description of having to "click on the map multiple times...", it sounds like you are not using the Ajax control. If you want dynamic panning you need to use ajax to reload the map. Check out ajax.aspx in the demo website project of v0.9.

Thanks for the code on the mouse over events.

The only other way I could think to do this would be to do hot spots on the map image itself. I am not sure how well that would work.
Feb 20, 2008 at 5:54 PM

Can you please send me sample code. I would like to just be able to drag the map into position and then have the map update (panning without having to click each time on the map to move it into place).

Thank You
Feb 21, 2008 at 11:44 AM

Download "WebDemo_v09" from


Download "" from

Both of these have samples using the AjaxMap control.