要在选择建议时让地图移动/缩放到某个位置,您需要实现一个onResults
处理程序:
var fromSearchBox = new nokia.places.widgets.SearchBox({
targetNode: "fromSearchBox",
template: "fromSearchBox",
map: map,
onResults: function (data) {
// Your code goes here //
}
});
正如您所注意到的,该data
字段可能包含也可能不包含边界框。这是因为只有少数几个位置覆盖了一个定义的区域,大多数是点地址。
对于具有边界框的子集,您可以执行以下操作:
if (data.results.items[0].boundingBox){
map.zoomTo(data.results.items[0].getBoundingBox());
}
对于其余部分,答案将取决于您要达到的目标,但您可以尝试以下任何一种方法:
在不改变缩放的情况下移动到地图上的位置。(即让用户决定)
map.set("center", data.results.items[0].position);
移动到指定位置的点对象的指定边界框。
var obj = new nokia.maps.map.StandardMarker(startPoint);
map.zoomTo(obj.getBoundingBox());
或者:map.zoomTo(nokia.maps.geo.BoundingBox.coverAll([startPoint]));
定义围绕点位置的边界框并缩放到该位置
startPoint =data.results.items[0].position;
bottomLeft = new nokia.maps.geo.Coordinate(startPoint.latitude - 0.1,
startPoint.longitude + 0.1);
topRight = new nokia.maps.geo.Coordinate(startPoint.latitude + 0.1,
startPoint.longitude - 0.1);
map.zoomTo(nokia.maps.geo.BoundingBox.coverAll([topRight, bottomLeft]));
Additionally, Display.zoomTo() can also take an additional parameter, so if you use map.zoomTo(BoundingBox, true)
you will also keep the current center for the map on screen, and this may give more context to a user.