2016-10-10 6 views
1

Ich versuche, einen PlacePicker für meine Google Maps-Anwendung zu erstellen, und fand eine nette Anleitung dazu. Das Handbuch ist nicht schrecklich veraltet, also habe ich nicht erwartet, dass es veraltete Features verwendet. Wie sich herausstellt, tut es das, und ich bin irgendwie verwirrt, wie ich mit diesem Problem umgehen soll. Ich bin immer noch neu in Android Development.IntentBuilder nicht erkannt in Android Studio (Google PlacePicker)

Ich habe noch nie zuvor einen IntentBuilder verwendet, also bin ich mir nicht sicher, ob er überhaupt richtig benutzt wird. Es scheint, als ob Android einfach nicht weiß, was es überhaupt ist, da die vorgeschlagene Lösung es zu einer Klasse macht.

Hier ist der entsprechende Code:

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 

} 

Auch getPlace und RESULT_ERROR ist rot als gut, und unerkannt von Android Studio hier.

Ich bin mir nicht sicher, ob ich den Link zu dem fraglichen Führer posten darf, also poste ich nur, wenn gefragt.

Ich bin noch ziemlich neu in Android Studio, so dass alle Hilfe sehr geschätzt wird!

+0

haben Sie hinzugefügt, um den Import? 'import com.google.android.gms.location.places.ui.PlacePicker;' – antonio

+0

@antonio dass dieser bestimmte Import auch nicht erkannt wird, aber ich habe alle normalen Orte importiert. Ich habe meine Antwort mit Importen bearbeitet. –

+0

Je nachdem, welche Google Play-Dienstversion Sie verwenden, müssen Sie möglicherweise die Änderungen in Ihrer App für das [neue Berechtigungsmodell] (https://developer.android.com/training/permissions/index.html) vornehmen. . Wie unter [Berechtigungen für Platzwähler] (https://developers.google.com/places/android-api/placepicker#permissions) angegeben, können Sie Ihre Version konfigurieren, sofern Sie die Google Play-Dienste der Version 8.1 oder höher verwenden App, um das Android 6.0 Marshmallow SDK zu targetieren und das neue Berechtigungsmodell zu verwenden. – Teyam

Antwort

0

fügen Sie diese an Ihre Abhängigkeiten in Ihrer app.gradle Datei

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

Ich habe bereits 9.4.0 in Abhängigkeiten, sollte ich 9.2.0 verwenden? War IntentBuilder veraltet? –

+0

Sie müssen die Orte Bibliothek seit 9.2.0 hinzufügen und ändern, wenn Sie es in Ihrem Code haben import com.google.android.gms.location.places; bis importieren com.google.android.gms.location.places.Place; – zombie

+0

versucht, von 9.4.0 Stellen Bibliothek zu 9.2.0 Stellen Bibliothek zu gehen, und ich bekomme immer noch den gleichen Fehler. –

Verwandte Themen