OSMdroid 无法在 android 工作室中获取当前位置
OSMdroid Cannot get current location in android studio
首先,我是 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
这里:
首先,我是 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
osmdroid 示例应用程序的源代码中有这两种示例,位于此处: https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/java/org/osmdroid/intro/PermissionsFragment.java#L94
这里: