HEREMap搜索引擎在Android探索SDK总是给出错误“禁止”,即使Map绘制工作正常.
使用Android Explore SDK heresdk-explore-android-4.13.4.0.11034.aar,当在Android Studio模拟器和Galaxy s22上运行示例Search时,它正确地显示了柏林的Map,但www.example.com()的SearchCallback .onSearchCompleted(searchEngine.search)总是给出SearchError“FORBIDDEN”
searchEngine.suggest()的onSuggestCompleted()也有同样的问题--它总是给SearchError“FORBIDDEN”
在Android Studio控制台中,执行搜索时出现以下错误:E/SearchErrorConvert:[ERROR] SearchErrorConvert -搜索错误:web::http::http_exception,what:{“error”:“Forbidden”,“error_description”:“These credentials do not authorize access”},value:403
尽管有这个错误,但由于我可以看到柏林的MapOK,我猜我的accessKeyID和accessKeySecret一定是正确的。
我有HERE基本计划,包括使用信用卡计费,并在2022年10月获得了我的访问密钥ID和Secret,当时我使用OAuth创建了凭据,并且从那时起就能够创建和查看Map,标记和折线。
下面是我的代码,从Android Explore SDK Search示例复制(我的真实的accessKeyID和accessKeySecret仅在本文中隐藏)。错误是从searchEngine.search(query,searchOptions,querySearchCallback)从searchInViewport()产生的,错误是从searchEngine产生的。suggest()从autoSuggestExample()产生的
private void initializeHERESDK() {
// Set your credentials for the HERE SDK.
String accessKeyID = "<id>";
String accessKeySecret = "<secret>";
SDKOptions options = new SDKOptions(accessKeyID, accessKeySecret);
try {
Context context = this;
SDKNativeEngine.makeSharedInstance(context, options);
} catch (InstantiationErrorException e) {
throw new RuntimeException("Initialization of HERE SDK failed: " + e.error.name());
}
}
public class Uncategorized {
private static final String LOG_TAG = SearchExample.class.getName();
private final Context context;
private final MapView mapView;
private final MapCamera camera;
private final List<MapMarker> mapMarkerList = new ArrayList<>();
private SearchEngine searchEngine;
public SearchExample(Context context, MapView mapView) {
this.context = context;
this.mapView = mapView;
camera = mapView.getCamera();
double distanceInMeters = 1000 * 10;
MapMeasure mapMeasureZoom = new MapMeasure(MapMeasure.Kind.DISTANCE, distanceInMeters);
camera.lookAt(new GeoCoordinates(52.520798, 13.409408), mapMeasureZoom);
try {
searchEngine = new SearchEngine();
} catch (InstantiationErrorException e) {
throw new RuntimeException("Initialization of SearchEngine failed: " + e.error.name());
}
setTapGestureHandler();
setLongPressGestureHandler();
Toast.makeText(context,"Long press on map to get the address for that position using reverse geocoding.", Toast.LENGTH_LONG).show();
}
public void onSearchButtonClicked() {
// Search for "Pizza" and show the results on the map.
searchExample();
// Search for auto suggestions and log the results to the console.
autoSuggestExample();
}
public void onGeocodeButtonClicked() {
// Search for the location that belongs to an address and show it on the map.
geocodeAnAddress();
}
private void searchExample() {
String searchTerm = "Pizza";
Toast.makeText(context,"Searching in viewport: " + searchTerm, Toast.LENGTH_LONG).show();
searchInViewport(searchTerm);
}
private void geocodeAnAddress() {
// Set map to expected location.
GeoCoordinates geoCoordinates = new GeoCoordinates(52.53086, 13.38469);
double distanceInMeters = 1000 * 7;
MapMeasure mapMeasureZoom = new MapMeasure(MapMeasure.Kind.DISTANCE, distanceInMeters);
camera.lookAt(geoCoordinates, mapMeasureZoom);
String queryString = "Invalidenstraße 116, Berlin";
Toast.makeText(context,"Finding locations for: " + queryString
+ ". Tap marker to see the coordinates. Check the logs for the address.", Toast.LENGTH_LONG).show();
geocodeAddressAtLocation(queryString, geoCoordinates);
}
private void setTapGestureHandler() {
mapView.getGestures().setTapListener(touchPoint -> pickMapMarker(touchPoint));
}
private void setLongPressGestureHandler() {
mapView.getGestures().setLongPressListener((gestureState, touchPoint) -> {
if (gestureState == GestureState.BEGIN) {
GeoCoordinates geoCoordinates = mapView.viewToGeoCoordinates(touchPoint);
if (geoCoordinates == null) {
return;
}
addPoiMapMarker(geoCoordinates);
getAddressForCoordinates(geoCoordinates);
}
});
}
private void getAddressForCoordinates(GeoCoordinates geoCoordinates) {
SearchOptions reverseGeocodingOptions = new SearchOptions();
reverseGeocodingOptions.languageCode = LanguageCode.EN_GB;
reverseGeocodingOptions.maxItems = 1;
searchEngine.search(geoCoordinates, reverseGeocodingOptions, addressSearchCallback);
}
private final SearchCallback addressSearchCallback = new SearchCallback() {
@Override
public void onSearchCompleted(@Nullable SearchError searchError, @Nullable List<Place> list) {
if (searchError != null) {
showDialog("Reverse geocoding", "Error: " + searchError.toString());
return;
}
// If error is null, list is guaranteed to be not empty.
showDialog("Reverse geocoded address:", list.get(0).getAddress().addressText);
}
};
private void pickMapMarker(final Point2D point2D) {
float radiusInPixel = 2;
mapView.pickMapItems(point2D, radiusInPixel, new MapViewBase.PickMapItemsCallback() {
@Override
public void onPickMapItems(@Nullable PickMapItemsResult pickMapItemsResult) {
if (pickMapItemsResult == null) {
return;
}
List<MapMarker> mapMarkerList = pickMapItemsResult.getMarkers();
if (mapMarkerList.size() == 0) {
return;
}
MapMarker topmostMapMarker = mapMarkerList.get(0);
Metadata metadata = topmostMapMarker.getMetadata();
if (metadata != null) {
CustomMetadataValue customMetadataValue = metadata.getCustomValue("key_search_result");
if (customMetadataValue != null) {
SearchResultMetadata searchResultMetadata = (SearchResultMetadata) customMetadataValue;
String title = searchResultMetadata.searchResult.getTitle();
String vicinity = searchResultMetadata.searchResult.getAddress().addressText;
showDialog("Picked Search Result",title + ". Vicinity: " + vicinity);
return;
}
}
showDialog("Picked Map Marker",
"Geographic coordinates: " +
topmostMapMarker.getCoordinates().latitude + ", " +
topmostMapMarker.getCoordinates().longitude);
}
});
}
private void searchInViewport(String queryString) {
clearMap();
GeoBox viewportGeoBox = getMapViewGeoBox();
TextQuery.Area queryArea = new TextQuery.Area(viewportGeoBox);
TextQuery query = new TextQuery(queryString, queryArea);
SearchOptions searchOptions = new SearchOptions();
searchOptions.languageCode = LanguageCode.EN_US;
searchOptions.maxItems = 30;
searchEngine.search(query, searchOptions, querySearchCallback);
}
private final SearchCallback querySearchCallback = new SearchCallback() {
@Override
public void onSearchCompleted(@Nullable SearchError searchError, @Nullable List<Place> list) {
if (searchError != null) {
showDialog("Search", "Error: " + searchError.toString());
return;
}
// If error is null, list is guaranteed to be not empty.
showDialog("Search", "Results: " + list.size());
// Add new marker for each search result on map.
for (Place searchResult : list) {
Metadata metadata = new Metadata();
metadata.setCustomValue("key_search_result", new SearchResultMetadata(searchResult));
// Note: getGeoCoordinates() may return null only for Suggestions.
addPoiMapMarker(searchResult.getGeoCoordinates(), metadata);
}
}
};
private static class SearchResultMetadata implements CustomMetadataValue {
public final Place searchResult;
public SearchResultMetadata(Place searchResult) {
this.searchResult = searchResult;
}
@NonNull
@Override
public String getTag() {
return "SearchResult Metadata";
}
}
private final SuggestCallback autosuggestCallback = new SuggestCallback() {
@Override
public void onSuggestCompleted(@Nullable SearchError searchError, @Nullable List<Suggestion> list) {
if (searchError != null) {
Log.d(LOG_TAG, "Autosuggest Error: " + searchError.name());
return;
}
// If error is null, list is guaranteed to be not empty.
Log.d(LOG_TAG, "Autosuggest results: " + list.size());
for (Suggestion autosuggestResult : list) {
String addressText = "Not a place.";
Place place = autosuggestResult.getPlace();
if (place != null) {
addressText = place.getAddress().addressText;
}
Log.d(LOG_TAG, "Autosuggest result: " + autosuggestResult.getTitle() +
" addressText: " + addressText);
}
}
};
private void autoSuggestExample() {
GeoCoordinates centerGeoCoordinates = getMapViewCenter();
SearchOptions searchOptions = new SearchOptions();
searchOptions.languageCode = LanguageCode.EN_US;
searchOptions.maxItems = 5;
TextQuery.Area queryArea = new TextQuery.Area(centerGeoCoordinates);
// Simulate a user typing a search term.
searchEngine.suggest(
new TextQuery("p", // User typed "p".
queryArea),
searchOptions,
autosuggestCallback);
searchEngine.suggest(
new TextQuery("pi", // User typed "pi".
queryArea),
searchOptions,
autosuggestCallback);
searchEngine.suggest(
new TextQuery("piz", // User typed "piz".
queryArea),
searchOptions,
autosuggestCallback);
}
private void geocodeAddressAtLocation(String queryString, GeoCoordinates geoCoordinates) {
clearMap();
AddressQuery query = new AddressQuery(queryString, geoCoordinates);
SearchOptions options = new SearchOptions();
options.languageCode = LanguageCode.DE_DE;
options.maxItems = 30;
searchEngine.search(query, options, geocodeAddressSearchCallback);
}
private final SearchCallback geocodeAddressSearchCallback = new SearchCallback() {
@Override
public void onSearchCompleted(SearchError searchError, List<Place> list) {
if (searchError != null) {
showDialog("Geocoding", "Error: " + searchError.toString());
return;
}
for (Place geocodingResult : list) {
// Note: getGeoCoordinates() may return null only for Suggestions.
GeoCoordinates geoCoordinates = geocodingResult.getGeoCoordinates();
Address address = geocodingResult.getAddress();
String locationDetails = address.addressText
+ ". GeoCoordinates: " + geoCoordinates.latitude
+ ", " + geoCoordinates.longitude;
Log.d(LOG_TAG, "GeocodingResult: " + locationDetails);
addPoiMapMarker(geoCoordinates);
}
showDialog("Geocoding result","Size: " + list.size());
}
};
private void addPoiMapMarker(GeoCoordinates geoCoordinates) {
MapMarker mapMarker = createPoiMapMarker(geoCoordinates);
mapView.getMapScene().addMapMarker(mapMarker);
mapMarkerList.add(mapMarker);
}
private void addPoiMapMarker(GeoCoordinates geoCoordinates, Metadata metadata) {
MapMarker mapMarker = createPoiMapMarker(geoCoordinates);
mapMarker.setMetadata(metadata);
mapView.getMapScene().addMapMarker(mapMarker);
mapMarkerList.add(mapMarker);
}
private MapMarker createPoiMapMarker(GeoCoordinates geoCoordinates) {
MapImage mapImage = MapImageFactory.fromResource(context.getResources(), R.drawable.poi);
return new MapMarker(geoCoordinates, mapImage, new Anchor2D(0.5F, 1));
}
private GeoCoordinates getMapViewCenter() {
return mapView.getCamera().getState().targetCoordinates;
}
private GeoBox getMapViewGeoBox() {
int mapViewWidthInPixels = mapView.getWidth();
int mapViewHeightInPixels = mapView.getHeight();
Point2D bottomLeftPoint2D = new Point2D(0, mapViewHeightInPixels);
Point2D topRightPoint2D = new Point2D(mapViewWidthInPixels, 0);
GeoCoordinates southWestCorner = mapView.viewToGeoCoordinates(bottomLeftPoint2D);
GeoCoordinates northEastCorner = mapView.viewToGeoCoordinates(topRightPoint2D);
if (southWestCorner == null || northEastCorner == null) {
throw new RuntimeException("GeoBox creation failed, corners are null.");
}
// Note: This algorithm assumes an unrotated map view.
return new GeoBox(southWestCorner, northEastCorner);
}
private void clearMap() {
for (MapMarker mapMarker : mapMarkerList) {
mapView.getMapScene().removeMapMarker(mapMarker);
}
mapMarkerList.clear();
}
private void showDialog(String title, String message) {
AlertDialog.Builder builder =
new AlertDialog.Builder(context);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
}
1条答案
按热度按时间mzmfm0qo1#
错误信息非常清楚:“这些凭据不授权访问”。如果您看到Map,并不一定意味着凭据正确。例如,在没有internet连接时,您仍然可以看到Map,因为Map数据是以前缓存的。
如果您100%确定您使用的是正确的凭据,您必须联系HERE支持。我刚刚测试了我的凭据,它们仍然按预期工作。