简体   繁体   English

Android GPS无法获取我的位置

[英]Android GPS unable to get my location

I have an app that used to work fine as of 2 weeks ago. 我有一个应用程序,直到2周前都可以正常工作。 Today I ran the code and running into some issues. 今天,我运行了代码并遇到了一些问题。 It doesn't seem to be able to detect that my gps (location option) is on startup. 它似乎无法检测到我的gps(位置选项)正在启动。 When the location is off, I will pop up a message on startup and asked to turn on the location option, after I turned it on, it will show my current location. 关闭位置后,我将在启动时弹出一条消息,并要求打开位置选项,将其打开后,它将显示我的当前位置。 However, if I exit the application, and reopen it again, it doesn't show my current location even though the location options is still on. 但是,如果我退出该应用程序,然后再次将其重新打开,即使位置选项仍处于启用状态,它也不会显示我的当前位置。

Again, it used to work, I was always able to get my current location on startup, but I don't know why it doesn't work anymore as I didn't change anything. 同样,它曾经可以工作,我总是能够在启动时获得当前的位置,但是我不知道为什么它不再起作用,因为我没有做任何更改。

The Location location = locationManager.getLastKnownLocation(provider) is always returning null. Location location = locationManager.getLastKnownLocation(provider)始终返回null。

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


    // Getting GoogleMap object from the fragment
    googleMap = fm.getMap();

    // Enabling MyLocation Layer of Google Map
    googleMap.setMyLocationEnabled(true);

    googleMap.setPadding(0, 0, 0, 90);
    //loadSettings();
    // Getting LocationManager object from System Service LOCATION_SERVICE
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        //Toast.makeText(this, "GPS is Enabled in your devide", Toast.LENGTH_SHORT).show();
    }else{
        showGPSDisabledAlertToUser();
    }

    googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng point) {
            changeLocation(point);
        }
    });
    // Creating a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Getting the name of the best provider
    provider = locationManager.getBestProvider(criteria, true);

    // Getting Current Location
    Location location = locationManager.getLastKnownLocation(provider);

    if(location!=null){
            onLocationChanged(location);
    }

    locationManager.requestLocationUpdates(provider, 20000, 0, this);



private void showGPSDisabledAlertToUser(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("GPS is disabled in your device. Would you like to enable it?")
            .setCancelable(false)
            .setPositiveButton("Goto Settings Page To Enable GPS",
                    new DialogInterface.OnClickListener(){
                        public void onClick(DialogInterface dialog, int id){
                            Intent callGPSSettingIntent = new Intent(
                                    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivity(callGPSSettingIntent);
                        }
                    });
    alertDialogBuilder.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int id){
                    dialog.cancel();
                }
            });
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
}

google-services信息库中有更新,也许您可​​以检查一下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM