.netgoogle-mapscoordinatesmercator

Plot Lat/Lng values on a static image map using .NET


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 :)


Solution

  • 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.