我在使用Google商家信息自动填充SDK时遇到问题,但我已经找到了一些无法解决问题的答案。特别是,找到了相关的问题和答案here和here。 Android开发者网站上的文档是here。
我激活了适用于Android的Places SDK(以及适用于Android的Maps SDK)并将创建的API密钥粘贴到我的strings.xml文件中。地图工作正常,但自动完成小部件在选择后立即关闭,在logcat中我收到错误:
05-23 06:00:40.376 14951-14951/com.example.tobi.beparty I/LOCATIONACTIVITY: An error occurred: Status{statusCode=ERROR, resolution=null}
由于链接的答案,我认为API密钥有问题,但是,在Google API仪表板中,我看到每个请求(错误率为100%),因此我认为这不是问题。此外,我尝试实现具有意图的Place Autocomplete版本(如文档中所述),但遇到了同样的问题,这就是为什么我认为,片段也不是问题。
有什么建议吗?非常感谢提前!
的Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.tobi.beparty">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<application
android:name=".ParseApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_maps_key"/>
<activity android:name=".LogInActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity android:name=".MainActivity" />
<activity
android:name=".LocationActivity"
android:label="@string/title_activity_location">
</activity>
<activity android:name=".SearchActivity"/>
</application>
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tobi.beparty.LocationActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/relLayoutBotBar">
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/map"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:elevation="10dp"
android:background="@drawable/white_border"
android:id="@+id/relLayout1">
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:id="@+id/ic_magnify"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:src="@drawable/ic_magnify"/>
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/ic_magnify"
android:layout_marginLeft="5dp"
android:layout_centerVertical="true"
android:textSize="15sp"
android:textColor="#000"
android:background="@null"
android:hint="Enter the party name"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="@+id/relLayoutBotBar"
android:layout_alignParentBottom="true">
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/white_grey_border_top"
app:menu="@menu/bottom_navigation_menu">
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
爪哇:
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;
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.MarkerOptions;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
public class LocationActivity extends FragmentActivity implements OnMapReadyCallback {
final private String TAG = "LOCATIONACTIVITY";
private GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;
EditText mSearchText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.location_layout);
// 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);
// mSearchText = (EditText) findViewById(R.id.inputSearch);
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i(TAG, "Place: " + place.getName());
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i(TAG, "An error occurred: " + status);
}
});
/* Bottom Navigationbar*/
BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomBar);
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);
Menu menu = bottomNavigationView.getMenu();
MenuItem menuItem = menu.getItem(3);
menuItem.setChecked(true);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.ic_search:
Intent intentSearch = new Intent(LocationActivity.this, MainActivity.class);
startActivity(intentSearch);
break;
case R.id.ic_favorite:
Intent intentFavorite = new Intent(LocationActivity.this, FavoriteActivity.class);
startActivity(intentFavorite);
break;
case R.id.ic_cake:
Intent intentParties = new Intent(LocationActivity.this, PartiesActivity.class);
startActivity(intentParties);
break;
case R.id.ic_location:
Intent intentLocation = new Intent(LocationActivity.this, LocationActivity.class);
startActivity(intentLocation);
break;
case R.id.ic_person:
Intent intentProfile = new Intent(LocationActivity.this, ProfileActivity.class);
startActivity(intentProfile);
break;
}
return false;
}
});
}
/**
* 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;
// Add a marker in Sydney and move the camera
/* LatLng center = new LatLng(-34, 151);
String centerName = "Sydney"; */
LatLng center = new LatLng(13.307, 52.500068);
String centerName = "Adenauerplatz";
mMap.addMarker(new MarkerOptions().position(center).title("Marker in " + centerName));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(center, 10));
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.i(TAG, location.toString());
Toast.makeText(LocationActivity.this, location.toString(), Toast.LENGTH_SHORT).show();
// LatLng center = new LatLng(-34, 151);
LatLng center = new LatLng(location.getLatitude(), location.getLongitude());
String centerName = "current position";
mMap.clear();
mMap.addMarker(new MarkerOptions().position(center).title("Marker in " + centerName));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(center, 10));
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
if(listAddresses != null && listAddresses.size() > 0){
Log.i(TAG, listAddresses.get(0).toString());
String address = "";
if(listAddresses.get(0).getSubThoroughfare() != null){
address += listAddresses.get(0).getSubThoroughfare() + "";
}
if(listAddresses.get(0).getThoroughfare() != null){
address += listAddresses.get(0).getThoroughfare() + "";
}
if(listAddresses.get(0).getLocale() != null){
address += listAddresses.get(0).getLocale() + ", ";
}
if(listAddresses.get(0).getPostalCode() != null){
address += listAddresses.get(0).getPostalCode() + ", ";
}
if(listAddresses.get(0).getCountryName() != null){
address += listAddresses.get(0).getCountryName() + "";
}
Toast.makeText(LocationActivity.this, address, Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
if (Build.VERSION.SDK_INT < 23) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
} else {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
String lastKnownLocationName;
LatLng lastKnownLatLng;
if(lastKnownLocation == null){
lastKnownLatLng = new LatLng(52.500068, 13.307);
lastKnownLocationName = "Adenauerplatz";
}else{
lastKnownLatLng = new LatLng(lastKnownLocation.getLatitude(), lastKnownLocation.getLongitude());
lastKnownLocationName = "Last known location";
}
mMap.addMarker(new MarkerOptions().position(lastKnownLatLng).title(lastKnownLocationName));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(lastKnownLatLng, 10));
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if(ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); // ToDo: Die zahlen hier stehen dafür, wieviele Meter bzw. Sekunden gegangen werden müssen, dass ein Update erfolgt. evtl. ändern
}
}
}
}
答案 0 :(得分:0)
遵循migration guide使用“地方信息”兼容性库时,我遇到了同样的问题。
问题是Google认为原始的“ Android版Places SDK”和新的“ Places API”是两个不同的API。因此,您需要进入Google Cloud Platform并启用新的“ Places API”。
如果尚未启用它,它将显示在“其他API”部分下-单击它,然后单击“启用”。
启用后,新的Places API应显示在“已启用的API”部分下,如下所示:
完成此操作后,自动完成窗口将保持打开状态并正常工作。