[mapguide-users] Re: Automatic zoom to selected features

soudemans soudemans at ruekert-mielke.com
Wed Nov 30 15:18:02 EST 2011


I love it when I get the answer before anyone gets a chance to respond.  And
(I think that) this solution is pretty sweet.  Some how I came accross the
fact that you could get the extents of the Mg  selection.  Well as they say,
everything else is history.  If you have any questions or comments please do
not hesitate to ask.  Solution's code is below.

Protected Sub btnShowMap_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnShowMap.Click
    ' Initialize mapguide instance
    InitMapGuide()
    Dim vMsg As String
    Dim map As New MgMap(_SiteConnection)
    map.Open(_ResourceService, _MapName)
    Dim layer As MgLayer = DirectCast(map.GetLayers().GetItem(_MgLayer),
MgLayer)
    Dim qryOptions As MgFeatureQueryOptions = New MgFeatureQueryOptions()
    qryOptions.SetFilter(_MgFeatID & " IN(" & GetSelectedIDs() & ")")
    Dim FeatReader As MgFeatureReader = layer.SelectFeatures(qryOptions)
    Dim vSelection As New MgSelection(map)
    Dim vSelExtents As MgEnvelope
    Dim vLLeftXY As MgCoordinate
    Dim vURightXY As MgCoordinate
    Dim vMapHeight, vMapWidth As Double
    Dim vLLx, vLLy, vURx, vURy, vCtrX, vCtrY As Double
    Dim _MapScale, _CtrPtX, _CtrPtY As String

    ' Create a mg selection
    vSelection.AddFeatures(layer, FeatReader, 0)
    ' Get the selection's extent
    vSelExtents = vSelection.GetExtents(_FeatureService)
    ' Get the lower left and upper right coordinate points of the extents to
calculate the center
    vLLeftXY = vSelExtents.GetLowerLeftCoordinate()
    vURightXY = vSelExtents.GetUpperRightCoordinate()
    ' Get the selection's height and width used to calculate a new scale
    vMapHeight = vSelExtents.GetHeight()
    vMapWidth = vSelExtents.GetWidth()
    ' Get the individial values for X and Y for each the lower left and
upper right coordinate points
    vLLx = vLLeftXY.GetX
    vLLy = vLLeftXY.GetY
    vURx = vURightXY.GetX
    vURy = vURightXY.GetY
    ' Calculate the center point X and Y values and convert them to strings
- that is what we need
    _CtrPtX = ((vURx - vLLx) / 2 + vLLx).ToString
    _CtrPtY = ((vURy - vLLy) / 2 + vLLy).ToString

    ' Calculate the new map scale.  65500 is the map scale when zoomed to
the map's extent
    ' 49300 is the heightin feet of the map and 43200 is the width in feet
of the map,
    ' both when zoomed to the map's extent. 
    If vMapHeight > vMapWidth Then
        ' Calculate scale when height of selection envelope is greater than
its width
        _MapScale = (((vMapHeight * 65500) / 43200) * 1.1).ToString
    Else
        ' Calculate scale when width of selection envelope is greater than
its height
        _MapScale = (((vMapWidth * 65500) / 49300) * 1.1).ToString
    End If

    ' Escape any single quotes 
    Dim xml As String = selectionSet.ToXml().Replace("'", "\'")

    ' Build the JavaScript code to set the active selection
    Dim s As String = "<script type=&quot;&quot;text/javascript&quot;&quot;
&quot;
    s += &quot;language=&quot;&quot;javascript&quot;&quot; &quot;
    s += &quot;for=&quot;&quot;window&quot;&quot;
event=&quot;&quot;onload&quot;&quot;>" & vbLf
    s += " var main = window.opener.window.opener;" & vbLf
    s += " var map = main.GetMapFrame();" & vbLf
    s += " map.SetSelectionXML('" & xml & "');" & vbLf
    s += " main.ZoomToView(" + _CtrPtX + "," + _CtrPtY + "," + _MapScale +
", true);" & vbLf
    s += "</script>" & vbLf

    ' Use the HttpResponse object to send this code to the client
    Response.Write(s)
End Sub

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Automatic-zoom-to-selected-features-tp7047230p7048282.html
Sent from the MapGuide Users mailing list archive at Nabble.com.


More information about the mapguide-users mailing list