Bing Maps Android SDK中的多边形信息框

时间:2012-06-21 11:47:23

标签: android gis bing-maps

我正在使用Bing Maps Android SDK,我正在寻找一种方法来点击我创建的多边形并显示信息框。我已经能够为图钉完成此操作,但不能用于多边形。我见过this answer,但我的应用程序需要生成数百个这样的多边形,我正在寻找一个更快的解决方案,使用多边形上的addHandler方法。我知道这可能是SDK的AJAX v7风格,它是Android SDK的基础。

我为AJAX版本尝试的代码(使用this模拟器进行测试。)

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的assets文件夹中将类似的代码推送到BingMapsAndroid.js,但这不起作用。当我使用hasHandler方法检查时,附加了处理程序。记录触摸并将其纬度和长值发送到日志,但即使触摸位于多边形内,也不会引发多边形事件。

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);
    }
}

1 个答案:

答案 0 :(得分:3)

经过大量测试后,我发现问题出在Android上。我尝试使用Bing Maps Interactive SDK for AjaxV7上的代码,这些是我的结果:

  • 桌面浏览器:有效(如问题中所述)
  • iPhone 4S:正常工作
  • 使用默认浏览器的Android 2.3.4:无法正常工作
  • Android 2.3.4 with Opera:作品
  • 使用Opera的Android ICS:正常工作
  • 使用默认浏览器的Android ICS:无法正常工作