首先,我是OSMdroid的新手。
我知道这个主题还有其他类似的问题,但是最近没有问题,旧问题的答案在我的代码中不起作用...
我无法使用OSMDroid获取当前位置。
我使用了github上给出的简单教程以及先前问题中给出的建议。
我拥有最新版本的OSMDroid,但是当我尝试为当前位置添加标签时,什么也没出现。
我什至没有收到错误,地图只显示了当前位置,没有标记。救命!这是我的代码。
任何帮助将不胜感激,因为它很烦人!
Java Class
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context ctx = getApplicationContext();
//important! set your user agent to prevent getting banned from the osm servers
Configuration.getInstance().load(ctx,
PreferenceManager.getDefaultSharedPreferences(ctx));
setContentView(R.layout.activity_map);
mMapView = (MapView) findViewById(R.id.mapview);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
IMapController mapController = mMapView.getController();
mapController.setZoom(9);
GeoPoint startPoint = new GeoPoint(52.1237453, 6.9293683);
mapController.setCenter(startPoint);
this.mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(ctx),mMapView);
this.mLocationOverlay.enableMyLocation();
mMapView.getOverlays().add(this.mLocationOverlay);
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
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tilesource="Mapnik" />
</LinearLayout>
最佳答案
两个可能的原因。
1)您的清单缺少对ACCESS_FINE_LOCATION
的权限
2)如果目标SDK为23或更高版本,则必须在创建GpsMyLocationProvider
之前要求用户级别的GPS权限
osmdroid示例应用程序的源代码中都有这两个示例,位于:
https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/intro/PermissionsFragment.java#L94
和这里:
https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/AndroidManifest.xml#L24