使用 google locationManager 获取 0.0 位置

Posted

技术标签:

【中文标题】使用 google locationManager 获取 0.0 位置【英文标题】:Get ride of 0.0 location using google locationManager 【发布时间】:2017-06-11 11:35:16 【问题描述】:

我正在使用谷歌位置来获取基于最佳提供商的位置,所以我在服务上运行它,该服务负责根据定义的距离和时间更新位置。

当我更新我的位置时,一切都运行良好,但是我想乘坐 0.0 位置,当我不更新位置时,我需要获取当前位置,或者使用 lastknowlocation 我该怎么做下面是我的代码?

    public class MyService extends Service
    
        private static final String TAG = "BOOMBOOMTESTGPS";
        private LocationManager mLocationManager = null;
        private static final int LOCATION_INTERVAL = 1000;
        private static final float LOCATION_DISTANCE = 10f;

        private final IBinder mBinder = new LocalBinder();
        private Intent intent;
        Context context;


        Location mLastLocation;

        public class LocalBinder extends Binder 
            public MyService getServerInstance() 
                return MyService.this;
            
        

        public Location getLocation()
            return mLastLocation;
        


        private class LocationListener implements android.location.LocationListener
        

            public LocationListener(String provider)
            
                Log.e(TAG, "LocationListener " + provider);
                mLastLocation = new Location(provider);
            

            @Override
            public void onLocationChanged(Location location)
            
                Log.e(TAG, "onLocationChanged: " + location);
                mLastLocation.set(location);
            


            @Override
            public void onProviderDisabled(String provider)
            
                Log.e(TAG, "onProviderDisabled: " + provider);
            

            @Override
            public void onProviderEnabled(String provider)
            
                Log.e(TAG, "onProviderEnabled: " + provider);
            

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras)
            
                Log.e(TAG, "onStatusChanged: " + provider);
            
        

        LocationListener[] mLocationListeners = new LocationListener[] 
                new LocationListener(LocationManager.GPS_PROVIDER),
                new LocationListener(LocationManager.NETWORK_PROVIDER)
        ;


        @Override
        public int onStartCommand(Intent intent, int flags, int startId)
        
            Log.e(TAG, "onStartCommand");
            context = getApplicationContext();
            super.onStartCommand(intent, flags, startId);
            return START_STICKY;
        


        @Override
        public IBinder onBind(Intent intent) 
            return mBinder;
        



        @Override
        public void onCreate()
        
            Log.e(TAG, "onCreate");
            initializeLocationManager();
            try 
               //mLastLocation = mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                mLocationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                        mLocationListeners[1]);
             catch (java.lang.SecurityException ex) 
                Log.i(TAG, "fail to request location update, ignore", ex);
             catch (IllegalArgumentException ex) 
                Log.d(TAG, "network provider does not exist, " + ex.getMessage());
            
            try 
                //mLastLocation = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                mLocationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, LOCATION_INTERVAL, LOCATION_DISTANCE,
                        mLocationListeners[0]);
             catch (java.lang.SecurityException ex) 
                Log.i(TAG, "fail to request location update, ignore", ex);
             catch (IllegalArgumentException ex) 
                Log.d(TAG, "gps provider does not exist " + ex.getMessage());
            
        

        @Override
        public void onDestroy()
        
            Log.e(TAG, "onDestroy");
            super.onDestroy();
            if (mLocationManager != null) 
                for (int i = 0; i < mLocationListeners.length; i++) 
                    try 
                        mLocationManager.removeUpdates(mLocationListeners[i]);
                     catch (Exception ex) 
                        Log.i(TAG, "fail to remove location listners, ignore", ex);
                    
                
            
        

        private void initializeLocationManager() 
            Log.e(TAG, "initializeLocationManager");
            mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            if (mLocationManager == null) 
                mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
            
        


**main activity call of the service**

public class MainActivity extends AppCompatActivity 

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


    public void nextAct(View view)
        Intent i = new Intent(this,GpsGetCoordinates.class);
        startActivity(i);
    

    public static final int REQ_PERMISSION = 99;


    private boolean checkPermission() 
        // PEDE PERMISSÃO SE A MESMA AINDA NÃO TIVER SIDO CONCEDIDA
        return (ContextCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED );
    

    // PEDIDO DE PERMISSÃO
    private void askPermission() 
        ActivityCompat.requestPermissions(
                this,
                new String[]  Manifest.permission.ACCESS_FINE_LOCATION ,
                REQ_PERMISSION);
    

    // VERIFICAÇÃO DA RESPOSTA DO UTILIZADOR AO PEDIDO DE PERMISSÃO
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull
            String[] permissions, @NonNull int[] grantResults) 
        super.onRequestPermissionsResult(requestCode, permissions,
                grantResults);
        switch ( requestCode ) 
            case REQ_PERMISSION: 
                if ( grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED )
                    Log.d("PERMISSAO","DADA");
                    startService(new Intent(this, MyService.class));
                 else 
                    // PERMISSÃO NEGADA
                    Log.w("fail", "permissionsDenied()");
                
                break;
            
        
    


    public void onResume() 
        super.onResume();
    

【问题讨论】:

【参考方案1】:

当您获得一个 !=0.0 的位置时,将其存储在变量中、共享首选项中或您想要的任何其他位置。然后下次从 locationManager 获取 0.0 时,只需使用您之前存储的最新实际值。

【讨论】:

以上是关于使用 google locationManager 获取 0.0 位置的主要内容,如果未能解决你的问题,请参考以下文章

在 Android 中使用 LocationManager 优先于提供者

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

如何在 Android 中通过 Wifi 获取位置?

无法创建 Google 地图实例

使用 Google Maps iOS 绘制路线方向

是否可以使用 Google 的新融合位置提供程序模拟位置?