Java Android-为什么我当前的位置应用程序总是返回0纬度和0经度?

Java Android-为什么我当前的位置应用程序总是返回0纬度和0经度?,java,android,xml,google-maps,Java,Android,Xml,Google Maps,无论何时调用getCurrentLocation()函数,返回值始终为纬度:0.0和经度:0.0 我使用了一个实际的手机和模拟器,它们都给出了0和0 这些都在我的AndroidManifest.xml中 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.thesis.adrianangub.myapplication" > <uses-permi

无论何时调用getCurrentLocation()函数,返回值始终为纬度:0.0和经度:0.0

我使用了一个实际的手机和模拟器,它们都给出了0和0

这些都在我的AndroidManifest.xml中

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.thesis.adrianangub.myapplication" >

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />


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


        <!--
             The API key for Google Maps-based APIs is defined as a string resource.
             (See the file "res/values/google_maps_api.xml").
             Note that the API key is linked to the encryption key used to sign the APK.
             You need a different API key for each encryption key, including the release key that is used to
             sign the APK for publishing.
             You can define the keys for the debug and release targets in src/debug/ and src/release/. 
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="@string/google_maps_key" />

        <activity
            android:name=".MapsActivity"
            android:label="@string/title_activity_maps" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
}

这是我的活动_maps.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.thesis.adrianangub.myapplication.MapsActivity" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#cc3b60a7"
        android:orientation="horizontal">

        <Button
            android:text="Curr"
            android:id="@+id/buttonCurrent"
            android:onClick="getCurrentLocation"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="15dp" />

        <Button
            android:text="Save"
            android:id="@+id/buttonSave"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="15dp" />


        <Button
            android:text="View"
            android:id="@+id/buttonView"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_margin="15dp"/>

    </LinearLayout>
</LinearLayout>


您必须申请棉花糖设备的许可。参考此

进入第一个活动时,添加以下代码:

将以下两个变量放在onCreate()方法的顶部:

if(canMakeSmores()){

        requestPermissions(perms, permsRequestCode);

    }
private boolean canMakeSmores(){

    return(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1);

}       


 public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){

    switch(permsRequestCode){

        case 200:

            if(grantResults.length > 0){

                boolean cameraAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED;

                if(cameraAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean writeAccepted = grantResults[1]==PackageManager.PERMISSION_GRANTED;

                if(writeAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean readAccepted = grantResults[2]==PackageManager.PERMISSION_GRANTED;

                if(readAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean fineLocationAccepted = grantResults[3]==PackageManager.PERMISSION_GRANTED;

                if(fineLocationAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean coarseLocationAccepted = grantResults[4]==PackageManager.PERMISSION_GRANTED;

                if(coarseLocationAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

            }

            break;

    }

}
 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
  • 您可以为棉花糖添加许多权限。下面我在字符串数组中添加了五个权限。借助于此,我将在oncreate方法之外授予权限

  • 我们在字符串数组中添加的权限是什么,它必须添加到清单中

    String[] perms = {"android.permission.CAMERA",
            "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_EXTERNAL_STORAGE",
             "android.permission.ACCESS_FINE_LOCATION", "android.permission.ACCESS_COARSE_LOCATION"};     
    
    int permsRequestCode = 200;
    
将下面的方法放入onCreate()方法中:

if(canMakeSmores()){

        requestPermissions(perms, permsRequestCode);

    }
private boolean canMakeSmores(){

    return(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1);

}       


 public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){

    switch(permsRequestCode){

        case 200:

            if(grantResults.length > 0){

                boolean cameraAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED;

                if(cameraAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean writeAccepted = grantResults[1]==PackageManager.PERMISSION_GRANTED;

                if(writeAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean readAccepted = grantResults[2]==PackageManager.PERMISSION_GRANTED;

                if(readAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean fineLocationAccepted = grantResults[3]==PackageManager.PERMISSION_GRANTED;

                if(fineLocationAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean coarseLocationAccepted = grantResults[4]==PackageManager.PERMISSION_GRANTED;

                if(coarseLocationAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

            }

            break;

    }

}
 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
将以下两个方法置于onCreate()方法之外:

if(canMakeSmores()){

        requestPermissions(perms, permsRequestCode);

    }
private boolean canMakeSmores(){

    return(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1);

}       


 public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){

    switch(permsRequestCode){

        case 200:

            if(grantResults.length > 0){

                boolean cameraAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED;

                if(cameraAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean writeAccepted = grantResults[1]==PackageManager.PERMISSION_GRANTED;

                if(writeAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean readAccepted = grantResults[2]==PackageManager.PERMISSION_GRANTED;

                if(readAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean fineLocationAccepted = grantResults[3]==PackageManager.PERMISSION_GRANTED;

                if(fineLocationAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean coarseLocationAccepted = grantResults[4]==PackageManager.PERMISSION_GRANTED;

                if(coarseLocationAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

            }

            break;

    }

}
 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
清单:

if(canMakeSmores()){

        requestPermissions(perms, permsRequestCode);

    }
private boolean canMakeSmores(){

    return(Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1);

}       


 public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults){

    switch(permsRequestCode){

        case 200:

            if(grantResults.length > 0){

                boolean cameraAccepted = grantResults[0]==PackageManager.PERMISSION_GRANTED;

                if(cameraAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean writeAccepted = grantResults[1]==PackageManager.PERMISSION_GRANTED;

                if(writeAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean readAccepted = grantResults[2]==PackageManager.PERMISSION_GRANTED;

                if(readAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean fineLocationAccepted = grantResults[3]==PackageManager.PERMISSION_GRANTED;

                if(fineLocationAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

                boolean coarseLocationAccepted = grantResults[4]==PackageManager.PERMISSION_GRANTED;

                if(coarseLocationAccepted){

                }else {
                    requestPermissions(perms, permsRequestCode);
                }

            }

            break;

    }

}
 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />


您是否正在签入棉花糖版本设备?一些日志信息将有助于确定problem@Naruto对这是棉花糖device@Sohil稍后我会发送日志信息,它在我的另一台电脑上。LOLL@Loller\u Toaster很乐意帮助!!