The HT for Web, as the logical topology graphics component itself, does not have GIS capabilities, but can be fused with a variety of GIS engines, namely its client components, to achieve seamless integration of logical and physical topologies, and this chapter will specifically describe the HT for Web and development free Openlayers map combined with the application of key technology points, the introduction of the principle of combining, in fact, can also be extended to ArcGIS, Baidu Map and GoogleMap and many other GIS map engine fusion solution.
The above screenshot for the example described in the final running effect, the next step to achieve, the preferred display map information needs to have the city latitude and longitude data, search the next thanks to the data provided by this blog. With such a large amount of data, I use the Getrawtext function described in "the Way (iv) of HT graphics component Design", with the data remaining the problem of presentation, and we need to overlay the HT Graphview component with the Openlayers map component. That is openlayers tile map picture below, Graphview component is above, because Graphview is transparent by default, so non-entity part user can penetrate see map content. Finding the right component insertion location is a headache, ArcGIS, Baidu maps including GoogleMap almost every different GIS component needs to be tried to find the right insertion location, and the integration of other GIS engine components will be introduced later in the chapter. The openlayers we focus on in this article is Map.viewPortDiv.appendChild (Graphview.getview ()).
After the HT and openlayers components are stacked together, the rest is the problem of the placement of elements in the topology and the latitude and longitude, which is stored in the general network topology diagram in HT. Node position is a logical location, and latitude and longitude does not have any relationship, so in the GIS application we need to use the latitude and longitude information of the entity to convert the position screen logical coordinate information, if you know the projection algorithm can also provide their own function processing, But all GIS components provide similar API functions for invocation, and of course this part is not standardized, and different GIS components need to call the API to differ, but the rationale is consistent, for openlayers we pass Map.getpixelfromlonlat ( Data.lonlat) can convert latitude and longitude information into screen pixel logical coordinates, which is HT. node needs the position coordinate information.
The careful classmate will think that the conversion is bidirectional, it is possible that the user needs to drag the element node to change its latitude and longitude information, this time we need another direction function, that is, according to the screen logical coordinates converted to the current coordinates corresponding latitude and longitude, In Openlayers we pass Map.getlonlatfrompixel (new Openlayers.pixel (x, y));
Show done after the rest is the problem of interaction, HT own set of interactive system, Openlayers also need map roaming and scaling interaction, how to combine them? If you can keep the function of both the best, the answer is certain, we only need to add MouseDown or Touchstart event monitoring, if Graphview.getdataat (e) Selected elements we pass the e.stoppropagation (); Stop the propagation of the event so that the map does not respond, when HT takes over the interaction, and if no entities are selected, the map takes over the interaction of the maps operation.
The above interaction design seems to be perfect, and the results run to find a few places that have been frustrating me for a long time to find a solution to the pit:
- Set Map.events.fallThrough = true; otherwise the map will not pass events to the Graphview component of HT
- Graphview.getview (). Style.zindex = 999; Need to specify a certain zindex otherwise it will be obscured
- Graphview.getview (). ClassName = ' olscrollable '; Otherwise the wheel does not respond to map scaling
- Set HT. default.basezindex:1000 otherwise the tooltip will be obscured
In order to make this example user experience more friendly, I also toss some technical points for reference:
- Open source free http://llllll.li/randomColor/random color class library, the library has a lot of very good color get function, I just simply display a different color for each province
- IsVisible, isnotevisible, and islabelvisible are overloaded to show more detail only when scaled to a certain level, otherwise all city information is displayed at full visibility when zoomed out, and how much can improve display performance
The following is the final effect of the capture, video and source code: http://v.youku.com/v_show/id_XODM5Njk0NTU2.html
functioninit () {Graphview=NewHt.graph.GraphView (); varView =Graphview.getview (); Map=NewOpenlayers.map ("Map"); varOL_WMS =NewOpenLayers.Layer.WMS ("Openlayers WMS", "Http://vmap0.tiles.osgeo.org/wms/vmap0", {layers:"Basic"} ); Map.addlayers ([ol_wms]); Map.addcontrol (NewOpenLayers.Control.LayerSwitcher ()); Map.zoomtomaxextent (); Map.events.fallThrough=true; Map.zoomtoproxy=Map.zoomto; Map.zoomto=function(zoom,xy) {view.style.opacity= 0; Map.zoomtoproxy (zoom, XY); Console.log (zoom); }; Map.events.register ("MoveStart", This,function() { }); Map.events.register ("Move", This,function() { }); Map.events.register ("MoveEnd", This,function() {view.style.opacity= 1; Reset (); }); Graphview.getview (). ClassName= ' olscrollable '; Graphview.setscrollbarvisible (false); Graphview.setautoscrollzone (-1); Graphview.handlescroll=function(){}; Graphview.handlepinch=function(){}; GRAPHVIEW.MI (function(e) {if(E.kind = = = ' Endmove ')) {Graphview.sm (). each (function(data) {if(Datainstanceofht. Node) {varPosition =data.getposition (), X= Position.x +graphview.tx (), Y= Position.y +Graphview.ty (); Data.lonlat= Map.getlonlatfrompixel (Newopenlayers.pixel (x, y)); } }); } }); Graphview.enabletooltip (); Graphview.gettooltip=function(event) {vardata = This. Getdataat (event); if(data) {return' City: ' + data.s (' note ') + 'Longitude:' + Data.lonLat.lon + 'Dimensions:' +Data.lonLat.lat; } return NULL; }; Graphview.isvisible=function(data) {returnMap.zoom > 1 | | This. isSelected (data); }; Graphview.isnotevisible=function(data) {returnMap.zoom > 6 | | This. isSelected (data); }; Graphview.getlabel=function(data) {return' Longitude: ' + Data.lonLat.lon + ' \ n Dimension: ' +Data.lonLat.lat; }; Graphview.islabelvisible=function(data) {returnMap.zoom > 7 | | This. isSelected (data); }; View.addeventlistener ("Ontouchend"inchDocument? ' Touchstart ': ' MouseDown ',function(e) {vardata =Graphview.getdataat (e); if(Data | | e.metakey | |E.ctrlkey) {e.stoppropagation (); } }, false); View.style.position= ' absolute '; View.style.top= ' 0 '; View.style.left= ' 0 '; View.style.right= ' 0 '; View.style.bottom= ' 0 '; View.style.zIndex= 999; Map.viewPortDiv.appendChild (view); varcolor =Randomcolor (); Lines= China.split (' \ n ')); for(vari=0; i<lines.length; i++) { line=Lines[i].trim (); if(Line.indexof (' "') = = = 0){ //province = line.substring (1, line.length-1); color =Randomcolor (); }Else{ varSS = Line.split (' '); if(Ss.length = = 3) {CreateNode (parsefloat (ss[1].SUBSTR (3)), Parsefloat (SS[2].SUBSTR (3)), SS[0].SUBSTR (3), color); } } } }functionReset () {GRAPHVIEW.TX (0); Graphview.ty (0); GRAPHVIEW.DM (). each (function(data) {if(Data.lonlat) {data.setposition (Map.getpixelfromlonlat (Data.lonlat)); } }); Graphview.validate ();}functionCreateNode (Lon, LAT, name, color) {varnode =Newht. Node (); NODE.S ({' Shape ': ' Circle ', ' Shape.background ': Color,' Note ': Name,' Label.background ': ' Rgba (255, 255, 0, 0.5) ', ' Select.type ': ' Circle ' }); Node.setsize (10, 10); varLonlat =NewOpenlayers.lonlat (lon, LAT); Lonlat.transform (' epsg:4326 ', Map.getprojectionobject ()); Node.setposition (Map.getpixelfromlonlat (Lonlat)); Node.lonlat=Lonlat; GRAPHVIEW.DM (). Add (node); returnnode;}
HT for Web integration openlayers GIS map application