我对获取当前位置android有问题[重复]

Posted

技术标签:

【中文标题】我对获取当前位置android有问题[重复]【英文标题】:I have a problem with the get current location android [duplicate] 【发布时间】:2019-12-24 02:13:23 【问题描述】:

我在 android (java) 中的 gps 有问题:我在清单中应用权限并打开网络并允许 gps 的权限,但一直显示“无法获取您的位置”消息我使用API 27 和我使用我的手机 j5 pro 进行模拟。

https://www.youtube.com/watch?v=rg7FFUbIu9o&lc=z225styi1rzijjv5v04t1aokgqisttmwxryvfo0rplzzrk0h00410.1566142987821928这个视频展示了代码源和工作

Java

    package com.example.myapplication9;
    import android.Manifest;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.pm.PackageManager;
    import android.location.Location;
    import android.location.LocationManager;
    import android.os.Bundle;
    import android.provider.Settings;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.app.ActivityCompat;

    public class MainActivity extends AppCompatActivity 

    private static  final int REQUEST_LOCATION=1;

    Button getlocationBtn;
    TextView showLocationTxt;

    LocationManager locationManager;
    String latitude,longitude;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Add permission

        ActivityCompat.requestPermissions(this,new String[]
                Manifest.permission.ACCESS_FINE_LOCATION, REQUEST_LOCATION);

        showLocationTxt=findViewById(R.id.show_location);
        getlocationBtn=findViewById(R.id.getLocation);

        getlocationBtn.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 

                locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);

                //Check gps is enable or not

                if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
                
                    //Write Function To enable gps

                    OnGPS();
                
                else
                
                    //GPS is already On then

                    getLocation();
                
            
        );

    

    private void getLocation() 

        //Check Permissions again

        if (ActivityCompat.checkSelfPermission(MainActivity.this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this,

                Manifest.permission.ACCESS_COARSE_LOCATION) !=PackageManager.PERMISSION_GRANTED)
        
            ActivityCompat.requestPermissions(this,new String[]
                    Manifest.permission.ACCESS_FINE_LOCATION, REQUEST_LOCATION);
        
        else
        
            Location LocationGps= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            Location LocationNetwork=locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            Location LocationPassive=locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);

            if (LocationGps !=null)
            
                double lat=LocationGps.getLatitude();
                double longi=LocationGps.getLongitude();

                latitude=String.valueOf(lat);
                longitude=String.valueOf(longi);

                showLocationTxt.setText("Your Location:"+"\n"+"Latitude= "+latitude+"\n"+"Longitude= "+longitude);
            
            else if (LocationNetwork !=null)
            
                double lat=LocationNetwork.getLatitude();
                double longi=LocationNetwork.getLongitude();

                latitude=String.valueOf(lat);
                longitude=String.valueOf(longi);

                showLocationTxt.setText("Your Location:"+"\n"+"Latitude= "+latitude+"\n"+"Longitude= "+longitude);
            
            else if (LocationPassive !=null)
            
                double lat=LocationPassive.getLatitude();
                double longi=LocationPassive.getLongitude();

                latitude=String.valueOf(lat);
                longitude=String.valueOf(longi);

                showLocationTxt.setText("Your Location:"+"\n"+"Latitude= "+latitude+"\n"+"Longitude= "+longitude);
            
            else
            
                Toast.makeText(this, "Can't Get Your Location", Toast.LENGTH_SHORT).show();
            

            //Thats All Run Your App
        

    

    private void OnGPS() 

        final AlertDialog.Builder builder= new AlertDialog.Builder(this);

        builder.setMessage("Enable GPS").setCancelable(false).setPositiveButton("YES", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 
                startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
            
        ).setNegativeButton("NO", new DialogInterface.OnClickListener() 
            @Override
            public void onClick(DialogInterface dialog, int which) 

                dialog.cancel();
            
        );
        final AlertDialog alertDialog=builder.create();
        alertDialog.show();
    


MainActivity xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout tools:context=".MainActivity" android:gravity="center" 
    android:orientation="vertical" android:layout_ 
    android:layout_ 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView android:layout_ 
    android:layout_ android:id="@+id/show_location" 
    android:textSize="20sp" android:hint="Location"/>

    <Button android:layout_ 
    android:layout_ android:id="@+id/getLocation" 
    android:text="Get Location"/>

    </LinearLayout>

清单

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


    <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:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

    </manifest>

【问题讨论】:

您是否尝试过使用移动网络连接来运行应用程序? 我编辑了已经给出的答案并附上了截图。 【参考方案1】:

试试这个:

locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);

代替你的

locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE);

【讨论】:

我在设备上试过这个。华为 Mate 10 精简版。它工作正常。

以上是关于我对获取当前位置android有问题[重复]的主要内容,如果未能解决你的问题,请参考以下文章

在android的服务中获取重复的当前位置?

Android:获取中国的当前位置

android google map当前位置没有得到[重复]

Android - 获取位置的最简单方法[重复]

如何在android应用程序中获取当前位置?

Mapbox Android:如何获取从当前位置到您选择的目的地的路线?