Google Places API Places.PlaceDetectionApi.getCurrentPlace始终在模拟器中返回0结果

时间:2017-01-03 21:26:11

标签: android android-emulator google-places-api android-location

我对Android中的PlaceDetection API有疑问。我想要靠近我的所有地方。在我的真实设备上这个代码工作正常,但不是在模拟器中,即使我一步一步地按照谷歌指南。 链接指南:https://codelabs.developers.google.com/codelabs/location-places-android/index.html?index=..%2F..%2Findex#0

代码:

public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener{

private static final String LOG_TAG = "MainActivity";
private static final int GOOGLE_API_CLIENT_ID = 0;
private GoogleApiClient mGoogleApiClient;
private static final int PERMISSION_REQUEST_CODE = 100;
ListView lstPlaces;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button currentButton = (Button) findViewById(R.id.currentButton);
    lstPlaces = (ListView) findViewById(R.id.listPlaces);
    mGoogleApiClient = new GoogleApiClient.Builder(MainActivity.this)
            .addApi(Places.PLACE_DETECTION_API)
            .enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
            .build();
    currentButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mGoogleApiClient.isConnected()) {
                int hasPermission = checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION);
                if (hasPermission != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
                } else {
                    callPlaceDetectionApi();
                }
            }
        }
    });
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch(requestCode){
        case PERMISSION_REQUEST_CODE:
            if((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)){
               callPlaceDetectionApi();
            }
            break;
    }
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.e(LOG_TAG, "Google Places API connection failed with error code: " + connectionResult.getErrorCode());
    Toast.makeText(this, "Google Places API connection failed with error code:" + connectionResult.getErrorCode(), Toast.LENGTH_LONG).show();

}

private void callPlaceDetectionApi() throws SecurityException {
    PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi.getCurrentPlace(mGoogleApiClient, null);
    result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
        @Override
        public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
            Toast.makeText(MainActivity.this, "hioi", Toast.LENGTH_SHORT).show();
            ArrayAdapter myAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1);
            for (PlaceLikelihood placeLikelihood : likelyPlaces) {
                Toast.makeText(MainActivity.this, "Hoiha", Toast.LENGTH_SHORT).show();
                if(placeLikelihood.getLikelihood()>0){
                    myAdapter.add(placeLikelihood.getPlace().getName().toString());
                }
                Log.i(LOG_TAG, String.format("Place '%s' with " +  "likelihood: %g", placeLikelihood.getPlace().getName(), placeLikelihood.getLikelihood()));
            }
            lstPlaces.setAdapter(myAdapter);
            likelyPlaces.release();
        }
    });
}

}

我知道位置和Studio Emulator存在一些问题,但为什么谷歌代码会运行而不是我的?

Screenshot on working device

Screenshot on Emulator

1 个答案:

答案 0 :(得分:0)

在您的模拟器窗口上,在此栏的底部,您会看到一个插入符号'...&#39;控制。选择它。这将打开扩展控件对话框,其上的顶部项目将是位置。 在其右侧,输入您想要的位置(对于exp:使用Latitude 49.2772和Longitude -123.108),然后尝试重新启动您的应用程序,它将起作用,可能需要几秒钟。