Help Setting Up Osmdroid Library For Displaying Opensourcemaps
Solution 1:
I managed to get osmdroid working in a project, but had to have the Google and the OSM views in different activities as if you switch a Mapview from Google to OSM and then try to go back to Google, I got a runtime error saying something like "only one mapview allowed per activity". This results in a lot of duplicate code but it does run OK.
Use the latest osmdroid-android-3.0.3.jar, it's much simpler for drawing overlays. You'll also need to include slf4j-android-1.5.8.jar. For what it's worth here is the code for the lashed up demo that used to start with. It's got a location listener and draws a very simple overlay (line across the screen) If you are familiar with Google Maps, you should be able to adapt it for your purpose.
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;
publicclassDemoMapextendsActivityimplementsLocationListener,
MapViewConstants {
private MapView mapView;
private MapController mapController;
privateMapOverlaymmapOverlay=null;
private LocationManager mLocMgr;
/** Called when the activity is first created. */@OverridepublicvoidonCreate(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);
GeoPointpoint2=newGeoPoint(53554070, -2959520);
mapController.setCenter(point2);
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100,
this);
this.mmapOverlay = newMapOverlay(this);
List<Overlay> listOfOverlays = mapView.getOverlays();
listOfOverlays.add(mmapOverlay);
mapView.invalidate();
}
publicvoidonLocationChanged(Location location) {
intlat= (int) (location.getLatitude() * 1E6);
intlng= (int) (location.getLongitude() * 1E6);
GeoPointgpt=newGeoPoint(lat, lng);
mapController.setCenter(gpt);
mapView.invalidate();
}
publicclassMapOverlayextendsorg.osmdroid.views.overlay.Overlay {
publicMapOverlay(Context ctx) {
super(ctx);
// TODO Auto-generated constructor stub
}
@Overrideprotectedvoiddraw(Canvas pC, MapView pOsmv, boolean shadow) {
if (shadow)
return;
Paint lp3;
lp3 = newPaint();
lp3.setColor(Color.RED);
lp3.setAntiAlias(true);
lp3.setStyle(Style.STROKE);
lp3.setStrokeWidth(1);
lp3.setTextAlign(Paint.Align.LEFT);
lp3.setTextSize(12);
finalRectviewportRect=newRect();
finalProjectionprojection= 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);
}
}
@OverridepublicvoidonProviderDisabled(String arg0) {}
@OverridepublicvoidonProviderEnabled(String provider) {}
@OverridepublicvoidonStatusChanged(String provider, int status, Bundle extras) {}
}
And the xml (copymain.xml)
<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"
><org.osmdroid.views.MapViewxmlns: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>
Post a Comment for "Help Setting Up Osmdroid Library For Displaying Opensourcemaps"