我設法讓osmdroid在一個項目中工作,但必須在不同的活動中使用Google和OSM視圖,就好像您將Mapview從Google切換到OSM,然後嘗試返回Google一樣,我得到了一個運行時錯誤表示「每個活動只允許一個mapview」。這會導致很多重複的代碼,但它確實運行正常。
使用最新的osmdroid-android-3.0.3.jar,繪製疊加層更加簡單。你還需要包含slf4j-android-1.5.8.jar。這裏值得一提的是以前的演示代碼。它有一個位置監聽器,並繪製了一個非常簡單的覆蓋圖(橫跨屏幕)如果您熟悉Google地圖,則應該可以根據自己的目的對其進行調整。
package osmdemo.demo;
import java.util.List;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapController;
import org.osmdroid.views.MapView;
import org.osmdroid.views.MapView.Projection;
import org.osmdroid.views.overlay.Overlay;
import org.osmdroid.views.util.constants.MapViewConstants;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
public class DemoMap extends Activity implements LocationListener,
MapViewConstants {
private MapView mapView;
private MapController mapController;
private MapOverlay mmapOverlay = null;
private LocationManager mLocMgr;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.copymain);
mapView = (MapView) this.findViewById(R.id.mapview);
mapView.setTileSource(TileSourceFactory.MAPNIK);
mapView.setBuiltInZoomControls(true);
mapView.setMultiTouchControls(true);
mapController = this.mapView.getController();
mapController.setZoom(15);
GeoPoint point2 = new GeoPoint(53554070, -2959520);
mapController.setCenter(point2);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100,
this);
this.mmapOverlay = new MapOverlay(this);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.add(mmapOverlay);
mapView.invalidate();
}
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint gpt = new GeoPoint(lat, lng);
mapController.setCenter(gpt);
mapView.invalidate();
}
public class MapOverlay extends org.osmdroid.views.overlay.Overlay {
public MapOverlay(Context ctx) {
super(ctx);
// TODO Auto-generated constructor stub
}
@Override
protected void draw(Canvas pC, MapView pOsmv, boolean shadow) {
if (shadow)
return;
Paint lp3;
lp3 = new Paint();
lp3.setColor(Color.RED);
lp3.setAntiAlias(true);
lp3.setStyle(Style.STROKE);
lp3.setStrokeWidth(1);
lp3.setTextAlign(Paint.Align.LEFT);
lp3.setTextSize(12);
final Rect viewportRect = new Rect();
final Projection projection = pOsmv.getProjection();
viewportRect.set(projection.getScreenRect());
// Draw a line from one corner to the other
pC.drawLine(viewportRect.left, viewportRect.top,
viewportRect.right, viewportRect.bottom, lp3);
}
}
@Override
public void onProviderDisabled(String arg0) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}
和XML(copymain.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_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mapview"
></org.osmdroid.views.MapView>
</LinearLayout>
感謝。有效。 – kevin78925 2011-07-02 23:28:33