Thursday 23 May 2013

An elegant MgMap hack

Ever wanted to set any of the following properties of a MgMap in your application code, but bemoan the fact that all these properties are read-only in the MgMap class?

  • DPI
  • Display Width
  • Display Height
  • View Center
  • View Scale
Here's a neat little hack you can do

If you remember, I previously showed how you can actually replicate mapagent functionality through the MgHttpRequest and MgHttpResponse classes. Armed with this knowledge, the key to changing any of the above properties is to set up the appropriate operation and parameters for an API that's only available in the mapagent: GETDYNAMICMAPOVERLAYIMAGE

GETDYNAMICMAPOVERLAYIMAGE differs from what's in the official API (MgRenderingService::RenderDynamicOverlay()) in that the mapagent version supports these additional parameters:
  • SETVIEWCENTERX
  • SETVIEWCENTERY
  • SETVIEWSCALE
  • SETDISPLAYDPI
  • SETDISPLAYWIDTH
  • SETDISPLAYHEIGHT
  • SHOWLAYERS
  • HIDELAYERS
  • SHOWGROUPS
  • HIDEGROUPS
These parameters actually modify the state of the MgMap that is to be rendered. Through the use of MgHttpRequest and MgHttpResponse we can indirectly tap into this mapagent API to achieve the same result.

Now the operation itself returns a rendered image obviously, so if we want this to be snappy (as we just want to modify MgMap state and don't really care about the rendered result), you'd want to request something that requires very minimal processing overhead to produce the rendered image. So under these conditions, the ideal parameters to supply to GETDYNAMICMAPOVERLAYIMAGE are:
  • BEHAVIOR = 1 (Selected features only)
  • FORMAT=PNG8/JPG (Smallest image format in case we actually do have a rendered selection)
I've attached a PHP code sample that demonstrates this technique.

No comments: