Convert long/lat to pixel x/y on a given picture

后端 未结 10 1306
遇见更好的自我
遇见更好的自我 2020-11-29 15:50

I have a city map of Moscow. We modified a Google Maps image with some artistic elements, but the relation between GPS coordinates and pixels remains the same.

10条回答
  •  执念已碎
    2020-11-29 16:23

    So you want to take latitude/longitude coordinates and find out the pixel coordinates on your image of that location?

    The main GMap2 class provides transformation to/from a pixel on the displayed map and a lat/long coordinate:

    Gmap2.fromLatLngToContainerPixel(latlng)
    

    For example:

    var gmap2 = new GMap2(document.getElementById("map_canvas"));
    var geocoder = new GClientGeocoder();
    
    geocoder.getLatLng( "1600 Pennsylvania Avenue NW Washington, D.C. 20500",
        function( latlng ) {
            var pixel_coords = gmap2.fromLatLngToContainerPixel(latlng);
    
            window.alert( "The White House is at pixel coordinates (" + 
                pixel_coodrs.x + ", " + pixel_coords.y + ") on the " +
                "map image shown on this page." );
        }
    );
    

    So assuming that your map image is a screen grab of the Google Map display, then this will give you the correct pixel coordinate on that image of a lat/long coordinate.

    Things are trickier if you're grabbing tile images and stitching them together yourself since the area of the complete tile set will lie outside the area of the displayed map.

    In this case, you'll need to use the left and top values of the top-left image tile as an offset from the coordinates that fromLatLngToContainerPixel(latlng:GLatLng) gives you, subtracting the left coordinate from the x coordinate and top from the y coordinate. So if the top-left image is positioned at (-50, -122) (left, top), and fromLatLngToContainerPixel() tells you a lat/long is at pixel coordinate (150, 320), then on the image stitched together from tiles, the true position of the coordinate is at (150 - (-50), 320 - (-122)) which is (200, 442).

    It's also possible that a similar GMap2 coordinate translation function:

    GMap2.fromLatLngToDivPixel(latlng:GLatLng)
    

    will give you the correct lat/long to pixel translation for the stitched-tiles case - I've not tested this, nor is it 100% clear from the API docs.

    See here for more: http://code.google.com/apis/maps/documentation/reference.html#GMap2.Methods.Coordinate-Transformations

提交回复
热议问题