Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change 1 meter to pixel distance?

Tags:

android

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 to the distance of two pixels depend on the zoom level. How to I convert it, is there anything API I can use.

Canvas.draw(x, y, radius), what value should I put to this method ?

like image 431
Chris Avatar asked Aug 02 '10 05:08

Chris


2 Answers

public static int metersToRadius(float meters, MapView map, double latitude) {
    return (int) (map.getProjection().metersToEquatorPixels(meters) * (1/ Math.cos(Math.toRadians(latitude))));         
}
like image 199
criss Avatar answered Oct 08 '22 12:10

criss


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

like image 37
Marcelo Avatar answered Oct 08 '22 12:10

Marcelo