2 Replies - 1610 Views - Last Post: 14 April 2011 - 06:23 AM

#1 Chopster  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 134
  • Joined: 29-March 08

Mercator Projection, plotting point onto stage.

Posted 13 April 2011 - 03:14 AM

Hi guys,

I'm looking to plot a point on a map, when passed it's latitude and longitude into a function, i am using the mercator projection code to map the position and i have this working fine. But my problem comes when i want to add more than one point to the map. My knowledge of flash/actionscript is limited.

example: the below code shows my document class and it calls the plot point function which figures out the position on the map, calls the draw_point function and places a red square on the stage. But when this function is called again, the previous red square is removed from the stage and the new one is placed. I need to show multiple points on a map as they appear, so the map could have say 100+ points on it, but as i mentioned the previous point gets removed when the function is run again.

If you need any more information please let me know, i am working with AS3.

MercatorProjection.as
package {
	
	import flash.display.*;
	import flash.events.*;
	import flash.net.*;
	import flash.text.*;
	import flash.utils.*;
	//import LoadText;

	
	public class MercatorProjection extends Sprite {
		
		var dot_size = 6;
		var longitude_shift = 0;   // number of pixels your map's prime meridian is off-center.
		var x_pos = 54;
		var y_pos = 0;
		var map_width = 774;
		var map_height = 600;
		var half_dot = dot_size / 2
		
		public function MercatorProjection() {
			
			plot_point(54, -2);
		}
			
		public function draw_point(x, y) {
			trace("draw_point function reached");
			var dot:Shape = new Shape;
			dot.graphics.beginFill(0xFF0000);
			dot.graphics.drawRect(0, 0, dot_size, dot_size);
			dot.graphics.endFill();
			addChild(dot);
		}
			
		public function plot_point(lat, lng) {
			trace("plot_point function reached");
   			// Mercator projection
		   	// longitude: just scale and shift
    		x = (map_width * (180 + lng) / 360 + longitude_shift) % map_width;
		    // latitude: using the Mercator projection
    		lat = lat * Math.PI / 180;  // convert from degrees to radians
    		y = Math.log(Math.tan((lat/2) + (Math.PI/4)));  // do the Mercator projection (w/ equator of 2pi units)
    		y = (map_height / 2) - (map_width * y / (2 * Math.PI)) + y_pos;   // fit it to our map
		   	draw_point(x - half_dot, y - half_dot);

		}
	}
}


Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Mercator Projection, plotting point onto stage.

#2 lordofduct  Icon User is offline

  • I'm a cheeseburger
  • member icon


Reputation: 2533
  • View blog
  • Posts: 4,633
  • Joined: 24-September 10

Re: Mercator Projection, plotting point onto stage.

Posted 13 April 2011 - 06:23 AM

When you draw more then one point, how do you go about it? In this code I see there is only one time you call to draw a point somewhere.
Was This Post Helpful? 0
  • +
  • -

#3 Chopster  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 134
  • Joined: 29-March 08

Re: Mercator Projection, plotting point onto stage.

Posted 14 April 2011 - 06:23 AM

I was looking for a way to change that function to dynamically handle a bunch of calls, so if i had to call it x number of times with different x + y positions it could handle it. Apologies for not being straight, but alas i have solved the problem, if anyone is interested please see code below.

		public function draw_point(xp, yp, id) {
			
			var dot:String = id; //unique ID passed into function
			this[dot] = new Sprite;
			this[dot].graphics.clear();
			this[dot].graphics.beginFill(0xFF0000);
			this[dot].graphics.drawRect(0, 0, dot_size, dot_size);
			this[dot].x = xp;
			this[dot].y = yp;		
			this[dot].graphics.endFill();
			addChild(this[dot]);
		}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1