【问题标题】:Showing overlay in wrong geo point using Open Street Map使用 Openstreetmap 在错误的地理点显示叠加层
【发布时间】:2011-09-07 13:15:02
【问题描述】:

我正在使用 osmdroid-android 3.0.5.jar 而不是 Google 地图 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));

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

我做错了什么?

【问题讨论】:

    标签: android openstreetmap osmdroid


    【解决方案1】:

    你需要在draw方法中获取投影。 getProjection 的 javadocs 告诉你

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

    这应该可以解决它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多