当前位置显示在屏幕上的虚假坐标上

时间:2010-11-24 01:26:23

标签: android google-maps

我只想弄清楚与Android导航相关的基本问题,即“如何显示当前位置”。我在漫长的谷歌搜索会议中使用了一些来自文章和教程的代码。一个简单的叠加(圆圈+文字信息)的显示是有效的,但在屏幕上的位置错误(显然在赤道上)。

我的代码设置包含一个implements LocationListener的小内部类,其onLocationChanged事件处理程序调用此方法:

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.getController().animateTo(geopoint);

  mapView.postInvalidate();
} 

到目前为止,一切看起来还不错,我已经调试好了,未经转换的lat / lng对也没问题,其E6变体也可以。这是CustomOverlay类:

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);
   }
}

这绝对是各种各样的投影错误,因为我的所有geo fix'd coords最终都出现在Equator上,所以有一些0.0值被抛出。

如果需要,我可以提供其他详细信息,我在模拟器上运行代码,Maps API Level 8(Android 2.2),我得到了瓷砖,并且显示了CustomOverlay(圆圈+文本),只是处于非常错误的位置(像54.foo和8.bar这样的坐标很远)。

代码库可能有点旧,那么可能不再需要toPixels这样的投影了吗?不知道。

提前致谢!

2 个答案:

答案 0 :(得分:1)

解决了,通过telnet发送的“geo fix”命令需要一个(Lng,Lat)对,而不是(Lat,Lng)对。最好的不一致!感谢您的投入。

答案 1 :(得分:0)

如果您只想显示当前位置并通过屏幕上的微小闪烁来更新,例如Google Maps for Android,则Android会默认执行此功能。

    MyLocationOverlay myLocationOverlay = new MyLocationOverlay(context, mapView);
    myLocationOverlay.enableMyLocation();
    mapView.getOverlays().add(myLocationOverlay);

这就是Android将处理所有事情。

相关问题