As we discussed in previous blog about , we can zoom to specified view port by calling ZoomToView. But it would be great if there is an API by which we can zoom to the current selection. OK, let’s do it ourselves.
To make it part of our public Fuion viewer API, we can edit the MapGuideViewerApi.js file, which locates at C:\Program Files\Autodesk\MapGuideEnterprise2011\WebServerExtensions\www\fusion\layers\MapGuide by default. Here is the JavaScript code, you can copy and paste it into MapGuideViewerApi.js
function ZoomToSelection(){ var mapWidget = Fusion.getWidgetById(mgApiMapWidgetId); if (mapWidget && mapWidget.hasSelection()) { mapWidget.getSelection(_zoomtoselection_handler) } else { alert('No selection, please select some features first.'); }}//private function function _zoomtoselection_handler(multiSelection){ var mapWidget = Fusion.getWidgetById(mgApiMapWidgetId); var selection = multiSelection[mapWidget.getMapName()]; var lowerLeftCoord = selection.getLowerLeftCoord(); var upperRightCoord= selection.getUpperRightCoord(); var centerX = upperRightCoord.x - (upperRightCoord.x - lowerLeftCoord.x)/2; var centerY = upperRightCoord.y - (upperRightCoord.y - lowerLeftCoord.y)/2; var scale = mapWidget.getScale(); ZoomToView(centerX,centerY,scale,true); }
If you don’t want to muss up the source code of the product, you can only add it to your project (your page), just a little modification needed. code as below:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FusionViewerAPIDemo.aspx.cs" Inherits="FusionViewerAPIDemo" %><%--reference the fusion viewer API javascript file--%>
Happy MapGuiding!
峻祁连