跟踪android studio中的位置(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)

时间:2018-11-06 10:18:31

标签: android-studio

我正在尝试将google API与android studio一起使用,以创建一串代码来跟踪某人的位置。但是,当我尝试实现locationManager.isProviderEnabled时,即使我具有android.location.LocationManager实现,也会收到错误消息“无法解析符号'isProviderEnabled'”。老实说,我无法弄清楚我的代码出了什么问题,因此,如果有人可以提供帮助,那就太好了!

这是我的代码以供更多参考:

 else if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                //get the latitude
                double latitude = location.getLatitude();
                //get the longitude
                double longitude = location.getLongitude();
                LatLng latLng = new LatLng(latitude, longitude);
                Geocoder geocoder = new Geocoder(getApplicationContext());
                try {
                    List<Address> addressList = geocoder.getFromLocation(latitude, longitude, 1);
                    String str = addressList.get(0).getLocality() + ",";
                    str += addressList.get(0).getCountryName();
                    mMap.addMarker(new MarkerOptions().position(latLng).title(str));
                    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10.2f));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

1 个答案:

答案 0 :(得分:0)

  1. GPSLocationService扩展服务
  2. 将onStart()复制并粘贴到GPSLocationService类
@Override
public void onStart(Intent intent, int startId) {

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
listener = new MyLocationListener();
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    if (locationManager != null) {
      if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){           
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, listener);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, listener);
    }
 }
     

}