2013-06-23 48 views
1

我正在使用google play服务api在我的应用中获取位置更新。但是,由于某种原因,客户端连接时client.getLastLocation()不会更改其值。只有在通过注册侦听器请求位置更新后,位置才会改变。我认为这可能只是一个模拟器问题,但即使使用ddms或telnet手动更改位置,getLastLocation()的值仍然不会更改。LocationClient不更新模拟器上的getLastLocation()

不幸的是,我的手机没有显示在adb中,所以我无法在实际设备上进行测试,但是有没有人对此有何看法?这不是什么大问题,因为我可以通过请求位置更新来解决问题,但我想提供一个设置,以允许用户手动更新其位置,而不是每隔几秒就上传一次。一些代码来显示我在说什么。

@Override 
public void onClick(View v) { 
    Location newLocation = client.getLastLocation(); 
    if(newLocation == null) 
    { 
     Toast.makeText(getActivity(), "Finding location", Toast.LENGTH_SHORT).show(); 
     return; 
    } 
    if(distanceSignificant(currentLocation, newLocation)) 
    { 
     currentLocation = newLocation; 
     Log.d(TAG, "distance significant, updating location"); 
      uploadNewLocation(); 
     } 
    } 
}); 

每当我点击那个按钮,该位置将与上次的位置相同。

@Override 
public void onConnected(Bundle data) { 
    Log.d(TAG, "connected to locations"); 
    Toast.makeText(getActivity(), "Starting location services", Toast.LENGTH_SHORT).show(); 
    currentLocation = client.getLastLocation(); 

    if(currentLocation == null) 
    { 
     Log.d(TAG, "can't add user location yet, not available"); 
    } 

    if(shouldUpdate) 
    { 
     client.requestLocationUpdates(locationRequest, this); 
     updateButton.setVisibility(View.GONE); 
    } 
    else 
    { 
     updateButton.setVisibility(View.VISIBLE); 
    } 

} 

在这种情况下,shouldUpdate在onCreateView中设置并由用户设置进行设置。

有谁知道我应该怎么做才能解决这个问题?我觉得我错过了简单的事情。

回答

3

发生这种情况是因为getLastLocation是获取最后一个已知位置的非阻塞亲电池调用(这可能是旧值),并且当没有任何活动检索位置更新时,它不会被神奇地更改。

您可以做的最好的事情是获取位置的用户请求,开始请求位置更新并在获取第一个位置时停止位置更新。