我正在使用 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));
这是美国斯坦福德的地理坐标现在红点显示在非洲某处,地图的中心(纬度和经度的交点)。
我做错了什么?