Google位置服务的getLastLocation方法是否可以在Android应用中启动GPS?

时间:2016-10-30 20:29:26

标签: android gps google-location-services

我有一个应用程序,当某个活动开始时,需要用当前地址填写一个字段。我不确定我这样做的方式是否会更新gps坐标,除非启动另一个使用gps的应用程序。

如果我有位置,我可以使用Geocoder获取地址。我可以使用Google Locations API的onCreate获取该位置。

如果我按照教程步骤在GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build(); 中初始化api,请执行以下操作:

onConnect

并在Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 中获取最后一个位置:

onConnected

我觉得我只获得了最后一个已知地址,但上面的代码实际上并没有在手机上启动GPS。

我是否需要在#include <iostream> #include <string> #include <ctime> #include <cstdlib> using std::cin; using std::cout; using std::string; using std::endl; const int line_width = 80; int main() { //declare string and get user input string not_justified; cout << "Input a line of text less than 80 characters to be justfied: " << endl; getline(cin, not_justified); int position = not_justified.find(' '); //start random number generator srand(time(nullptr)); while (not_justified.size() != line_width) { //find space position position = not_justified.find(' '); //get random number from 1-80 int random_number = rand() % 80 + 1; //test to see if number is less than 40, if it is return true random_number < 40 ? true : false; //if true, insert a space if (true) not_justified.insert(position, " "); position += position; } cout << "Your justified line is: " << not_justified << endl; } //end main 中启用位置更新,然后我第一次获得更新位置或上述代码是否实际启动了GPS?

1 个答案:

答案 0 :(得分:1)

getLastLocation方法无法启动GPS,只检索最后一个位置。

此外,您可以检查位置的日期,以检测它是否太旧。

如果是旧位置或想要获取新位置,则需要提出位置请求。

考虑我的LocationRequest

private LocationRequest mLocationRequest;

private void createLocationRequest(){
    mLocationRequest = new LocationRequest();

    // 0 means here receive location as soon as possible
    mLocationRequest.setInterval(0);
    mLocationRequest.setFastestInterval(0);

    // setNumUpdates(1); stops location requests after receiving 1 location
    mLocationRequest.setNumUpdates(1);

    // PRIORITY_HIGH_ACCURACY option uses your GPS
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

@Override
public void onLocationChanged(Location location) {
    // Use new location
}