javascript - Bing map - 在图钉上调用点击事件

标签 javascript bing-maps

我有以下必应 map :https://www.bob-elliot.co.uk/locateV2.php

当在左侧列表中选择一家商店时, map 会自动以该图钉为中心,我现在需要做的是调用该图钉的 click() 句柄(或显示相关的信息框)。

问题是,我将引脚插入名为 dataLayerMicrosoft.Maps.EntityCollection();,所以我无法直接调用 map.entities.get() - 或者更确切地说 - 如果我尝试以这种方式调用它,get() 不会返回任何内容。

请注意此代码段底部的 zoomMap() 函数,我已注释掉有问题的行。

如何调用点击事件?

var map = null;
var dataLayer = null;
var infoboxLayer = null;

function loadMap() {
    //#### Prep default location
    var defaultLocation = new Microsoft.Maps.Location(53.6880, -2.6646);

    //#### Prep Map Options
    var mapOptions = {
        credentials: 'KEY_REMOVED',
        center: defaultLocation,
        mapTypeId: Microsoft.Maps.MapTypeId.road,
        zoom: 7,
        enableClickableLogo: false,
        enableSearchLogo: false
    }

    //#### Initialise the primary map control
    map = new Microsoft.Maps.Map(document.getElementById('BobElliotMap'), mapOptions);

    //#### Add data layer
    dataLayer = new Microsoft.Maps.EntityCollection();
    map.entities.push(dataLayer);

    //#### Add infobox layer
    infoboxLayer = new Microsoft.Maps.EntityCollection();
    map.entities.push(infoboxLayer);

    //#### Prep InfoBox & add to infobox layer
    var infoboxOptions = {
        width: 300,
        height: 200,
        visible: false,
        offset: new Microsoft.Maps.Point(0, 20)
    };

    infobox = new Microsoft.Maps.Infobox(new Microsoft.Maps.Location(0, 0), infoboxOptions);
    infoboxLayer.push(infobox);

    //#### Add Data to the map
    DrawPins();
}

function displayInfobox(e) {
    if (e.targetType == "pushpin") {
        infobox.setOptions({
            title: e.target.Title,
            description: e.target.Description,
            visible: true,
            offset: new Microsoft.Maps.Point(0, 25)
        });
        infobox.setLocation(e.target.getLocation());

        //#### A buffer limit to use to specify the infobox must be away from the edges of the map.
        var buffer = 30;
        var infoboxOffset = infobox.getOffset();
        var infoboxAnchor = infobox.getAnchor();
        var infoboxLocation = map.tryLocationToPixel(e.target.getLocation(), Microsoft.Maps.PixelReference.control);
        var dx = infoboxLocation.x + infoboxOffset.x - infoboxAnchor.x;
        var dy = infoboxLocation.y - 25 - infoboxAnchor.y;

        if (dy < buffer) { //Infobox overlaps with top of map.
            //#### Offset in opposite direction.
            dy *= -1;
            //#### add buffer from the top edge of the map.
            dy += buffer;
        } else {
            //#### If dy is greater than zero than it does not overlap.
            dy = 0;
        }

        if (dx < buffer) { //Check to see if overlapping with left side of map.
            //#### Offset in opposite direction.
            dx *= -1;
            //#### add a buffer from the left edge of the map.
            dx += buffer;
        } else { //Check to see if overlapping with right side of map.
            dx = map.getWidth() - infoboxLocation.x + infoboxAnchor.x - infobox.getWidth();
            //#### If dx is greater than zero then it does not overlap.
            if (dx > buffer) {
                dx = 0;
            } else {
                //#### add a buffer from the right edge of the map.
                dx -= buffer;
            }
        }

        //#### Adjust the map so infobox is in view
        if (dx != 0 || dy != 0) {
            map.setView({
                centerOffset: new Microsoft.Maps.Point(dx, dy),
                center: map.getCenter()
            });
        }
    }
}

