2012-06-21 18 views
5

Estoy usando el Bing Maps Android SDK y estoy buscando una manera de hacer clic en los polígonos que he creado y mostrar un cuadro de información. Pude lograr esto por un marcador, pero no por un polígono. He visto this answer, pero mi aplicación necesita cientos de tales polígonos, y estoy buscando una solución más rápida usando el método addHandler en polígonos. Sé que esto es posible para el sabor AJAX v7 del SDK, que es la base subyacente del SDK de Android.Infobox en polígonos en Bing Maps Android SDK

El código Probé para la versión AJAX (probado usando this emulador.)

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

Sin embargo, empujé un código similar a los BingMapsAndroid.js de la carpeta de los activos del SDK, pero Eso no t trabajo. El controlador está adjunto, como he comprobado usando el método hasHandler. Los toques se graban y sus valores de latitud y longitud se envían al registro, pero el evento de polígono no se evoca incluso cuando el toque se encuentra dentro de un polígono.

función de prueba en Polígono 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); 
    } 
} 

Respuesta

3

Después de muchas pruebas, he encontrado el problema radica en Android. He probado el código en Bing Maps Interactivo SDK para AjaxV7 y estos son mis resultados: navegadores

  • de Escritorio: obras (como está escrito en la pregunta)
  • iPhone 4S: Obras
  • 2.3.4 de Android con defecto navegador: no funciona
  • 2.3.4 Android con Opera: trabaja
  • Android ICS con Opera: trabaja
  • Android ICS con el navegador por defecto: no funciona
Cuestiones relacionadas