使用Open Street Map在错误的地理位置显示叠加层

时间:2011-09-07 13:15:03

标签: android openstreetmap osmdroid

我使用osmdroid-android 3.0.5.jar而不是Google map api来使用地理坐标显示地图中的位置。

我能够在Android模拟器中获取地图,但问题是我用来指向位置的叠加是错误的。

这是我的代码:

public class MainActivity extends Activity {
/** Called when the activity is first created. */
private MapView osmMapView;
private MapController controller;
private Projection projection;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    osmMapView = (MapView) findViewById(R.id.osm_map_view);

    this.osmMapView.setTileSource(TileSourceFactory.MAPNIK);
    this.osmMapView.setBuiltInZoomControls(true);
    this.osmMapView.setMultiTouchControls(true);

    this.projection = this.osmMapView.getProjection();

    this.controller = this.osmMapView.getController();
    this.controller.setZoom(5);

    List<Overlay> overLayList = osmMapView.getOverlays();
    overLayList.add(new ScaleBarOverlay(this));
    overLayList.add(new PutOverLayOnMap(this));
}

private class PutOverLayOnMap extends Overlay {
    public PutOverLayOnMap(Context ctx) {
        super(ctx);
    }

    @Override
    protected void draw(Canvas c, MapView osmv, boolean shadow) {
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setDither(true);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setStrokeJoin(Paint.Join.ROUND);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStrokeWidth(10);
        Point point = new Point();
        GeoPoint geoPoint = new GeoPoint((int)(41.057121 * 1E6), (int)(-73.561867 * 1E6));
        projection.toPixels(geoPoint, point);
        c.drawPoint(point.x, point.y, paint);
        System.out.println("GeoPoint(OSM) : "+geoPoint.getLatitudeE6()+" #### "+geoPoint.getLongitudeE6());

    }
}

以下是我的main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<org.osmdroid.views.MapView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_centerInParent="true"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true"
    android:id="@+id/osm_map_view"
    renderer="CloudMadeStandardTiles"
    cloudmadeStyle="1"
/>
</LinearLayout>

如您所见,我正在使用

GeoPoint geoPoint = new GeoPoint((int)(41.057121 * 1E6), (int)(-73.561867 * 1E6));

这是美国斯坦福德的地理坐标现在红点显示在非洲的某个地方,地图的中心(纬度和经度的交点)。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您需要在draw方法中获得投影。 getProjection的javadoc告诉你

public MapView.Projection getProjection() 获取用于在屏幕像素坐标和纬度/经度坐标之间进行转换的投影。您不应该为多个绘图保留此对象,因为地图的投影可能会发生变化。

所以在你的draw方法中,插入如下所示的行

....
paint.setStrokeWidth(10);
Point point = new Point();
projection = osmMapView.getProjection(); // <<<<<<<<<<<<<<<<< ADD THIS LINE
GeoPoint geoPoint = new GeoPoint((int) (41.057121 * 1E6),
                        (int) (-73.561867 * 1E6));
projection.toPixels(geoPoint, point);
.....

这应该解决它。