【问题标题】:OSMdroid Cannot get current location in android studioOSMdroid 无法在 android studio 中获取当前位置
【发布时间】:2017-11-23 09:48:24
【问题描述】:

首先,我是 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>

【问题讨论】:

    标签: android osmdroid


    【解决方案1】:

    两个可能的原因。

    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

    【讨论】:

    • 嗨。感谢您的帮助,终于让它在那里工作了。我输入了权限并重建了代码,它现在可以工作了。再次感谢!
    猜你喜欢
    • 2015-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多