function DrawPins() {
    var pin0 = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(53.1913, -2.51945));
    pin0.Title = "** CYCLONE CYCLES A/C CLOSED";
    pin0.Description = "74 WEAVER STREET<br />WINSFORD<br />CHESHIRE<br />CW7 4AA<br /><br /><b>Tel:</b> +44606861992";
    Microsoft.Maps.Events.addHandler(pin0, 'click', displayInfobox);
    dataLayer.push(pin0);
    var pin1 = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(53.7485, -0.348162));
    pin1.Title = "**2 WHEELS CYCLES LIMITED - HULL";
    pin1.Description = "8 SPRING STREET<br />HULL<br />HU2 8RB<br /><br /><b>Tel:</b> +44482216170<br /><b>Fax:</b> +44828515<br /><b>Web:</b> <a href='http://www.2wheelscycles.co.uk' targer='_blank'>www.2wheelscycles.co.uk</a>";
    Microsoft.Maps.Events.addHandler(pin1, 'click', displayInfobox);
    dataLayer.push(pin1);
}

function zoomMap(PinID, Lat, Long) {
    var ZoomLocation = new Microsoft.Maps.Location(Lat, Long);
    map.setView({
        zoom: 13,
        center: ZoomLocation
    });

    //#### These are the lines that dont work, selectedPin remains undefined
    //var selectedPin = dataLayer.entities.get(PinID);
    //Microsoft.Maps.Events.invoke(selectedPin, 'click', '');
}

最佳答案

由于某些原因,e.target 似乎没有出现在 displayInfobox 函数中。

我只是在 2、3 个地方稍微修改了你的代码,以获得接近你的预期点的东西。请检查以下内容:

var map = null;    
var dataLayer = null;    
var infoboxLayer = null;

function loadMap() {

    //Prep default location    
    var defaultLocation = new Microsoft.Maps.Location(53.6880, -2.6646);

    //Prep Map Options    
    var mapOptions = {
        //credentials: 'KEY_REMOVED',
        center: defaultLocation,
        mapTypeId: Microsoft.Maps.MapTypeId.road,
        zoom: 7,
        enableClickableLogo: false,
        enableSearchLogo: false
    }

    //Initialise the primary map control
    map = new Microsoft.Maps.Map(document.getElementById('BobElliotMap'), mapOptions);

    //Add data layer
    dataLayer = new Microsoft.Maps.EntityCollection();
    map.entities.push(dataLayer);

    // Add infobox layer
    infoboxLayer = new Microsoft.Maps.EntityCollection();
    map.entities.push(infoboxLayer);

    //Prep InfoBox & add to infobox layer
    var infoboxOptions = {
        width: 300,
        height: 200,
        visible: false,
        offset: new Microsoft.Maps.Point(0, 20)
    };

    infobox = new Microsoft.Maps.Infobox(new Microsoft.Maps.Location(0, 0), infoboxOptions);

    infoboxLayer.push(infobox);

    //Add Data to the map
    DrawPins();


    setTimeout(function() {
        //Initial zoom for testing zoomMap function
        var pushpin = dataLayer.get(0);
        var indx = dataLayer.indexOf(pushpin);
        zoomMap(indx, pushpin.getLocation().latitude, pushpin.getLocation().longitude);
    },500);

}


