How to change 1 meter to pixel distance?

后端 未结 5 2172
一个人的身影
一个人的身影 2020-12-25 09:50

When I develop an Android map application, I want to draw a circle on the map whose radius is 1 meter. As you known, I can\'t draw 1 meter directly, I should convert 1 meter

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-25 10:16

    Assuming that your map is Google Maps, they use the Mercator projection, so you'd need to use that for the conversion. Under the Mercator projection, the distance that a pixel represents in meters varies with latitude, so while a meter is a very small distance compared to the Earth radius, latitude is important.

    All the examples below are javascript, so you might need to translate them.

    Here is a general explanation of the coordinate system:

    http://code.google.com/apis/maps/documentation/javascript/maptypes.html#WorldCoordinates

    This example contains a MercatorProjection object, which includes the methods fromLatLngToPoint() and fromPointToLatLng():

    http://code.google.com/apis/maps/documentation/javascript/examples/map-coordinates.html

    Once you have converted your (x,y) to (lat,lon), this is how you draw a circle:

    // Pseudo code
    var d = radius/6378800; // 6378800 is Earth radius in meters
    var lat1 = (PI/180)* centerLat;
    var lng1 = (PI/180)* centerLng;
    
    // Go around a circle from 0 to 360 degrees, every 10 degrees
    for (var a = 0 ; a < 361 ; a+=10 ) {
        var tc = (PI/180)*a;
        var y = asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc));
        var dlng = atan2(sin(tc)*sin(d)*cos(lat1),cos(d)-sin(lat1)*sin(y));
        var x = ((lng1-dlng+PI) % (2*PI)) - PI ;
        var lat = y*(180/PI);
        var lon = x*(180/PI);
    
        // Convert the lat and lon to pixel (x,y) 
    }
    

    These two mashups draw a circle of a given radius on the surface of the Earth:

    http://maps.forum.nu/gm_sensitive_circle2.html

    http://maps.forum.nu/gm_drag_polygon.html

    If you choose to ignore the projection then you'd use cartesian coordinates and simply draw the circle using Pythagoras Theorem:

    http://en.wikipedia.org/wiki/Circle#Cartesian_coordinates

提交回复
热议问题