-1

I am using Google Maps Image APIs to fetch a static image of a map. I have the center location coordinates and the zoom level.

What I want to do is place other Lat/Lng coordinates on the static map image so I should be converting Lat/Lng coordinates to XY coordinates.

My approach:

First I calculate the World Coordinates of the center point using a Mercator projection similar to this. Then I figure out the bounds of the static image using the formulas below:

PointF SWPoint = new PointF((float)(centerPointWorldCoordinates.X - (mapWidth / 2.0f) / scale), (float)(centerPointWorldCoordinates.Y + (mapHeight / 2.0f) / scale));

 PointF NEPoint = new PointF((float)(centerPointWorldCoordinates.X + (mapWidth / 2.0f) / scale), (float)(centerPointWorldCoordinates.Y - (mapHeight / 2.0f) / scale));

SWPoint and NEPoint are the World Coordinates of the South-West point and the North-East point (image map bounds). Then I calculate the Pixel Coordinates of these points using the following formula :

pixelCoordinate = worldCoordinate * 2^zoomLevel

After fetching the bounds' pixels coordinates, I also calculate the pixel coordinates of each Lat/Lng point (that I want to plot on the map) using the same formulas.

In order to place a certain point on the image, I use:

PointF point = new PointF((float)(pointPixelCoordinates.X - SWpixelCoordinate.X), (float)(pointPixelCoordinates.Y - NEpixelCoordinate.Y));

This way I can figure out the amount of pixels to move the point (from the top left corner).

This approach is working, however, it isn't really accurate, specially when the zoom level is really high. The point is offset by an unacceptable distance.

How can I make this work? Am I missing something here? I would really appreciate the help :)

kristoffz
  • 129
  • 1
  • 8
  • 1
    According to Wikipedia, Google Maps uses a *close variant* of the Mercator projection. Are you using the Google variant, or the standard Mercator? http://en.wikipedia.org/wiki/Google_Maps – Eric J. Sep 25 '14 at 20:25
  • I am using the Google variant detailed in this link https://developers.google.com/maps/documentation/javascript/examples/map-coordinates – kristoffz Sep 25 '14 at 20:27
  • 1
    Are you possibly running into precision errors using `float` rather than `double`? You mention the problem manifests when you zoom in significantly. – Eric J. Sep 25 '14 at 20:34
  • @EricJ. No it's not just about precision error between `float` and `double`. I tried using `double` and the error remained. – kristoffz Sep 25 '14 at 22:27
  • 1
    If you're sure that your projection code is working correctly, check to make sure you're being consistent with the order of your latitude and longitude coordinates. Otherwise I'm not seeing anything jumping out at me. More code might help us analyze it better. – peterjb Sep 26 '14 at 03:23

1 Answers1

1

You can try other equation for latitude. I think it's the only fault. Try useing the mercator projection on the latitude like in this question:Convert lat/lon to pixel coordinate? and don't change the longitude equation.

Community
  • 1
  • 1
Micromega
  • 12,486
  • 7
  • 35
  • 72