开发者

Showing overlay in wrong geo point using Open Street Map

I am using the osmdroid-android 3.0.5.jar instead of Google map api to show the location in map by using geo-coordinate.

Well i am able to get map in android emulator but the problem is the overlay i am using to point to location is wrong.

Here's my code :

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 p开发者_Go百科aint = 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());

    }
}

The following is my 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>

As you can see I am using

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

which is the geo-coordinate of Stamford US Now the red dot is showing somewhere Africa, center of the map (intersection of latitude & longitude).

What i am doing wrong?


You need to get the projection in the draw method. The javadocs for getProjection tell you

public MapView.Projection getProjection() Get a projection for converting between screen-pixel coordinates and latitude/longitude coordinates. You should not hold on to this object for more than one draw, since the projection of the map could change.

So in your draw method, insert the line as shown below

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

This should fix it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