2014-09-02 135 views
0

我正在尝试获取用户位置,无论是网络位置还是gps位置或两者。为此,我正在运行一个实现LocationListener的后台服务。我还在清单文件中添加了所有需要的权限。这里是我的服务无法获取用户位置

public class GetLocation extends Service implements LocationListener { 

    LocationManager lm; 
    Location networkLocation, gpsLocation; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     lm = (LocationManager) getSystemService(LOCATION_SERVICE); 
     if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
      gpsLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
      Toast.makeText(getApplicationContext(), "Getting Location Via GPS", 
        Toast.LENGTH_LONG).show(); 
     } 
     if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 
      lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, 
        this); 
      networkLocation = lm 
        .getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 
      Toast.makeText(getApplicationContext(), 
        "Getting location Via Network", Toast.LENGTH_LONG).show(); 
     } 
     if (gpsLocation != null) { 
      Toast.makeText(
        getApplicationContext(), 
        "GPSLOCATION: " + gpsLocation.getLatitude() + " , " 
          + gpsLocation.getLongitude(), Toast.LENGTH_LONG) 
        .show(); 
     } 
     if (networkLocation != null) { 
      Toast.makeText(
        getApplicationContext(), 
        "NETWORKLOCATION: " + networkLocation.getLatitude() + " , " 
          + networkLocation.getLongitude(), Toast.LENGTH_LONG) 
        .show(); 
     } 
     if (gpsLocation == null && networkLocation == null) { 
      Toast.makeText(getApplicationContext(), 
        "Couldn't get user location", Toast.LENGTH_LONG).show(); 
     } 

    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     new Thread() { 
      public void run() { 
       Looper.prepare(); 

       if (gpsLocation != null) { 
        Log.d("Usman", "GPSLOCATION: " + gpsLocation.getLatitude() 
          + " , " + gpsLocation.getLongitude()); 
       } 
       if (networkLocation != null) { 
        Log.d("Usman", 
          "NETWORKLOCATION: " + networkLocation.getLatitude() 
            + " , " + networkLocation.getLongitude()); 
       } 
       if (gpsLocation == null && networkLocation == null) { 
        Log.d("Usman", "Couldn't get user lcoation"); 
       } 
       try { 
        Thread.sleep(3000); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
       Looper.loop(); 
      } 
     }.start(); 
     return START_STICKY; 
    } 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     // do something with this location 

     Toast.makeText(
       getApplicationContext(), 
       location.getProvider() + " Location Changed:" 
         + location.getLatitude() + " , " 
         + location.getLongitude(), Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onProviderDisabled(String arg0) { 
     Toast.makeText(getApplicationContext(), arg0 + " Disabled", 
       Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onProviderEnabled(String provider) { 
     Toast.makeText(getApplicationContext(), provider + " Enabled", 
       Toast.LENGTH_LONG).show(); 
    } 

    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
    } 

} 

在onCreate()方法中,gps和网络位置都为空。 任何机构可以告诉我,我错了

+0

如果您在建筑物内进行测试,GPS将不起作用。请出去一次,然后测试。 – 2014-09-02 06:09:11

+0

rü检查真实设备或Emulater – 2014-09-02 06:10:41

+0

@Dhruti是我坐在建筑物的地下室内。但网络位置呢?是不是应该不是null? – 2014-09-02 06:14:54

回答

1

正如你所说,你甚至没有得到“通过网络获取位置”的祝酒。所以你的执行是不会的

if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { 

} 

if块内,从而确保您已检查

谷歌的定位服务 - 在您的设备设置中选中

设置>位置服务> Google的位置服务和访问位置。

然后你应该从LocationManager获得位置。其他代码看起来不错。

希望这会有所帮助。

Reference link

0
// The minimum distance to change Updates in meters 
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 

// The minimum time between updates in milliseconds 
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute 

locationManager.requestLocationUpdates(
          LocationManager.GPS_PROVIDER, 
          MIN_TIME_BW_UPDATES, 
          MIN_DISTANCE_CHANGE_FOR_UPDATES, this); 

lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10, 1000*60*1, this); 
0

你需要牢记有关权限:

1.如果您想同时使用NETWORK_PROVIDER和GPS_PROVIDER usepermission如以下所说的。

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

2.如果希望只使用NETWORK_PROVIDER 使用

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

3.A非常常见的错误,可以通过正常的人来完成。唐`吨忘记给上网权限

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

4.对于位置首次定位可够长,//locationManager.getLastKnownLocation(locationProvider)为您提供了缓存的位置 它可以为空也。

您可以根据一定的标准要求的位置:

// The minimum distance to change Updates in meters 
    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters 
    // The minimum time between updates in milliseconds 
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 5; // 5 minute 


Criteria criteria = new Criteria(); 
      criteria.setAltitudeRequired(false); 
      criteria.setBearingRequired(false); 
      criteria.setCostAllowed(false); 
      criteria.setPowerRequirement(Criteria.POWER_LOW); 
      criteria.setAccuracy(Criteria.ACCURACY_FINE); 
      String providerFine = locationManager.getBestProvider(criteria, 
        true); 

      criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
      String providerCoarse = locationManager.getBestProvider(criteria, 
        true); 

      if (providerCoarse != null) { 

       locationManager.requestLocationUpdates(providerCoarse, 
         MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, 
         this); 
      } 
      if (providerFine != null) { 

       locationManager.requestLocationUpdates(providerFine, 
         MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, 
         this); 
      } 

让你可以检查最佳位置位置固定 下面的方法后,这里给出了:http://developer.android.com/guide/topics/location/strategies.html

private static final int TWO_MINUTES = 1000 * 60 * 2; 

/** Determines whether one Location reading is better than the current Location fix 
    * @param location The new Location that you want to evaluate 
    * @param currentBestLocation The current Location fix, to which you want to compare the new one 
    */ 
protected boolean isBetterLocation(Location location, Location currentBestLocation) { 
    if (currentBestLocation == null) { 
     // A new location is always better than no location 
     return true; 
    } 

    // Check whether the new location fix is newer or older 
    long timeDelta = location.getTime() - currentBestLocation.getTime(); 
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; 
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; 
    boolean isNewer = timeDelta > 0; 

    // If it's been more than two minutes since the current location, use the new location 
    // because the user has likely moved 
    if (isSignificantlyNewer) { 
     return true; 
    // If the new location is more than two minutes older, it must be worse 
    } else if (isSignificantlyOlder) { 
     return false; 
    } 

    // Check whether the new location fix is more or less accurate 
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); 
    boolean isLessAccurate = accuracyDelta > 0; 
    boolean isMoreAccurate = accuracyDelta < 0; 
    boolean isSignificantlyLessAccurate = accuracyDelta > 200; 

    // Check if the old and new location are from the same provider 
    boolean isFromSameProvider = isSameProvider(location.getProvider(), 
      currentBestLocation.getProvider()); 

    // Determine location quality using a combination of timeliness and accuracy 
    if (isMoreAccurate) { 
     return true; 
    } else if (isNewer && !isLessAccurate) { 
     return true; 
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { 
     return true; 
    } 
    return false; 
} 

/** Checks whether two providers are the same */ 
private boolean isSameProvider(String provider1, String provider2) { 
    if (provider1 == null) { 
     return provider2 == null; 
    } 
    return provider1.equals(provider2); 
}