Android GPS定位问题

时间:2016-11-13 10:36:02

标签: android android-studio firebase gps

我在使用GPS设置当前位置时遇到问题。 在Android 5.0.1上它的工作方式(关于查找位置的通知,然后正确设置)(小米Redmi Note 3) 但是例如在Android 6.0(索尼Xperia Z5 Compact)上它根本不起作用。没有通知,GPS位置永远不会。在Xperia上,我甚至没有在code(mMap.setMyLocationEnabled(true);)

中按行显示的地图右侧的按钮

在Android 4.4.2(技嘉GsMart Roma R2plus)上也没有通知,但我可以看到屏幕顶部右侧当前位置的按钮。但仍然无法正常工作。

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }

    mMap.setMyLocationEnabled(true);
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    String provider = locationManager.getBestProvider(criteria, false);

    Location location = locationManager.getLastKnownLocation(provider);
    if (location != null) {
        latitude = location.getLatitude();
        longitude = location.getLongitude();
    } else {

    }

    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    emailicek = user.getEmail();
    emailicek = emailicek.replace(".", "");
    mDatabase = FirebaseDatabase.getInstance().getReference();

    if (latitude != 0) {
        if (longitude != 0) {
            mDatabase.child("userdata/" + email + "/latitude").setValue(latitude);
            mDatabase.child("userdata/" + email + "/longitude").setValue(longitude);
        }
    }
    LatLng latLng = new LatLng(latitude, longitude);
    mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    //mMap.animateCamera(CameraUpdateFactory.zoomTo(14));

    if (latitude != 0) {
        mMap.animateCamera(CameraUpdateFactory.zoomTo(14));
    } else {
        mMap.animateCamera(CameraUpdateFactory.zoomTo(1));
    }
}

1 个答案:

答案 0 :(得分:1)

您确定在Manifest中请求了正确的权限吗? 获取您需要的最后一个已知位置

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

表示基于网络的位置,或者:

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

用于基于GPS的位置。

顺便说一下,为什么你应该考虑转移到Play服务以获取你的位置有很多很好的理由,我强烈建议你阅读:

Getting the Last Known Location - Android Developers