使用places api键时的空白地图
Posted
技术标签:
【中文标题】使用places api键时的空白地图【英文标题】:blank map while using places api key 【发布时间】:2020-11-15 14:07:40 【问题描述】:我正在使用 google Places API(已激活支付)我得到了密钥,当将其添加到我的应用程序时,搜索效果很好,但是,地图本身不工作它显示为空白,下面我可以看到 google 徽标
我的 XML
<fragment
android:id="@+id/map_fragment"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_
android:layout_ />
还有我用于声明地点 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" />
谁能帮帮我,好吗?
【问题讨论】:
***.com/a/14761878/12478830 你救了我的命 【参考方案1】:如果还有人在受苦
找了半天,错过了添加
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/places_api_key" />
到清单文件。
【讨论】:
以上是关于使用places api键时的空白地图的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 React 和 Google Places API 在 Google 地图上显示地点标记?
如何使用 Google 地图搜索我自己的位置数据(与 Places 搜索 API 的功能相同,但用于我自己的“地点”)
适用于 Android 的 Google Places API:地图立即消失