帮助显示OpenSourceMaps设置OSMdroid库 [英] Help setting up OSMdroid library for displaying OpenSourceMaps
问题描述
哎。我无法建立OSMdroid库显示OpenSourceMaps。我工作的一个活动,将允许用户查看地图的当前位置机智按钮允许用户正常的谷歌地图视图,地形谷歌地图视图和开放街道地图视图之间切换。我目前使用的JAR文件,只是将它添加到我的项目的构建路径。它编译罚款,但是我不知道如何使用图书馆。很少有教程/方向网上。我是否需要作出IMapView标签中的布局和使用它像一个谷歌地图的看法?如何切换正常谷歌地图视图布局砖?
Hey. I am having trouble setting up the OSMdroid library to display OpenSourceMaps. I am working on an activity that will allow the user to see a map of their current location wit buttons to allow the user to switch between normal google maps view, terrain google maps view, and openstreetmaps view. I am currently using the JAR file and just adding it to the build path of my project. It compiles fine however I don't understand how to use the library. There are very few tutorials/directions online. Do I need to make an IMapView tag in the layout and use it like a Google Map view? How do I switch the layout tiles from normal google maps view?
如果有人能够给我的这个图书馆是如何工作的这将是更AP preciated很短的演练。我在JAVA编码已经很多年,所以我只是需要什么,我需要做的英文说明。
If someone could just give me a very short walkthrough of how this library works it would be much appreciated. I have been coding in JAVA for numerous years so I just need an english description of what I need to do.
先谢谢了。
推荐答案
我设法得到osmdroid在一个项目工作,却不得不因为如果你从谷歌切换图形页面来OSM有谷歌,并在不同的活动中OSM意见然后尝试回到谷歌,我得到一个运行时错误说什么只有一个每个活动允许图形页面。这导致了许多重复code,但它不运行正常。
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.
使用最新的osmdroid-Android的3.0.3.jar,它的绘制覆盖要简单得多。您还需要包括SLF4J-Android的1.5.8.jar。对于什么是值得这里是code的猛烈抨击了演示所使用的开始。它有一个位置监听器,并绘制一个很简单的叠加(线在屏幕上)如果你熟悉谷歌地图,你应该能够适应它你的目的。
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;
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)
And the 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>
这篇关于帮助显示OpenSourceMaps设置OSMdroid库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!