2017-09-13 109 views
0

我想通过GPS或位置提供商获取用户的当前位置,但是我尝试过的每个解决方案(很多来自stacksoverflow和google,youtube)都给出了纬度和经度为零。如何获取当前的经纬度而不使用谷歌地图android

这里是我使用

locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     Criteria criteria = new Criteria(); 
     criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
     criteria.setAltitudeRequired(false); 
     criteria.setSpeedRequired(false); 
     criteria.setBearingRequired(false); 
     criteria.setCostAllowed(false); 

     double lat = 0; 
     double lng = 0; 
     provider = locationManager.getBestProvider(criteria, false); 

     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) { 

      return; 
     } 
     Location location = locationManager.getLastKnownLocation(provider); 
     if (location != null) 
     { 
      lat = location.getLatitude(); 
      lng = location.getLongitude(); 
      Toast.makeText(this,"Location"+lat+" "+lng+" ",Toast.LENGTH_LONG).show(); 

     }else 
      Toast.makeText(this,"Location"+lat+" "+lng+" ",Toast.LENGTH_LONG).show(); 

上面的代码总是给出0.0和0.0的lat和长的代码。

我也包括在Android清单的权限:

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

任何建议吗?

+0

https://developer.android.com/guide/topics/location/strategies.html – CommonsWare

回答

0
public static void getLocation(final Context context, final Looper looper, final LocationUpdateListener locationUpdateListener, final LocationListener locationListener) { 


    try { 
     boolean isProviderEnabled; 
     Location location; 
     final LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 

     isProviderEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 
     if (isProviderEnabled) { 
      if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
       locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, locationListener, looper); 
       Handler handler = new Handler(); 
       handler.postDelayed(new Runnable() { 
        @Override 
        public void run() { 
         if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
          locationManager.removeUpdates(locationListener); 
         } 
         getBackupLocation(context, looper, locationUpdateListener, locationListener); 
        } 
       }, 10000); 

       return; 

      } 
     } 
     isProviderEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); 
     if (isProviderEnabled) { 

      locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, locationListener, looper); 
      return; 

     } 
     location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
     if (location != null) { 
      if (locationUpdateListener != null) { 
       locationUpdateListener.onLocationUpdate(location); 
       locationManager.removeUpdates(locationListener); 
      } 
      return; 
     } 

     location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if (location != null) { 
      if (locationUpdateListener != null) { 
       locationUpdateListener.onLocationUpdate(location); 
       locationManager.removeUpdates(locationListener); 
      } 
      return; 

     } 

     float defaultValue = -10000; 
     SharedManager sharedManager = SharedManager.getInstance(); 
     double lat = sharedManager.getDoubleValue(SharedManager.LAT, defaultValue); 
     double lng = sharedManager.getDoubleValue(SharedManager.LNG, defaultValue); 

     if (lat != defaultValue && lng != defaultValue) { 
      location = new Location(LocationManager.PASSIVE_PROVIDER); 
      location.setLatitude(lat); 
      location.setLongitude(lng); 
      if (locationUpdateListener != null) { 
       locationUpdateListener.onLocationUpdate(location); 
       locationManager.removeUpdates(locationListener); 
      } 
      return; 
     } 
     location = new Location(LocationManager.PASSIVE_PROVIDER); 
     location.setLatitude(PASSIVE_LAT); 
     location.setLongitude(PASSIVE_LONG); 
     if (locationUpdateListener != null) { 
      locationUpdateListener.onLocationUpdate(location); 
      locationManager.removeUpdates(locationListener); 
     } 

} 
    catch(
Exception e) 

    { 
     //No Location 
     Location location = new Location(LocationManager.PASSIVE_PROVIDER); 
     location.setLatitude(PASSIVE_LAT); 
     location.setLongitude(PASSIVE_LONG); 
     if (locationUpdateListener != null) { 
      locationUpdateListener.onLocationUpdate(location); 
     } 
    } 

    } 

工作职能,检查GPS,网络和LastKnownLocation .. 我的建议:一是利用LastKnownLocation然后元网络和不得已的GPS ..少电池使用。

定位结果返回给听众。

0

这就是我用于我的项目的全部内容。希望能帮助到你!

声明:

Location location; 
    LocationManager locationManager; 
    String provider; 

的onCreate:

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

     locationManager.requestLocationUpdates(provider, 400, 1, this); 

     location = locationManager.getLastKnownLocation(provider); 

     lat = location.getLatitude(); long = location.getLongitude(); 
+0

不能解析方法 “requestLocationUpdates” –

+0

你有没有进口这些?导入android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; –

+0

是的,我输入了所有这些 –

相关问题