5

我在使用EntityClickedListenerBing 地图时遇到问题。我按照给出的示例对其进行了编码:

map.setEntityClickedListener(new EntityClickedListener()
{
   @Override
   public void onAvailableChecked(String layerName, int entityId)
   {
      HashMap<String, Object> metadata = map.getLayerManager.GetMetadataByID(layerName, entityId);
      Toast.makeText(Activity.this, metadata.toString(), Toast.LENGTH_LONG)
           .show();
   }
});

但是,单击Pushpin没有任何作用。我创建了一条 Toast 消息以准确查看元数据中的内容,但没有任何反应。查看应用程序的路径,我可以看出 Bing 使用数据服务来检索其信息:

bsds.FindByAreaCompleted = new Handler(){
     public void handleMessage(Message msg) {
     if(msg.obj != null){
        Record[] records = (Record[])msg.obj;
        EntityLayer el = (EntityLayer)bingMapsView.getLayerManager().getLayerByName(Constants.DataLayers.Search);
        double maxLat = -90, minLat = 90, maxLon = -180, minLon = 180;

        for(Record r : records){
            Pushpin p = new Pushpin(r.Location);
            p.Title = r.DisplayName;
            HashMap<String, Object> metadata = new HashMap<String, Object>();
            metadata.put("record", r);
            el.add(p, metadata);
        }

     bingMapsView.setMapView(new LocationRect(maxLat, maxLon, minLat, minLon));

     el.updateLayer();
     }
};

我还编辑了 JavaScript 文件以删除一些可能阻止其Listener工作的检查:

this.ShowInfobox = function(entity){
        window.BingMapsInterlop.entityClicked(entity.layerName, entityId);
    };

var Layer = function(name, map, dataLayer)
{
    this.Name = name;

    var entities = new MM.EntityCollection(),
    _map = map;

    this.AddEntities = function(data)
    {
        if(data != null)
        {
            var i = data.length - 1;
            if (i >= 0)
            {
                do
                {
                    data[i].Entity.entityId = data[i].EntityId;
                    data[i].Entity.layerName = name;

                    // Commented out
                    // if(data[i].title != null && data[i].title != undefined && data[i].title != '')
                    // {
                        data[i].Entity.title = data[i].title;

                        MM.Events.addHandler(data[i].Entity, 'click', function(e)
                        {
                            BingMapsAndroid.ShowInfobox(e.target);
                        });
                    // }
                    entities.push(data[i].Entity);
                }
                while (i--)
            }
        }
    };
    // fluff
};

我是否正确编辑了 JavaScript 文件?我自己不了解 JavaScript,我一直在关注本指南Pushpin正在添加,我在我的 上看到,BingMapsView但该OnClick方法没有执行。有什么我想念的吗?

在我看来,必应为Pushpin点击事件创建监听器和事件的路径几乎是一个迷宫。必须有一种更简单的方法来做到这一点,我错过了。任何人都可以提供任何建议吗?

4

1 回答 1

2

I have finally found the answer! Edit the following methods in JavaScript file like so such that the script doesn't check for metadata that doesn't exist.

var Layer = function(name, map, dataLayer)
{
    this.Name = name;

    var entities = new MM.EntityCollection(),
    _map = map;

    this.AddEntities = function(data)
    {
        if(data != null)
        {
            var i = data.length - 1;
            if (i >= 0)
            {
                do
                {
                    data[i].Entity.entityId = data[i].EntityId;
                    data[i].Entity.layerName = name;

                    // Commented out
                    // if(data[i].title != null && data[i].title != undefined && data[i].title != '')
                    // {
                        data[i].Entity.title = data[i].title;

                        MM.Events.addHandler(data[i].Entity, 'click', function(e)
                        {
                            BingMapsAndroid.ShowInfobox(e.target);
                        });
                    // }
                    entities.push(data[i].Entity);
                }
                while (i--)
            }
        }
    };
    // fluff
};

Then, change the ShowInfoBox method in the JavaScript file to

this.ShowInfobox = function (entity) {
        window.BingMapsInterlop.entityClicked(entity.layerName, entity.entityId);
    };

This code funnels straight to the click listener you code in your Activity instead of messing around with it in the JavaScript. The difference from the JavaScript I edited earlier is that the parameter for the entityClicked method should actually be entity.entityId not entityId.

于 2012-07-12T17:54:34.217 回答