【问题标题】:IntentBuilder not recognized in Android Studio (Google PlacePicker)IntentBuilder 在 Android Studio 中无法识别(Google PlacePicker)
【发布时间】:2016-10-10 07:16:40
【问题描述】:

我正在尝试为我的 Google 地图应用程序创建一个 PlacePicker,并找到了一个很好的指南来执行此操作。该指南并没有过时,所以我没想到它会使用任何已弃用的功能。事实证明,确实如此,我对如何处理这个问题有点困惑,我仍然是 Android 开发的新手。

我以前从未使用过 IntentBuilder,所以我不确定它是否正确使用。似乎 Android 根本不知道它是什么,因为建议的解决方案是让它成为一个类。

以下是相关代码:

import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.Toast;

import com.firebase.client.ChildEventListener;
import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ServerValue;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.PlaceBuffer;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
import com.google.android.gms.maps.model.MarkerOptions;

import java.util.HashMap;
import java.util.Map;

public class PlacePicker extends FragmentActivity implements OnMapReadyCallback, ChildEventListener {

private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private LatLngBounds.Builder mBounds = new LatLngBounds.Builder();
private static final int REQUEST_PLACE_PICKER = 1;
public static final int REQUEST_ID_ACCESS_COURSE_FINE_LOCATION = 100;

private static final String FIREBASE_URL = "MYURL";
private static final String FIREBASE_ROOT_NODE = "checkouts";

private Firebase mFirebase;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_place_picker);
    // 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);

    // Set up the API client for Places API
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(Places.GEO_DATA_API)
            .build();
    mGoogleApiClient.connect();

    final Button button = (Button) findViewById(R.id.checkout_button);
    button.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    mMap.setPadding(0, button.getHeight(), 0, 0);
                }
            }
    );

    Firebase.setAndroidContext(this);
    mFirebase = new Firebase(FIREBASE_URL);
    mFirebase.child(FIREBASE_ROOT_NODE).addChildEventListener(this);


}

public void checkOut(View view) {
    try {
        PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder();
        Intent intent = intentBuilder.build(this);
        startActivityForResult(intent, REQUEST_PLACE_PICKER);
    } catch (GooglePlayServicesRepairableException e) {
        GoogleApiAvailability.getInstance().getErrorDialog(this, e.getConnectionStatusCode(),
                REQUEST_PLACE_PICKER);
    } catch (GooglePlayServicesNotAvailableException e) {
        Toast.makeText(this, "Please install Google Play Services!", Toast.LENGTH_LONG).show();
    }
}

private void addPointToViewPort(LatLng newPoint) {
    mBounds.include(newPoint);
    mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(mBounds.build(),
            findViewById(R.id.checkout_button).getHeight()));
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_PLACE_PICKER) {
        if (resultCode == Activity.RESULT_OK) {
            Place place = PlacePicker.getPlace(data, this);

            Map<String, Object> checkoutData = new HashMap<>();
            checkoutData.put("time", ServerValue.TIMESTAMP);

            mFirebase.child(FIREBASE_ROOT_NODE).child(place.getId()).setValue(checkoutData);

        } else if (resultCode == PlacePicker.RESULT_ERROR) {
            Toast.makeText(this, "Places API failure! Check the API is enabled for your key",
                    Toast.LENGTH_LONG).show();
        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}

@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
    String placeId = dataSnapshot.getKey();
    if (placeId != null) {
        Places.GeoDataApi
                .getPlaceById(mGoogleApiClient, placeId)
                .setResultCallback(new ResultCallback<PlaceBuffer>() {
                                       @Override
                                       public void onResult(PlaceBuffer places) {
                                           LatLng location = places.get(0).getLatLng();
                                           addPointToViewPort(location);
                                           mMap.addMarker(new MarkerOptions().position(location));
                                           places.release();
                                       }
                                   }
                );
    }
}
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;


    if (Build.VERSION.SDK_INT >= 23) {
        int accessCoarsePermission
                = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION);
        int accessFinePermission
                = ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION);


        if (accessCoarsePermission != PackageManager.PERMISSION_GRANTED
                || accessFinePermission != PackageManager.PERMISSION_GRANTED) {
            // The Permissions to ask user.
            String[] permissions = new String[]{android.Manifest.permission.ACCESS_COARSE_LOCATION,
                    android.Manifest.permission.ACCESS_FINE_LOCATION};
            // Show a dialog asking the user to allow the above permissions.
            ActivityCompat.requestPermissions(this, permissions,
                    REQUEST_ID_ACCESS_COURSE_FINE_LOCATION);

            return;
        }
    }
    mMap.setMyLocationEnabled(true);
    mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
            addPointToViewPort(ll);
            // we only want to grab the location once, to allow the user to pan and zoom freely.
            mMap.setOnMyLocationChangeListener(null);
        }
    });
    // Add a marker in Sydney and move the camera

}

getPlace 和 RESULT_ERROR 也是红色的,Android Studio 在此处无法识别。

我不确定我是否可以发布相关指南的链接,所以我只会在被问到时发布。

我对 Android Studio 还是很陌生,因此非常感谢所有帮助!

【问题讨论】:

  • 你添加了导入吗? import com.google.android.gms.location.places.ui.PlacePicker;
  • @antonio 也没有识别特定的导入,但我已经添加了所有正常的地方导入。我已经用导入编辑了我的答案。
  • 根据您使用的 Google Play 服务版本,您可能需要在您的应用程序中对new permissions model 进行更改。如Place Picker permissions 所述,如果您使用的是 8.1 或更高版本的 Google Play 服务,您可以将您的应用配置为面向 Android 6.0 Marshmallow SDK 并使用新的权限模型。
  • 回到旧的权限模型有效,谢谢!

标签: android google-maps android-intent gmsplacepicker


【解决方案1】:

请将此添加到您的 app.gradle 文件中的依赖项中

compile 'com.google.android.gms:play-services-places:9.2.0'

【讨论】:

  • 我已经有 9.4.0 的依赖项,我应该使用 9.2.0 吗? IntentBuilder 是否已弃用?
  • 您需要从 9.2.0 开始添加位置库,如果您的代码中有它,请更改它 import com.google.android.gms.location.places;导入 com.google.android.gms.location.places.Place;
  • 试图从 9.4.0 地方库转到 9.2.0 地方库,但我仍然遇到同样的错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-06
相关资源
最近更新 更多