问题
I'm trying to do a simple places search within a specified rectangle area. My assumption was that the boundingBox Parameter for the findPlaces() method was to limit the search results to the specified rectangular area. But when I do a search with a specified boundingBox I get results all over the map.
That's essentially what I'm doing:
//create coordinates
var mapCenter = map.center;
var topLeft = new nokia.maps.geo.Coordinate(mapCenter.latitude + .005, mapCenter.longitude - .01);
var bottomRight = new nokia.maps.geo.Coordinate(mapCenter.latitude - .005, mapCenter.longitude + .01);
//create bounding box
var bBox = new nokia.maps.geo.BoundingBox(topLeft, bottomRight);
//create rectangle
var searchRectangle = new nokia.maps.map.Rectangle(bBox);
map.objects.add(searchRectangle);
//do the search
nokia.places.search.manager.findPlaces({
searchTerm: "Starbucks",
onComplete: processResults,
boundingBox: bBox
});
Unfortunately I can't post any Images. But basically what I get is a rectangle at the center of the map and many Markers inside but also outside of the rectangle. Why is that?
Edit: I noticed that when I enter a searchterm like "food" rather than "Starbucks" (where "food" obviously gets more results) most of the results are actually inside the rectangle. But still some arent. Also after experimenting with different terms it looks like the search manager always tries to return 20 results. It just starts to look for places inside the rectangle but when it can't find no more results it also looks outside till it found 20. Is that correct? If yes, how can I make it only search inside the rectangle?
回答1:
The Places section of the Maps API JavaScript is merely a wrapper around the RESTful Places API. The area Bounding Box you pass into the findPlaces()
method is setting the location context - it doesn't restrict the results to a location or area, but merely biases the results towards it. The documentation of the Places API describes this better and the use of the location context in a lot more detail.
For a free text search such as you describe, there is a trade off in a fuzzy search between distance from the desired area for results and the match to the search term you entered. Sometimes the match will win over the area.
You have several ways you can counter this.
Firstly you could use the
findPlacesByCategory()
method. In the example you gave of searching for "food", a category search foreat-drink
would give better results (and also avoid issues of the different words for "food" in languages other than English.Secondly you could filter the responses you retrieve and only display matches within your preferred area
An example is shown below:
function onPlaceSearchComplete (data, requestStatus, requestId) {
if (requestStatus == "OK") {
// Convert all found locations with view port into a set of markers
var resultSet = new nokia.maps.map.Container();
var locations = data.results.items;
for (i = 0, len = locations.length; i < len; i++) {
var bbox = nokia.maps.geo.BoundingBox.coverAll([locations[i].position]);
if(map.getViewBounds().contains(bbox)){
marker = new nokia.maps.map.StandardMarker(locations[i].position, { text: i+1 });
resultSet.objects.add(marker);
}
}
map.objects.add(resultSet);
map.zoomTo(resultSet.getBoundingBox(), false);
}
}
- Thirdly you display all the results in a list, but only
zoomTo()
results in your preferred area, by filtering your results into aContainer
first, then allow the User to decide what they really meant:
Something like:
var resultSet = new nokia.maps.map.Container();
for (i = 0, len = locations.length; i < len; i++) {
var bbox = nokia.maps.geo.BoundingBox.coverAll([locations[i].position]);
marker = new nokia.maps.map.StandardMarker(locations[i].position, { text: i+1 });
if(map.getViewBounds().contains(bbox)){
resultSet.objects.add(marker); // Add to container
} else {
map.objects.add(marker); // Add to map only.
}
}
map.objects.add(resultSet);
map.zoomTo(resultSet.getBoundingBox(), false);
来源:https://stackoverflow.com/questions/20600367/why-are-search-results-not-limited-to-the-bounding-box