Current Location displayed at false on-screen coordinates
I'm just trying to make sense of a basic Android navigation-related question, namely "How can I display the current location". I've used a bit of code from articles and tutorials from my long googling sessions. The display of a simple overlay (circle + text message) is works, yet at a wrong on-screen position (on the Equator apparently).
My code setup includes a small inner class that implements LocationListener
, and its onLocationChanged
event handler calls this method :
protected void createAndShowCustomOverlay(Location newLocation)
{
double lat = newLocation.getLatitude();
double lng = newLocation.getLongitude();
// geoPointFromLatLng is an E6 converter :
// return new GeoPoint((int) (pLat * 1E6), (int) (pLng * 1E6));
GeoPoint geopoint = GeoFunctions.geoPointFromLatLng(lat, lng);
CustomOverlay overlay = new CustomOverlay(geopoint);
mapView.getOverlays().add(overlay);
mapView.ge开发者_运维知识库tController().animateTo(geopoint);
mapView.postInvalidate();
}
Up to this point, it all looks ok, I've debugged around and the non-transformed lat/lng pair is ok, the E6 variant thereof ok as well. Here is the CustomOverlay class :
public class CustomOverlay extends Overlay
{
private static final int CIRCLERADIUS = 2;
private GeoPoint geopoint;
public CustomOverlay(GeoPoint point)
{
geopoint = point;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow)
{
// Transfrom geoposition to Point on canvas
Projection projection = mapView.getProjection();
Point point = new Point();
projection.toPixels(geopoint, point);
// background
Paint background = new Paint();
background.setColor(Color.WHITE);
RectF rect = new RectF();
rect.set(point.x + 2 * CIRCLERADIUS, point.y - 4 * CIRCLERADIUS,
point.x + 90, point.y + 12);
// text "My Location"
Paint text = new Paint();
text.setAntiAlias(true);
text.setColor(Color.BLUE);
text.setTextSize(12);
text.setTypeface(Typeface.MONOSPACE);
// the circle to mark the spot
Paint circle = new Paint();
circle.setColor(Color.BLUE);
circle.setAntiAlias(true);
canvas.drawRoundRect(rect, 2, 2, background);
canvas.drawCircle(point.x, point.y, CIRCLERADIUS, circle);
canvas.drawText("My Location", point.x + 3 * CIRCLERADIUS, point.y + 3
* CIRCLERADIUS, text);
}
}
It's most definitely a projection error of sorts, since all my geo fix'd coords end up on the Equator, so there are some 0.0 values getting thrown around.
I can provide other details if needed, I'm running the code on the emulator, Maps API Level 8 (Android 2.2) and I get tiles and the CustomOverlay (circle + text) gets displayed, only at a really false position (coords such as 54.foo and 8.bar are way off).
The codebase could be a bit older, then perhaps a projection such as toPixels
isn't required anymore ? No idea.
Thanks in advance !
Solved, turns out the "geo fix" command sent via telnet wants a (Lng, Lat) pair, not a (Lat, Lng) pair. Inconsistency at its best ! Thanks for the input.
If you only want to show current location and keep it updating with a small blinking circule on the screen like Google Maps for Android does, then Android has a default implementation of this feature.
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(context, mapView);
myLocationOverlay.enableMyLocation();
mapView.getOverlays().add(myLocationOverlay);
That's it Android will handle everything.
精彩评论