执行搜索建议时如何获取 DiscoveryResult 的位置?
How can I get Location of a DiscoveryResult when executing search suggestion?
我正在使用 android SDK 实现搜索建议,代码如下所示:
private void performSearch(CharSequence searchTerm) {
try {
DiscoveryRequest request = new SearchRequest( searchTerm.toString())
.setSearchCenter( mMap.getCenter() )
.setCollectionSize( 10 );
ErrorCode error = request.execute( mSearchRequestListener );
if ( error != ErrorCode.NONE ) {
Log.i( TAG, "Here API place search error: " + error.name() );
mSearchAdapter.notifyDataSetInvalidated();
}
} catch ( IllegalArgumentException ex ) {
Log.i( TAG, "Here API place search exception: " +
ex.getMessage() != null ? ex.getMessage() : "" );
mSearchAdapter.notifyDataSetInvalidated();
}
}
private ResultListener<DiscoveryResultPage> mSearchRequestListener =
new ResultListener<DiscoveryResultPage>() {
@Override
public void onCompleted(DiscoveryResultPage data, ErrorCode error) {
if ( error != ErrorCode.NONE ) {
Log.i( TAG, "Here API place search error: " + error.name() );
mSearchAdapter.notifyDataSetInvalidated();
return;
}
Log.d(TAG, "mSearchRequestListener.onCompleted: count=" + data.getItems().size() );
mResultList = new ArrayList<DiscoveryResult>( data.getItems());
String vicinity = mResultList.get(0).getVicinity();
//String location = ?
mSearchAdapter.notifyDataSetChanged();
}
};
在获取 DiscoveryResult 列表后如何获取 DiscoveryResult 的 Location?我似乎没有在 DiscoveryResult 对象中找到这个 属性。我需要将此位置添加到我的 RoutePlan 中以计算路线。
RoutePlan routePlan = new RoutePlan();
routePlan.addWaypoint(currentGeoCoordinate);
routePlan.addWaypoint(destGeoCoordinate);
routeManager.calculateRoute( routePlan, mRouteManagerListener );
我的代码中有一个解决方法。到达附近后,我在 http://geocoder.cit.api.here.com/6.2/geocode.json?searchtext=" + 附近 + "gen=9"; 发出请求以获取响应。有一个 Location 属性 响应此请求。缺点是我需要每次都请求获取位置。有什么建议可以在不向服务器发出请求的情况下获取位置吗?
提前致谢。
DiscoveryResultPage 中的数据可以有多种类型,您应该寻找合适的类型。
在此处查看文档:
https://developer.here.com/mobile-sdks/documentation/android-hybrid-plus/topics/places.html
重要部分:
Calling DiscoveryResultPage.getItems(), returns a List containing one
of the following types of objects, which are DiscoveryResult
instances. DiscoveryResult is a collection of Link subtypes.
PlaceLink - Represents discovery information about a Place. The
PlaceLink contains a brief summary about a place. Details about a
place are available from the Place that the PlaceLink references.
DiscoveryLink - Represents a discovery-related API link used to
retrieve additional DiscoveryResultPage. This type of Link can be a
result item in an Explore or Here type of search. The DiscoveryLink
references refined discovery requests resulting in more specific
results. For example, the DiscoveryLink may link to a discovery
request to search for 'Eat & Drink', 'Going Out', 'Accommodation', and
so on. Since there may be new types of Link items in the future, it is
recommended that each type of DiscoveryResult be checked before it is
used (as shown in the following code snippet).
实际上,这意味着您迭代数据并检查类型,如下所示:
// Implement a search result listener
ResultListener<DiscoveryResultPage> searchListener = new ResultListener<DiscoveryResultPage>() {
@Override
public void onCompleted(DiscoveryResultPage results, ErrorCode error) {
if (error == ErrorCode.NONE) {
// The results is a DiscoveryResultPage which represents a
// paginated collection of items.
List<DiscoveryResult> items = results.getItems();
// Iterate through the found place items.
for (DiscoveryResult item : items) {
// A Item can either be a PlaceLink (meta information
// about a Place) or a DiscoveryLink (which is a reference
// to another refined search that is related to the
// original search; for example, a search for
// "Leisure & Outdoor").
if (item.getResultType() == ResultType.PLACE) {
PlaceLink placeLink = (PlaceLink) item;
// PlaceLink should be presented to the user, so the link can be
// selected in order to retrieve additional details about a place
// of interest.
...
} else if (item.getResultType() == ResultType.DISCOVERY) {
DiscoveryLink discoveryLink = (DiscoveryLink) item;
// DiscoveryLink can also be presented to the user.
// When a DiscoveryLink is selected, another search request should be
// performed to retrieve results for a specific category.
...
}
}
} else {
// Handle search request error.
}
}
};
或者像这样直接获取 PlaceLink 项目:
public void onCompleted(DiscoveryResultPage data, ErrorCode error) {
List<PlaceLink> results = data.getPlaceLinks(); // we are only interested in PlaceLinks
if (results.size() > 0) {
for (PlaceLink result : results) {
// do something with the PlaceLink item
}
} else {
// handle empty result case
}
然后 PlaceLink 对象具有您想要的所有方法,还有 getPosition() 或 getDistance(),您可以使用它们将结果放在地图上或计算路线。
我正在使用 android SDK 实现搜索建议,代码如下所示:
private void performSearch(CharSequence searchTerm) {
try {
DiscoveryRequest request = new SearchRequest( searchTerm.toString())
.setSearchCenter( mMap.getCenter() )
.setCollectionSize( 10 );
ErrorCode error = request.execute( mSearchRequestListener );
if ( error != ErrorCode.NONE ) {
Log.i( TAG, "Here API place search error: " + error.name() );
mSearchAdapter.notifyDataSetInvalidated();
}
} catch ( IllegalArgumentException ex ) {
Log.i( TAG, "Here API place search exception: " +
ex.getMessage() != null ? ex.getMessage() : "" );
mSearchAdapter.notifyDataSetInvalidated();
}
}
private ResultListener<DiscoveryResultPage> mSearchRequestListener =
new ResultListener<DiscoveryResultPage>() {
@Override
public void onCompleted(DiscoveryResultPage data, ErrorCode error) {
if ( error != ErrorCode.NONE ) {
Log.i( TAG, "Here API place search error: " + error.name() );
mSearchAdapter.notifyDataSetInvalidated();
return;
}
Log.d(TAG, "mSearchRequestListener.onCompleted: count=" + data.getItems().size() );
mResultList = new ArrayList<DiscoveryResult>( data.getItems());
String vicinity = mResultList.get(0).getVicinity();
//String location = ?
mSearchAdapter.notifyDataSetChanged();
}
};
在获取 DiscoveryResult 列表后如何获取 DiscoveryResult 的 Location?我似乎没有在 DiscoveryResult 对象中找到这个 属性。我需要将此位置添加到我的 RoutePlan 中以计算路线。
RoutePlan routePlan = new RoutePlan();
routePlan.addWaypoint(currentGeoCoordinate);
routePlan.addWaypoint(destGeoCoordinate);
routeManager.calculateRoute( routePlan, mRouteManagerListener );
我的代码中有一个解决方法。到达附近后,我在 http://geocoder.cit.api.here.com/6.2/geocode.json?searchtext=" + 附近 + "gen=9"; 发出请求以获取响应。有一个 Location 属性 响应此请求。缺点是我需要每次都请求获取位置。有什么建议可以在不向服务器发出请求的情况下获取位置吗?
提前致谢。
DiscoveryResultPage 中的数据可以有多种类型,您应该寻找合适的类型。
在此处查看文档: https://developer.here.com/mobile-sdks/documentation/android-hybrid-plus/topics/places.html
重要部分:
Calling DiscoveryResultPage.getItems(), returns a List containing one of the following types of objects, which are DiscoveryResult instances. DiscoveryResult is a collection of Link subtypes.
PlaceLink - Represents discovery information about a Place. The PlaceLink contains a brief summary about a place. Details about a place are available from the Place that the PlaceLink references. DiscoveryLink - Represents a discovery-related API link used to retrieve additional DiscoveryResultPage. This type of Link can be a result item in an Explore or Here type of search. The DiscoveryLink references refined discovery requests resulting in more specific results. For example, the DiscoveryLink may link to a discovery request to search for 'Eat & Drink', 'Going Out', 'Accommodation', and so on. Since there may be new types of Link items in the future, it is recommended that each type of DiscoveryResult be checked before it is used (as shown in the following code snippet).
实际上,这意味着您迭代数据并检查类型,如下所示:
// Implement a search result listener
ResultListener<DiscoveryResultPage> searchListener = new ResultListener<DiscoveryResultPage>() {
@Override
public void onCompleted(DiscoveryResultPage results, ErrorCode error) {
if (error == ErrorCode.NONE) {
// The results is a DiscoveryResultPage which represents a
// paginated collection of items.
List<DiscoveryResult> items = results.getItems();
// Iterate through the found place items.
for (DiscoveryResult item : items) {
// A Item can either be a PlaceLink (meta information
// about a Place) or a DiscoveryLink (which is a reference
// to another refined search that is related to the
// original search; for example, a search for
// "Leisure & Outdoor").
if (item.getResultType() == ResultType.PLACE) {
PlaceLink placeLink = (PlaceLink) item;
// PlaceLink should be presented to the user, so the link can be
// selected in order to retrieve additional details about a place
// of interest.
...
} else if (item.getResultType() == ResultType.DISCOVERY) {
DiscoveryLink discoveryLink = (DiscoveryLink) item;
// DiscoveryLink can also be presented to the user.
// When a DiscoveryLink is selected, another search request should be
// performed to retrieve results for a specific category.
...
}
}
} else {
// Handle search request error.
}
}
};
或者像这样直接获取 PlaceLink 项目:
public void onCompleted(DiscoveryResultPage data, ErrorCode error) {
List<PlaceLink> results = data.getPlaceLinks(); // we are only interested in PlaceLinks
if (results.size() > 0) {
for (PlaceLink result : results) {
// do something with the PlaceLink item
}
} else {
// handle empty result case
}
然后 PlaceLink 对象具有您想要的所有方法,还有 getPosition() 或 getDistance(),您可以使用它们将结果放在地图上或计算路线。