我正在使用Bing Maps Android SDK,我正在寻找一种方法来单击我创建的多边形并显示一个信息框。我已经能够为图钉完成此操作,但不能为多边形完成。我已经看到了这个答案,但是我的应用程序需要制作数百个这样的多边形,我正在寻找一种更快的解决方案,在多边形上使用 addHandler 方法。我知道这对于 SDK 的 AJAX v7 风格是可能的,它是 Android SDK 的底层基础。
我为 AJAX 版本尝试的代码(使用此模拟器测试。)
map.entities.clear();
latlon = map.getCenter();
var polygon = new Microsoft.Maps.Polygon([new Microsoft.Maps.Location(latlon.latitude, latlon.longitude-0.15), new Microsoft.Maps.Location(latlon.latitude+0.1, latlon.longitude-0.05), new Microsoft.Maps.Location(latlon.latitude+0.1, latlon.longitude+0.05), new Microsoft.Maps.Location(latlon.latitude, latlon.longitude+0.15), new Microsoft.Maps.Location(latlon.latitude-0.1, latlon.longitude+0.05), new Microsoft.Maps.Location(latlon.latitude-0.1, latlon.longitude-0.05), new Microsoft.Maps.Location(latlon.latitude, latlon.longitude-0.15)], null);
Microsoft.Maps.Events.addHandler(polygon, 'click', DisplayInfo);
map.setView( {zoom:10});
map.entities.push(polygon);
function DisplayInfo (e) {
var vertices = e.target.getLocations();
var verticeCenter = new Microsoft.Maps.Location(0,0);
//Calculating location of center
for (i=0; i<vertices.length-1; i++) {
verticeCenter.latitude = verticeCenter.latitude + vertices[i].latitude;
verticeCenter.longitude = verticeCenter.longitude + vertices[i].longitude;
}
verticeCenter.latitude = verticeCenter.latitude / (vertices.length - 1);
verticeCenter.longitude = verticeCenter.longitude / (vertices.length - 1);
defaultInfobox = new Microsoft.Maps.Infobox(verticeCenter, {width: 200, height: 50} );
map.entities.push(defaultInfobox);
}
但是,我从 SDK 的资产文件夹中将类似的代码推送到了 BingMapsAndroid.js,但这不起作用。处理程序已附加,正如我使用 hasHandler 方法检查的那样。记录触摸并将它们的 lat 和 long 值发送到日志,但即使触摸位于多边形内,也不会触发多边形事件。
BingMapsAndroid.js 中的多边形测试函数:
this.PolygonTest = function() {
_map.entities.clear();
latlon = new Microsoft.Maps.Location(1,1);
console.log("Polygon test function");
var polygon = new Microsoft.Maps.Polygon([new Microsoft.Maps.Location(latlon.latitude, latlon.longitude-0.15), new Microsoft.Maps.Location(latlon.latitude+0.1, latlon.longitude-0.05), new Microsoft.Maps.Location(latlon.latitude+0.1, latlon.longitude+0.05), new Microsoft.Maps.Location(latlon.latitude, latlon.longitude+0.15), new Microsoft.Maps.Location(latlon.latitude-0.1, latlon.longitude+0.05), new Microsoft.Maps.Location(latlon.latitude-0.1, latlon.longitude-0.05), new Microsoft.Maps.Location(latlon.latitude, latlon.longitude-0.15)], null);
try {
Microsoft.Maps.Events.addHandler(polygon, 'click', function(e) { console.log("Polygon click!"); }); //This is never evoked
Microsoft.Maps.Events.addHandler(_map, 'click', function(e) { var point = new MM.Point(e.getX(), e.getY()); var loc = e.target.tryPixelToLocation(point); console.log("lat: " + loc.latitude + ", lon: " + loc.longitude); });
} catch(e) {
alert("Error");
}
_map.setView( {zoom:10});
_map.entities.push(polygon);
if (Microsoft.Maps.Events.hasHandler(polygon,'click')) {
console.log("Polygon has click handler."); //This works
}
//This function should be added to the click handler for polygon. I'll add it when I know the handler works.
function DisplayInfo (e) {
console.log("Polygon has been clicked.");
var vertices = e.target.getLocations();
var verticeCenter = new Microsoft.Maps.Location(0,0);
for (i=0; i<vertices.length-1; i++) {
verticeCenter.latitude = verticeCenter.latitude + vertices[i].latitude;
verticeCenter.longitude = verticeCenter.longitude + vertices[i].longitude;
}
verticeCenter.latitude = verticeCenter.latitude / (vertices.length - 1);
verticeCenter.longitude = verticeCenter.longitude / (vertices.length - 1);
defaultInfobox = new Microsoft.Maps.Infobox(verticeCenter, { width: 200, height: 50 });
_map.entities.push(defaultInfobox);
}
}