如何在鼠标悬停时添加关于bing的图钉说明?

时间:2011-01-06 10:38:11

标签: javascript bing-maps

请帮我在bing map中的推针上添加鼠标悬停的描述? 或者,当鼠标翻过推针时,请帮我调用一个功能 感谢

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
        <head>
            <title></title>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
                <script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
                <script type="text/javascript">

    var map = null;
    function GetMap() {

        // Initialize the map

        map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), { credentials: "Ah6hamk-cQOK9E8CiVl2mvmNR1f0UWpQJXNKxuWNhDBWiFCbpreme4p6Qpj6C03s", mapTypeId: "r" });

    }
    function ClickGeocode(credentials) {
        map.getCredentials(MakeGeocodeRequest);
    }
    function MakeGeocodeRequest(credentials) {
        var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/Cochin?output=json&jsonp=GeocodeCallback&key=" + credentials;

        CallRestService(geocodeRequest);

    }
    function GeocodeCallback(result) {
        //alert("Found location: " + result.resourceSets[0].resources[0].name);

        if (result &&
            result.resourceSets &&
            result.resourceSets.length > 0 &&
            result.resourceSets[0].resources &&
            result.resourceSets[0].resources.length > 0) {
            // Set the map view using the returned bounding box
            var bbox = result.resourceSets[0].resources[0].bbox;
            var viewBoundaries = Microsoft.Maps.LocationRect.fromLocations(new Microsoft.Maps.Location(bbox[0], bbox[1]), new Microsoft.Maps.Location(bbox[2], bbox[3]));
            map.setView({ bounds: viewBoundaries });
            // Add a pushpin at the found location
            var location = new Microsoft.Maps.Location(result.resourceSets[0].resources[0].point.coordinates[0], result.resourceSets[0].resources[0].point.coordinates[1]);
            //alert(location);
            var pushpin = new Microsoft.Maps.Pushpin(location);
            map.entities.push(pushpin);
        }
    }
    function CallRestService(request) {
        var script = document.createElement("script");
        script.setAttribute("type", "text/javascript");
        script.setAttribute("src", request);
        document.body.appendChild(script);
    }
</script>

              

2 个答案:

答案 0 :(得分:1)

在“var pushpin”和“map.entities.push ...”行之间,添加下面的内容。那会让你开始。基本上,您要添加一个事件处理程序。

请注意,在这种情况下,事件会添加到“pin” - 但如果我们在此处有一个,则可以添加到其他对象,例如“信息框”。只有少数支持的事件 - 检查文档。但是,一旦你连接了这个事件,你就可以做一些巧妙的事情。玩得开心!

CODE:

var pushpin = new Microsoft.Maps.Pushpin(location);

Microsoft.Maps.Events.addHandler(pushpin, 'mouseover', function () {
alert("The mouse went over the pin");});               

map.entities.push(pushpin);

答案 1 :(得分:0)

卢埃林答案有效。另一种方法是在图钉行中为'typeName'参数设置一个值:

var pushpin = new Microsoft.Maps.Pushpin(location, {typeName: 'pin123'});
map.entities.push(pushpin);

接下来,使用jQuery,您可以添加元素或捕获事件:

$(".pin123" ).mouseover(function() { alert("hey!"); });