[mapguide-users] Re: Getting layer's maxscale to Viewer API

Kenneth Skovhede, GEOGRAF A/S ks at geograf.dk
Thu Feb 18 10:25:54 EST 2010


Performance of codebehind is much better, and so is the general
dependency etc. If at all possible, I recommend that you use codebehind.
Also, when writing ajax handlers, be sure to use .ashx as they have less 
overhead than
the .aspx pages.

In your code (regardless of where it is placed), you can either
use the public variable approach I outlined, or use the "response.write"
method to write data to the output stream.

Try to install and use FireBug (for FireFox) or Fiddler (for IE),
with either tool, you can see the data being transfered, and that should
help you understand what data you are transmitting.

I'm not sure why you use the JavaScripSerializer, as you already have 
the string in a JSON format?
The value should not be enclosed in strings, eg, it should be:
Dim MString as String = "{""scale:""" & 
MaxSC.ToString(System.Globalization.CultureInfo.InvariantCulture) & "}"

Then:
Response.Clear()
Response.Write(MString)
Response.Flush()
Response.End()

In the JS code, you attempt to do something directly after you issue the 
AJAX call.
Since AJAX is async you do not yet have the data.
You must wait for the data to be fetched from the server, which is what the
onreadystatechanged function is for.

Inside this function, you "unwrap" the JSON string into a javascript object.
Your maxsc2 variable is the object, so you can call:

var scale = maxsc2.scale; //Your code names the variable "double", eg: 
var scale = maxsc2.double;
parent.KartenFrame.viewerFrame.mapFrame.ZoomToView(xcoord,ycoord,scale,true);

Remember that the code after request.send() gets executed immediately, and
does NOT wait for the server to finish processing.

You must do all the processing inside the onreadystatechanged function.
Once the data is returned, you can call the zoom function, but not before.

I recommend that you also send the x/y values back and forth, so you can 
easily access them
inside the onreadystatechanged callback. Otherwise you need to worry 
about function scopes
and javascript enclosures (advanced JS topic).

I recomend that you use some JS framework to achieve this, as that will
handle function scope better than the raw method you use here.
A JS framework will also give you a nice error handler, and will be 
cross browser.

Prototype seems to be popular:
http://www.prototypejs.org/api/ajax/request

It's ajax request object has "onSuccess" and "onFailure", so
you can give the user a nice message if the server fails, or the network 
link breaks.

Regards, Kenneth Skovhede, GEOGRAF A/S

On 18-02-2010 15:57, Johannes Foell wrote:
> You have exactly pointed out what i need.
>
> Unfortunately I'm using code inline and not codebehind. The first way, which
> you presented doesn't work in my code.
>
> As newbie to JSON i've tested your second solution, but i do'nt get the
> scale to the client there, too.
> Does anyone here knows, how to deliver a JSON-object from the server to the
> client? What is wrong in my code?
>
> The important lines are:
> In VB.NET:
>
> Sub reload (ByVal o As Object, ByVal e As EventArgs)
>
>    MaxSc is the double-variable, which contains the MaxScale for the Layer.
>
>    Dim MString As String = "{" + Chr(34) + "double" + Chr(34) + ":" + Chr(34)
> + MaxSc.ToString + Chr(34) + "}" ' e.g. the JSON-Object:
> {"double":"31456.57"}
>
>    Dim serializer As New JavaScriptSerializer
>
>    Dim json As String = serializer.Serialize(MString)
> End Sub
>
>
> In JS:
>
> function ZoomToView()
> {
> var url; //the current aspx-file
> var maxsc;
> var maxsc2;
> try {
>    var request = new XMLHttpRequest();
> } catch(ex) {
>    try {
>      var request = new ActiveXObject("MSXML2.XMLHTTP");
>    } catch (ex) {
>      var request = new ActiveXObject("Microsoft.XMLHTTP");
>    }
> }
> request.onreadystatechange = function() {
>
> if(request.readyState == 4) {
>    maxsc2 = eval("(" + request.responseText + ")"); //what do i have to call
> here?
> }
> }
> request.open("GET", url, true);
> request.send();
> maxsc = maxsc2.double; //the double-field of the object
> maxsc -= 0.1; //to show the layer in the map
> parent.KartenFrame.viewerFrame.mapFrame.ZoomToView(xcoord,ycoord,maxsc,true);
> //the coordinates are no problem. if i set here a number as maxsc, e.g.
> 15000, it works
> }
>
>
>
> Thanks for any help.
> Regards,
> Johannes Foell
>
>
>
> Kenneth Skovhede, GEOGRAF A/S wrote:
>    
>> Not sure this is what you are asking but, this is how I understand your
>> question.
>>
>> Basically the output of an ASPX page is html (or any format really).
>> Since you want to output the scale, you would want the following html
>> output:
>> <html><body><script>
>> var maxScale =<%=serverGeneratedMaxScale%>;
>> GetMap().ZoomToView(x, y, maxScale);
>> </script></body></html>
>>
>> (I've left out the GetMap() and x, y code).
>>
>> The<%=serverGeneratedMaxScale%>  gets processed by the ASP.Net runtime,
>> and inserts the value of the variable named "serverGeneratedMaxScale" in
>> th output.
>>
>> In your codebehind file, just declare a public instance variable with
>> that name,
>> and insert the above code in the aspx (below the codebehind declaration),
>> and it should come out.
>>
>>
>> Another slightly more sophisticated way of doing this, is to issue and
>> AJAX call
>> and have the page return a JSON object string like:
>> {x: 333, y: 444, maxScale: 555}
>>
>> Then in your Ajax callback handler do this:
>> var obj = eval('(' + responseText + ')');
>> GetMap().ZoomToView(obj.x, obj.y, obj.maxScale);
>>
>> I can't remember what the AJAX stuff is called in the viewer, but there
>> are
>> plenty of free AJAX helper code avalible online.
>>
>> The benefit of using an AJAX call is that the user cannot pres "back",
>> there is no "click" sound, and you don't need to mess with hidden frames.
>>
>> Regards, Kenneth Skovhede, GEOGRAF A/S
>>
>> On 15-02-2010 16:25, Johannes Foell wrote:
>>      
>>> Hi List,
>>>
>>> I'm new to MG (and JS) and have a question about the layer's maxscale. I
>>> can
>>> read the maxscale out of the Layerdefinition using a XML-File. Having the
>>> maxscale read out for each layer, I need the highest maximum for the
>>> layergroup at the Viewer API to zoom to a given coordinate and this
>>> maxscale.
>>> Currently i compare the different maxscales and save the largest number
>>> as
>>> double. Writing this double to a new XML-File, i can't read it to the
>>> Viewer
>>> API.
>>>
>>> Is there a way to get this maxscale to the Viewer API and the method
>>> ZoomToView()?
>>>
>>> I'm using VB.Net and JS.
>>>
>>> Or is there just another solution?
>>>
>>> Thanks for any hint in advance.
>>> Best regards,
>>> Johannes Foell
>>>
>>>        
>> _______________________________________________
>> mapguide-users mailing list
>> mapguide-users at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/mapguide-users
>>
>>
>>      
>    


More information about the mapguide-users mailing list