代码之家  ›  专栏  ›  技术社区  ›  LAffair

当前位置出错?

  •  0
  • LAffair  · 技术社区  · 8 年前

    我想知道我现在的位置

    locationListener = new LocationListener() {
    
                @Override
                public void onLocationChanged(Location loc) {
    
                    loc.getLatitude();
                    loc.getLongitude();
    
                    LatLng coordinate = new LatLng(loc.getLatitude(), loc.getLongitude());
                    CameraUpdate currentLocation = CameraUpdateFactory
                            .newLatLngZoom(coordinate, 16);
                    mMap.animateCamera(currentLocation);
    
                    String Text = "My current location is: " +
                            "Latitude = " + loc.getLatitude() +
                            "Longitude = " + loc.getLongitude();
                    Log.d("Dana","acc="+loc.getAccuracy());
    
                    Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onProviderDisabled(String provider) {
                    Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onProviderEnabled(String provider) {
                    Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onStatusChanged(String provider, int status, Bundle extras) {
    
                }
            };
            locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED
                    && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                        1);
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                        1);
            }
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                    0, 0, locationListener);
    

    但在模拟器和我的真实设备上,我的位置在英国的某个地方,即使我的手机已经激活了位置。为什么会这样?为什么不检索我当前的真实位置?我已经记录了准确度,只有20。。。

    此外,是否有方法存储该位置,即使活动已关闭?因为我已经看到,当我关闭活动并重新打开时,它不会再次调用onLocationChanged函数。

    2 回复  |  直到 8 年前
        1
  •  0
  •   Danger    8 年前

        2
  •  0
  •   vahitdurmus    8 年前

    如果你愿意的话,你可以用GoogleLocationAPI来代替这个,我认为,它比你以前使用的效率更高。在谷歌上搜索,你会发现例子。

    推荐文章