[转帖]Overlaying markers using Google Maps API on an arbitrary image

Overlaying markers using Google Maps API on an arbitrary image
December 5, 2008

 

      So, my previous post from Inventoriana land looked at  how you could use Google Maps as a basic navigator for the image of your choosing using what is called a Tile Layer Base. All that is nice and good, but for Inventoriana to really work, it has to be able to munge an XML feed and then, using (x,y) coordinates that refer to the original image, correctly place them on the google map. It sounds easier than it is, but the solution is not terribly difficult either. Here’s what you need to do.

      Step 1: Get an XML Feed

      For this example, we’ll use this one:

Code

      And we want to put it on this image:


      Inventoriana Test Image

       Easy enough, right?

      Step 2: Define a map projection.
      This is probably the most conceptually difficult part. Since Google Maps assumes that you are mapping part of the earth, there are two sets of coordinates being tracked: your current pixels, and also the latitude and longitude. Typically, people use mercator projections with Google maps, and, if you aren’t trying to bring in an overlay from a 2-D data source, you’ll be fine with that. However, translating (x,y) into (lat,lng) is harder than it sounds, not least because ActionScript 3.0 has not-mathematica-level trig functionality.
So the way to really go is to define your own map projection, which is linear in every direction. Here is the code:

 

Code

 

 

      The main meat of the class are the two functions fromLatLngToPixel() and fromPixelToLatLng(). They give google maps the tools they need  to correctly place everything on the map. You’ll notice that we are essentially only using latitude and longitude values between 0 and 90; since images are cut in to squares by GMap Cutter (see previous post) this is still ok.
      So now you have a projection that is entirely linear, which would be terribly irritating for the earth but handy for using images.


      Step 3: Make your map use the projection.

      Now that you have your map projection you can update your map subclass as follows:

      First, make a ProjectionBase property (the numbers are the dimensions of the original image):

    public var inventorianaProjectionBase:InventorianaProjectionBase = new InventorianaProjectionBase(new Point(670,670));  


      Then, after the map object has dispatched the MAP_READY event, set the Map Type to use the projection:

    var inventorianaMapType:MapType = new MapType(tileLayers,inventorianaProjectionBase,”Inventoriana”);

 

      Finally, once your XML data source has dispatched a “ready” event, you can add the overlay:

 

Code

      Your final result should look something like this (it is just a screenshot because I am too lazy to upload my tile base to a publicly accessible location):

 

 

posted @ 2009-06-15 19:46  webgis松鼠  阅读(334)  评论(0)    收藏  举报