I’m having problem with the EntityClickedListener in Bing Maps. I coded it following the example given:
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();
}
});
However, clicking on the the Pushpin does nothing. I created a Toast message to see exactly what’s in the metadata but nothing happens. Looking through the path the application, I can tell that Bing uses a data service to retrieve its information:
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();
}
};
I’ve also edited the JavaScript file to remove some checking that may have prevented the Listener from working:
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
};
Have I edited the JavaScript file correctly? I don’t know JavaScript myself and I’ve been following this guide. The Pushpin is being added and I’m seeing on my BingMapsView, but the OnClick method doesn’t execute. Is there something I’m missing?
The path that Bing takes to create Listeners and events for Pushpin click events is almost a labyrinth in my opinion. There has to be a simpler way to do this that I’m missing. Can anyone give any advice?
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.
Then, change the
ShowInfoBoxmethod in the JavaScript file toThis code funnels straight to the click listener you code in your
Activityinstead of messing around with it in the JavaScript. The difference from the JavaScript I edited earlier is that the parameter for theentityClickedmethod should actually beentity.entityIdnotentityId.