Android Studio API 19:谷歌地方未找到 api 异常 15

Posted

技术标签:

【中文标题】Android Studio API 19:谷歌地方未找到 api 异常 15【英文标题】:Android studio API 19 : google places place not found api exception 15 【发布时间】:2020-11-11 06:40:12 【问题描述】:

我正在尝试开发一个显示附近餐馆的应用程序。这是我的代码:

MapsActivity.java: 公共类 MapsActivity 扩展 FragmentActivity 实现 OnMapReadyCallback

private GoogleMap mMap;
private String API_KEY = BuildConfig.API_KEY;
PlacesClient placesClient;
private static final int RC_LOCATION = 10;
LatLng latLng;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    // Initialize the SDK
    Places.initialize(getApplicationContext(), API_KEY);

    // Create a new PlacesClient instance
    placesClient = Places.createClient(this);

    getCurrentLocation();


/**
 * Manipulates the map once available.
 * This callback is triggered when the map is ready to be used.
 * This is where we can add markers or lines, add listeners or move the camera. In this case,
 * we just add a marker near Sydney, Australia.
 * If Google Play services is not installed on the device, the user will be prompted to install
 * it inside the SupportMapFragment. This method will only be triggered once the user has
 * installed Google Play services and returned to the app.
 */
@Override
public void onMapReady(GoogleMap googleMap) 
    mMap = googleMap;




private void getCurrentLocation() 
    List<Place.Field> placeFields = Arrays.asList(Place.Field.NAME, Place.Field.LAT_LNG,
            Place.Field.TYPES);

// 使用构建器创建 FindCurrentPlaceRequest。 FindCurrentPlaceRequest 请求 = FindCurrentPlaceRequest.newInstance(placeFields);

// 调用 findCurrentPlace 并处理响应(首先检查用户是否已授予权限)。 if (ContextCompat.checkSelfPermission(this, ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) 任务 placeResponse = placesClient.findCurrentPlace(request); placeResponse.addOnCompleteListener(任务-> if (task.isSuccessful()) FindCurrentPlaceResponse response = task.getResult();

                boolean firstRestaurantFound = false;

                for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) 
                    Log.i("success", String.format("Place '%s' has likelihood: %f",
                            placeLikelihood.getPlace().getName(),
                            placeLikelihood.getLikelihood()));

                    final Place.Type lookingFor = RESTAURANT; // Place.Type.RESTAURANT
                    // if latitude and longitude aren't null and if the place type is RESTAURANT
                    final List<Place.Type> types = placeLikelihood.getPlace().getTypes();
                    Log.d("TYPES", types.toString());
                    if (placeLikelihood.getPlace().getLatLng() != null && types.contains(lookingFor)) 

                        latLng = placeLikelihood.getPlace().getLatLng();

                        //Move camera to the first restaurant found
                        if(!firstRestaurantFound)
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15));
                            firstRestaurantFound = true;
                        

                        MarkerOptions options = new MarkerOptions()
                                .position(latLng)
                                .title(placeLikelihood.getPlace().getName());

                        mMap.addMarker(options);
                    

                
             else 
                Exception exception = task.getException();
                if (exception instanceof ApiException) 
                    ApiException apiException = (ApiException) exception;
                    Log.e("error", "Place not found: " + apiException.getStatusCode());
                
            
        );
     else 
        getLocatePermission();

    



private void getLocatePermission() 
    ActivityCompat.requestPermissions(this, new String[]ACCESS_FINE_LOCATION, RC_LOCATION);


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) 
    if (requestCode == RC_LOCATION) 
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
            getCurrentLocation();
         else 
            getLocatePermission();
        
    

但是模拟器没有找到我并告诉我 Api exception 15: connection timed out。 但是它适用于我的三星 s10e android 10。

【问题讨论】:

【参考方案1】:

它有效。我在模拟器参数中手动输入了我的位置。

【讨论】:

以上是关于Android Studio API 19:谷歌地方未找到 api 异常 15的主要内容,如果未能解决你的问题,请参考以下文章

离子框架:不在设备上加载谷歌地图库

如何在 Flutter Web 中使用谷歌地图库? [关闭]

Visual Studio:Android SDK 设置(API 级别 19 和 21、22、23)

我怎么能降级在Android Studio中的库从API 23日至19 API

当我在 19 API 中打开我的图片库时,没有看到画廊,但在 23API 中它在 android studio 中显示了画廊?

在 Android Studio 中从 JSON 向谷歌地图添加标记