In the previous article, if Google Map, Yahoo Map, and Bing Map are added to MapGuide Studio as the basemap, someone has given me a problem today. I know that on the MapGuide Fusion Viewer interface, you can use the External Provider menu to switch the map type. How can I implement it using a program?
To be honest, I will not solve this problem either. However, after some research, we found a solution. Here I will write down my research process for your reference. In the future, you can study it on your own. "Authorize it to Fish.
The basic network layout of Fusion Viewer and MapGuide Ajax Viewer is not the same. Instead, the framework of Ajax Viewer is no longer used, but the more popular layout of <Div> + CSS. Naturally, the APIs of Fusion Viewer vary depending on the basic network layout of Ajax Viewer. Since there are not many open methods in the Fusion Viewer API, and there are relatively few documents, it is estimated that everyone is also like me, the development process is often not document and worried. First, let's introduce the existing APIs of Fusion Viewer. In the MapGuide developer Guide, there is a chapter devoted to the basic APIs of Fusion Viewer. If you have not read them yet, we recommend that you first take a look. This document can be found in the installation directory of MapGuide. "C: \ Program Files \ Autodesk \ mapguidepolicise2011 \ WebServerExtensions \ Help \ DevGuide.pdf ". If you have previously developed the basic network layout of Ajax Viewer, you must be sure to know whether the APIs in AjaxViewer can be used in Fusion Viewer. In fact, MapGuide Fusion viewer also provides a set of simulation APIs, the function is consistent with that of the Ajax Viewer API. Junqilian has written related articles before. You can refer:
Digitize using Fusion Viewer API in MapGuide Enterprise 2011
To Implement ZoomToSelection in Fusion Viewer MapGuide Enterprise 2011
Now we start to program the switching of the basemap. First, I want to have an interface to trigger my program. Here I create an iInvoke Script custom command named SwitchBaseMap and add it to the ToolBar.
Next, I started To write my implementation code in the Script To invoke box. Of course, for convenience of editing, I usually wrote it in the text editor first and then pasted it in. How to write it? This requirement is to switch the BaseMap, just like on the Fusion Viewer interface. It seems that no related methods are introduced in the developer guide document. Maybe I forgot. Let's just look at the source code of Fusion. The benefits of MapGuide are as follows :)
Find a useful text editor. I use notepad ++ to open the Fusion source code file fusionSF. js. This file contains all related Fusion things, which are located in the C: \ Program Files \ Autodesk \ mapguideappsise2011 \ WebServerExtensions \ www \ fusion \ lib \ directory. The same directory contains Fusion, js, and other separate js files. Each file corresponds to a module. I am not quite sure that basemap-related things are stored in that module. I just opened this full fusionSF. js one-way search.
However, by searching for the "basemap" keyword, I found that there is a function called setBasemap. Hey, this name is good, and it seems like I am looking for something.
Let's take a look at his implementation. The above estimates are available parameters, such as Aerial and Hybrid. Try again first. At a glance, this setBasemap is a method of the Fusion. Widget. BasemapSwitcher class. Now let's see how to use this function.
By reading the Developer's Guide document, I know that Fusion is the boss of the global variable in Fusion Viewer. It is not difficult to find this Fusion object. There should be some sample code in the document for getting Fusion objects, but I am a little lazy. Remember C: \ Program Files \ Autodesk \ mapguide1_ise2011 \ WebServerExtensions \ www \ fusion \ layers \ MapGuide \ MapGuideViewerApi. js has already been implemented. I will take it, haha! First, test alert () to see if Fusion is not obtained.
var mgApiMapWidgetId = 'Map';var MainFusionWindow = GetFusionWindow();var OpenLayers = MainFusionWindow.OpenLayers;var Fusion = MainFusionWindow.Fusion;/* locate the Fusion window */function GetFusionWindow() { var curWindow = window; while (!curWindow.Fusion) { if (curWindow.parent && curWindow != curWindow.parent) { curWindow = curWindow.parent; } else if(curWindow.opener) { curWindow = curWindow.opener; } else { alert('Could not find Fusion instance'); break; } } return curWindow;}function GetFusionMapWidget() { return Fusion.getWidgetById(mgApiMapWidgetId);}alert(Fusion);
After testing, the Fusion object has been obtained successfully. The following figure shows the Fusion. Widget. BasemapSwitcher object. Then, the setBasemap object will be used. How can I find the Fusion. Widget. BasemapSwitcher object? At this time, I thought of the Developer Guide document as if to say that the method for obtaining widgets is GetWidgetById. In fact, the previous Code also used to obtain Map. But what is the BasemapSwitcher ID? I have been searching for a long time. Forget it. It doesn't consume much. Isn't there another similar method getWidgetsByType? Search by type. Note that there may be Multiple widgets of the same type, so an array is returned. There is only one BasemapSwitcher on my map, so you can get 0th. The following code is available:
var basemapSwitcher = Fusion.getWidgetsByType('BasemapSwitcher')[0];alert(basemapSwitcher);
The alert method is used to test and return [object]. It can be seen that the [object] has been found, and then the setBasemap method called by BasemapSwitcher is a matter of course. Let's test it. It works normally.
All the code is like this, which is also very simple:
var mgApiMapWidgetId = 'Map';var MainFusionWindow = GetFusionWindow();var OpenLayers = MainFusionWindow.OpenLayers;var Fusion = MainFusionWindow.Fusion;/* locate the Fusion window */function GetFusionWindow() { var curWindow = window; while (!curWindow.Fusion) { if (curWindow.parent && curWindow != curWindow.parent) { curWindow = curWindow.parent; } else if(curWindow.opener) { curWindow = curWindow.opener; } else { alert('Could not find Fusion instance'); break; } } return curWindow;}function GetFusionMapWidget() { return Fusion.getWidgetById(mgApiMapWidgetId);}var basemapSwitcher = Fusion.getWidgetsByType('BasemapSwitcher')[0];//close base map//basemapSwitcher.setBasemap('None');// siwth to Bing Map AerialbasemapSwitcher.setBasemap('Aerial');
Well, in short, MapGuide Fusion is now a simple document, but the source code is rich. If you want something without a document, go to the source code and click it.
Cheers,
Junqilian