2

我正在尝试为我的 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 还是很陌生,所以非常感谢所有帮助!

4

1 回答 1

0

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

compile 'com.google.android.gms:play-services-places:9.2.0'
于 2016-10-10T07:24:51.697 回答