dhoenig

I would like to call the map.LatLongToPixel function from my codebehind. Is there a way to do that using PageMethods Or can I get that code in a .net class

(map = new VEMap('myMap'))

Thanks,
Dan

### Re: Call javascript functions from Code behind

Derek Chan

How exactly do you want to call it from codebehind (Do you want to click a button and then call map.LatLongToPixel )

### Re: Call javascript functions from Code behind

SoulSolutions

You can do it in code given the lat long and zoom level:

#region VEPixelCalulations

private const double earthRadius = 6378137; //The radius of the earth - should never change!

private const double earthCircum = earthRadius*2.0*Math.PI; //calulated circumference of the earth

private const double earthHalfCirc = earthCircum/2; //calulated half circumference of the earth

private const int pixelsPerTile = 256;

/// <summary>

/// helper function - converts a latitude at a certain zoom into a y pixel

/// </summary>

/// <param name="lat">Latitude</param>

/// <param name="zoom">Zoomlevel of VE map</param>

/// <returns>Y pixel</returns>

public static int LatitudeToYAtZoom(double lat, int zoom)

{

int y;

double arc = earthCircum/((1 << zoom)*pixelsPerTile);

double metersY = earthRadius/2*Math.Log((1 + sinLat)/(1 - sinLat));

y = (int) Math.Round((earthHalfCirc - metersY)/arc);

return y;

}

/// <summary>

/// helper function - converts a longitude at a certain zoom into a x pixel

/// </summary>

/// <param name="lon">Longitude</param>

/// <param name="zoom">Zoomlevel of VE map</param>

/// <returns>X pixel</returns>

public static int LongitudeToXAtZoom(double lon, int zoom)

{

int x;

double arc = earthCircum/((1 << zoom)*pixelsPerTile);

x = (int) Math.Round((earthHalfCirc + metersX)/arc);

return x;

}

/// <summary>

/// helper function - converts degrees to radians

/// </summary>

/// <param name="d">degrees</param>

{

return d*Math.PI/180.0;

}

#endregion

John.

### Re: Call javascript functions from Code behind

dhoenig

This looks like it will work for me. The reason I want this is to take the load off of the client machine and put it on the server. I have a couple thousand tracking points that need to call this function within a for loop and it takes longer than I would like. So I was hoping if I did it on the server it would be faster. Thanks for the code John.

-Dan

### Re: Call javascript functions from Code behind

dhoenig

John,
After looking at the code you posted, it seems like it is missing some code. The number it returns for the pixels is way to big. I found the following code while stepping through the javascript map.LatLongToPixel function.

originX = MathRound(currentView.center.x-width/2);
a.x = originX + offsetX;

Before these two lines of code run the a.x equals the number that I get when I run the code you gave me. I would add these two lines of code but i can't figure out where currentView.center.x gets it's value. Do I have to add these lines of code to get it to work or am I totally missing something

Thanks for the help.

### Re: Call javascript functions from Code behind

SoulSolutions

Ah yes sorry should have mentioned that.

These functions return the pixels from the edge of the world, so if you need to know something in relation to what is currently on the screen then you really need the top left lat/long (or i guess the centre if your logic works better that way).

Image if your in africa somewhere zoomed in to level 16 or something then the lat/long values may well converted to say 35,000 pixels x and 45,000 pixels y. This is becouse that location is that many pixels form the top left of the entire world. Now if you had the current map views top left you may find this is say 34,500 x and 44,500 y. by taking the differnece of these you now that on the current view the point will be 500,500.

I use these calulations to determine in pins are overlapping at the given zoomlevel on the server side. It works very well. If you were planing to use this to say add lables next to pins on the map you are far better off to call the client side function to do this conversion.

I'm concerned you may be doing something that will be detrimental to the performance of your application, if you can briefly explain what you are tring to do I may be able to explain a better way

John.

### Re: Call javascript functions from Code behind

dhoenig

Yeah, I was thinking about it last night and I think I'll just use the LatLongToPixel function on the client side because the performance gain I get by doing it on the server wouldn't be as good as I would like.

I can't go into detail about what I'm doing but I need to plot hundreds/thousands of points on the map at one time. In that process I have to convert the Lat/Long's I get from the db to pixels and I thought I could speed it up by doing it on the server before I sent the data back to the client. Currently I can plot 2400 points in 25 seconds, which will be fine for now.

Thanks for the help John, I appreciate it.

-Dan

### Re: Call javascript functions from Code behind

SoulSolutions

Try something like this out if you want it to really fly:

http://www.soulsolutions.com.au/Articles/ClusteringVirtualEarthrev2/tabid/103/Default.aspx

I will have some other performance tips lke the pin differential logic (render only pins that change) next week.

John.