Android Google Places API Places.PlaceDetectionApi.getCurrentPlace在Emulator中始终返回0个结果

Android Google Places API Places.PlaceDetectionApi.getCurrentPlace在Emulator中始终返回0个结果,android,android-emulator,google-places-api,android-location,Android,Android Emulator,Google Places Api,Android Location,我有一个关于Android中PlaceDetection API的问题。我想让所有的地方都靠近我。在我的真实设备上,这段代码可以正常工作,但在模拟器中却不行,尽管我一步一步地遵循谷歌指南。 指南链接: 代码: public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener{ private static final String LOG_TAG =

我有一个关于Android中PlaceDetection API的问题。我想让所有的地方都靠近我。在我的真实设备上,这段代码可以正常工作,但在模拟器中却不行,尽管我一步一步地遵循谷歌指南。 指南链接:

代码:

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();
        }
    });
}
公共类MainActivity扩展AppCompativeActivity实现GoogleAppClient.OnConnectionFailedListener{
私有静态最终字符串日志\u TAG=“MainActivity”;
私有静态final int GOOGLE_API_CLIENT_ID=0;
私人GoogleapClient MGoogleapClient;
私有静态最终int权限\请求\代码=100;
列表视图位置;
@凌驾
创建时受保护的void(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
按钮currentButton=(按钮)findViewById(R.id.currentButton);
lstPlaces=(ListView)findViewById(R.id.listPlaces);
mgoogleapclient=新的Googleapclient.Builder(MainActivity.this)
.addApi(Places.PLACE\u DETECTION\u API)
.enableAutoManage(这个,谷歌API客户ID,这个)
.build();
currentButton.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
if(mgoogleapClient.isConnected()){
int hasPermission=checkSelfPermission(Manifest.permission.ACCESS\u FINE\u LOCATION);
if(hasPermission!=已授予PackageManager.PERMISSION){
ActivityCompat.requestPermissions(MainActivity.this,新字符串[]{Manifest.permission.ACCESS\u FINE\u LOCATION},permission\u REQUEST\u CODE);
}否则{
callPlaceDetectionApi();
}
}
}
});
}
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
开关(请求代码){
案例权限\请求\代码:
如果((grantResults.length>0)和&(grantResults[0]==PackageManager.PERMISSION_已授予)){
callPlaceDetectionApi();
}
打破
}
}
@凌驾
public void onconnection失败(@NonNull ConnectionResult ConnectionResult){
Log.e(Log_标记,“Google Places API连接失败,错误代码:”+connectionResult.getErrorCode());
makeText(这是“Google Places API连接失败”,错误代码为:“+connectionResult.getErrorCode(),Toast.LENGTH_LONG.show()”;
}
私有void callPlaceDetectionApi()引发SecurityException{
PendingResult结果=Places.PlaceDetectionApi.getCurrentPlace(mgoogleAppClient,null);
result.setResultCallback(新的ResultCallback(){
@凌驾
public void onResult(PlaceLikelihoodBuffer likelyPlaces){
Toast.makeText(MainActivity.this,“hioi”,Toast.LENGTH_SHORT.show();
ArrayAdapter myAdapter=新的ArrayAdapter(getApplicationContext(),android.R.layout.simple\u list\u item\u 1);
for(place可能性place可能性:likelyPlaces){
Toast.makeText(MainActivity.this,“Hoiha”,Toast.LENGTH_SHORT.show();
if(placeEllibility.getEllibility()>0){
myAdapter.add(place.getPlace().getName().toString());
}
Log.i(Log_标记,String.format(“带“+”可能性:%g的位置“%s”、placelikeligence.getPlace().getName()、placelikeligence.getlikeligence());
}
lstPlaces.setAdapter(myAdapter);
likelyPlaces.release();
}
});
}
}

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


在emulator窗口中,在此栏底部,您将看到插入符号“…”控件。选择它。这将打开“扩展控件”对话框,上面的项目将是“位置”。
在右侧,输入您想要的位置(例如:使用纬度49.2772和经度-123.108),然后尝试重新启动您的应用程序,它将正常工作,可能需要几秒钟时间。

我知道您说过您遵循了该指南,但要确认(因为您的屏幕截图没有显示此内容):是否在emulator的“扩展控件”对话框中输入纬度和经度?当您按下位置按钮时,模拟器中的谷歌地图应用程序是否显示该位置?