function displayInfobox(e) {

    if ( this.target.id && this.target.id.indexOf("pin") != -1 ) {
        alert("click event fired on pushpin");

        infobox.setOptions({
            title: this.target.Title,
            description: this.target.Description,
            visible: true,
            offset: new Microsoft.Maps.Point(0, 25)
        });

        infobox.setLocation(this.target.getLocation());

        //A buffer limit to use to specify the infobox must be away from the edges of the map.

        var buffer = 30;
        var infoboxOffset = infobox.getOffset();
        var infoboxAnchor = infobox.getAnchor();
        var infoboxLocation = map.tryLocationToPixel(this.target.getLocation(), Microsoft.Maps.PixelReference.control);
        var dx = infoboxLocation.x + infoboxOffset.x - infoboxAnchor.x;
        var dy = infoboxLocation.y - 25 - infoboxAnchor.y;

        if (dy < buffer) { //Infobox overlaps with top of map.
            //#### Offset in opposite direction.
            dy *= -1;
            //#### add buffer from the top edge of the map.
            dy += buffer;
        } else {
            //#### If dy is greater than zero than it does not overlap.
            dy = 0;
        }

        if (dx < buffer) { //Check to see if overlapping with left side of map.
            //#### Offset in opposite direction.
            dx *= -1;
            //#### add a buffer from the left edge of the map.
            dx += buffer;
        } else { //Check to see if overlapping with right side of map.
            dx = map.getWidth() - infoboxLocation.x + infoboxAnchor.x - infobox.getWidth();
            //#### If dx is greater than zero then it does not overlap.
            if (dx > buffer) {
                dx = 0;
            } else {
                //#### add a buffer from the right edge of the map.
                dx -= buffer;
            }
        }

        //#### Adjust the map so infobox is in view
        if (dx != 0 || dy != 0) {
            map.setView({
                centerOffset: new Microsoft.Maps.Point(dx, dy),
                center: map.getCenter()
            });
        }
    }
}


function DrawPins() {

    var pin0 = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(53.1913, -2.51945));
    pin0.id = "pin_0";
    pin0.Title = "** CYCLONE CYCLES A/C CLOSED";
    pin0.Description = "74 WEAVER STREET<br />WINSFORD<br />CHESHIRE<br />CW7 4AA<br /><br /><b>Tel:</b> +44606861992";
    Microsoft.Maps.Events.addHandler(pin0, 'click', displayInfobox);
    dataLayer.push(pin0);
    var pin1 = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(53.7485, -0.348162));
    pin1.id = "pin_1";
    pin1.Title = "**2 WHEELS CYCLES LIMITED - HULL";
    pin1.Description = "8 SPRING STREET<br />HULL<br />HU2 8RB<br /><br /><b>Tel:</b> +44482216170<br /><b>Fax:</b> +44828515<br /><b>Web:</b> <a href='http://www.2wheelscycles.co.uk' targer='_blank'>www.2wheelscycles.co.uk</a>";
    Microsoft.Maps.Events.addHandler(pin1, 'click', displayInfobox);
    dataLayer.push(pin1);
}


function zoomMap(indx, Lat, Long) {

    var ZoomLocation = new Microsoft.Maps.Location(Lat, Long);

    map.setView({
        zoom: 13,
        center: ZoomLocation
    });

    //#### These are the lines that dont work, selectedPin remains undefined
    //var selectedPin = dataLayer.entities.get(PinID);
    //Microsoft.Maps.Events.invoke(selectedPin, 'click', '');

    var selectedPin = dataLayer.get(indx);
    Microsoft.Maps.Events.invoke(selectedPin, 'click');
}

关于javascript - Bing map - 在图钉上调用点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11148042/

相关文章:

javascript - AngularJS 添加到浏览器历史记录而无需重新加载页面且无需 HTML5 pushstate

Javascript 模块在函数中是变量,无法访问外部作用域变量

javascript - 使用散列值数组的 AWS DynamoDB Scan 和 FilterExpression

c# - 使用 C# 为独立应用程序进行 Google 或 Bing map 编程

javascript - 如何超越Bing Map 25个航点限制?

javascript - 如何扩展所有 Sequelize 模型?

javascript - 表达式中的第一个字符不能是破折号 (-)

javascript - map API 不适用于 HTML 中的 Windows Phone

iphone - iPad/iPhone 上的 Bing map

javascript - Ajax Bing map 版本 7 - 移动和删除图钉 - 初学者教程