android - 使用places api键时的空白地图
问题描述
我正在使用 google Places API(已激活支付)我得到了密钥,当将其添加到我的应用程序时,搜索效果很好,但是,地图本身不起作用,它显示为空白,下面我可以看到 google 徽标
我的 XML
<fragment
android:id="@+id/map_fragment"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
和我声明地点 API 和地图的 java 代码
private void initMapsAndPlaces() {
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
Places.initialize(this, mApiKey);
placesClient = Places.createClient(this);
final AutocompleteSessionToken token = AutocompleteSessionToken.newInstance();
MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(this);
mapView = mapFragment.getView();
materialSearchBar.setOnSearchActionListener(new MaterialSearchBar.OnSearchActionListener() {
@Override
public void onSearchStateChanged(boolean enabled) {
}
@Override
public void onSearchConfirmed(CharSequence text) {
startSearch(text.toString(), true, null, true);
}
@Override
public void onButtonClicked(int buttonCode) {
if (buttonCode == MaterialSearchBar.BUTTON_BACK) {
materialSearchBar.disableSearch();
materialSearchBar.clearSuggestions();
}
}
});
materialSearchBar.addTextChangeListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
FindAutocompletePredictionsRequest predictionsRequest = FindAutocompletePredictionsRequest.builder()
.setCountry(mCountry)
.setSessionToken(token)
.setQuery(s.toString())
.build();
placesClient.findAutocompletePredictions(predictionsRequest).addOnCompleteListener(new OnCompleteListener<FindAutocompletePredictionsResponse>() {
@Override
public void onComplete(@NonNull Task<FindAutocompletePredictionsResponse> task) {
if (task.isSuccessful()) {
FindAutocompletePredictionsResponse predictionsResponse = task.getResult();
if (predictionsResponse != null) {
predictionList = predictionsResponse.getAutocompletePredictions();
List<String> suggestionsList = new ArrayList<>();
for (int i = 0; i < predictionList.size(); i++) {
AutocompletePrediction prediction = predictionList.get(i);
suggestionsList.add(prediction.getFullText(null).toString());
}
materialSearchBar.updateLastSuggestions(suggestionsList);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (!materialSearchBar.isSuggestionsVisible()) {
materialSearchBar.showSuggestionsList();
}
}
}, 1000);
}
} else {
Log.i(TAG, "prediction fetching task unSuccessful");
}
}
});
}
@Override
public void afterTextChanged(Editable s) {
}
});
materialSearchBar.setSuggestionsClickListener(new SuggestionsAdapter.OnItemViewClickListener() {
@Override
public void OnItemClickListener(int position, View v) {
if (position >= predictionList.size()) {
return;
}
AutocompletePrediction selectedPrediction = predictionList.get(position);
String suggestion = materialSearchBar.getLastSuggestions().get(position).toString();
materialSearchBar.setText(suggestion);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
materialSearchBar.clearSuggestions();
}
}, 1000);
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(materialSearchBar.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
}
String placeId = selectedPrediction.getPlaceId();
List<Place.Field> placeFields = Arrays.asList(Place.Field.LAT_LNG, Place.Field.NAME, Place.Field.ADDRESS);
FetchPlaceRequest fetchPlaceRequest = FetchPlaceRequest.builder(placeId, placeFields).build();
placesClient.fetchPlace(fetchPlaceRequest).addOnSuccessListener(new OnSuccessListener<FetchPlaceResponse>() {
@Override
public void onSuccess(FetchPlaceResponse fetchPlaceResponse) {
Place place = fetchPlaceResponse.getPlace();
Log.i(TAG, "place found " + place.getName() + place.getAddress());
LatLng latLng = place.getLatLng();
if (latLng != null) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, DEFAULT_ZOOM));
}
rippleBg.startRippleAnimation();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
rippleBg.stopRippleAnimation();
}
}, 2000);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
if (e instanceof ApiException) {
ApiException apiException = (ApiException) e;
apiException.printStackTrace();
int statusCode = apiException.getStatusCode();
Log.i(TAG, "place not found" + e.getMessage());
Log.i(TAG, "status code : " + statusCode);
}
}
});
}
@Override
public void OnItemDeleteListener(int position, View v) {
}
});
}
添加了权限
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
任何人都可以帮助我吗?
解决方案
如果还有人在受苦
经过长时间的搜索,我错过了添加
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/places_api_key" />
到清单文件。
推荐阅读
- c# - JsonIgnore 属性在抽象类中不起作用
- botframework - Bot Framework .dialog 文件和 CodeAction
- web-scraping - page.close() 不会停止 ui4j 活动和网页的定期重新加载
- javascript - JS:如何管理 POST 请求队列
- npm - 猜猜是不是digitalocean中的npm冲突和pm2状态错误
- django - 为什么它 urlpatterns 给我页面错误?
- python - MultiIndex 数据帧的列差异
- c++ - 不一致的操作数约束
- reactjs - 每当用户在填字游戏上水平或垂直滑动时,它应该做两件事,一是它应该改变颜色,二是它应该导航?
- authentication - 使用 OAuth 进行 IIS 身份验证