0

AndroidのPlaceDetection APIに関する質問があります。私は私の近くのすべての場所を取得したい。私の実際のデバイスでは、このコードはうまく動作しますが、エミュレータではありません。ガイドへ リンク:https://codelabs.developers.google.com/codelabs/location-places-android/index.html?index=..%2F..%2Findex#0Google Places API Places.PlaceDetectionApi.getCurrentPlaceは常にエミュレータで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(); 
     } 
    }); 
} 

}

鉱山ではない、私は場所やメーカーエミュレータといくつかの問題がある知っているが、なぜGoogleのコードが実行されていると?あなたのエミュレータWindows上

Screenshot on working device

Screenshot on Emulator

+0

私はあなたがそのガイドに従っていると言っていますが、あなたのスクリーンショットにこれが表示されなかったので確認することができます:エミュレータの拡張コントロールダイアログに緯度と経度を入力しましたか?エミュレータのGoogleマップアプリは、場所ボタンを押したときにその場所を表示しますか? – spiv

答えて

0

、このバーの下部に、あなたはキャレット「...」コントロールが表示されます。選択してください。これにより、拡張コントロールダイアログが開き、その上の一番上の項目が場所になります。 これの右側に、あなたのアプリを再起動しようとすると、あなたが望む場所を入力します(exp:Latitude 49.2772とLongitude -123.108を使用してください)。

関連する問題