需要针对学校项目的Android Studio / OSMDroid指南

时间:2016-04-26 10:04:12

标签: android gps location maps osmdroid

我目前正在学校/考试目的中开发应用程序。 我使用的是Android Studio 2.0和一个带有5.1.1的OPO和一个带有6.0.1的Nexus 7!

目标是在地图上显示设备的位置,并跟踪路标(文化建筑)的路线。

我在显示地图方面没有问题(我可以将它放在我的城市中心并具有良好的缩放级别)

现在我想显示设备的当前位置。 我遇到权限问题..

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="alpha.testmap">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    // AUTORISATIONS
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

我的MainActivity.java:

package alpha.testmap;

 import android.Manifest;
 import android.app.Activity;
 import android.content.pm.PackageManager;
 import android.location.Location;
 import android.location.LocationListener;
 import android.location.LocationManager;
 import android.os.Bundle;
 import android.support.v4.app.ActivityCompat;

 import org.osmdroid.api.IMapController;
 import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
 import org.osmdroid.util.GeoPoint;
 import org.osmdroid.views.MapView;

 public class MainActivity extends Activity {

 private LocationManager mLocMgr;



@Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
//Can't find the class of this fonction --> REQUEST_LOCATION);
    }else {
        Location myLocation = LOCATION_SERVICE.FusedLocationApi.getLastLocaation(mGoogleApiClient);
    }

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
 //Can't find the class of this fonction --> REQUEST_LOCATION);
    }

    setContentView(R.layout.activity_main);

    MapView map = (MapView) findViewById(R.id.map);
    map.setTileSource(TileSourceFactory.MAPNIK);

    //Adding zoom ability
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);

    //Create a default point
    IMapController mapController = map.getController();
    mapController.setZoom(16);
    GeoPoint ptStart = new GeoPoint(47.215576, -1.549089);
    mapController.setCenter(ptStart);
    GeoPoint ptTourLu = new GeoPoint(47.21545, -1.54624);
    //GPS
    mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
    mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 100, (LocationListener) this);

}

}

1 个答案:

答案 0 :(得分:0)

您的权限问题是由于Android 6.0带来的变化,需要您在运行时询问权限,而不是像之前那样请求安装。

您可以在“清单”部分下阅读更多here