0

我试试这段代码

for(i=0; i<num ;i++)
{
    points.push([lats[i], lngs[i]]);

    if(i==0) str = 'S';
    else if(i==num-1) str = 'E';
    else str = '';

    var marker = new nokia.maps.map.Marker(
            [lats[i], lngs[i]],
            {
                    title: str,
                    visibility: true,
                    icon: img,
                    anchor: new nokia.maps.util.Point(5, 5)
            });
    marker.addListener('click', function(evt){
            $('.loc').html(times[i]);
    });
    map.objects.add(marker);
}

但它只是不会触发点击事件。代码有什么问题吗?

latslngstimes已定义,points将在以后使用。

编辑:

问题解决了。请参阅下面的评论以获取答案。

4

1 回答 1

0

看起来问题出在以下行:

$('.loc').html(times[i]);

在标记侦听器内。该事件在触发时无法访问数组元素,因此失败。我认为您需要向标记添加一个属性并访问它,如下所示:

var marker = new nokia.maps.map.Marker(
            [lats[i], lngs[i]],
            {
                    title: str,
                    visibility: true,
                    icon: img,
                    anchor: new nokia.maps.util.Point(29, 71),
                    $html : times[i]
            });
    marker.addListener('click', function(evt){
            //$('.loc').html(times[i]); 
           alert (evt.target.$html);
    });

尝试以下操作(当然使用您自己的 App Id 和令牌):

<!DOCTYPE HTML SYSTEM>
<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9" />
        <script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.3/jsl.js"></script>


        <style type="text/css">

            html {
                overflow:hidden;
            }

            body {
                margin: 0;
                padding: 0;
                overflow: hidden;
                width: 100%;
                height: 100%;
                position: absolute;
            }

            #mapContainer {
                width:100%;
                height: 100%;
                left: 0;
                top: 0;
                position: absolute;
            }

 </style>
    </head>
    <body>
        <div id="mapContainer"></div>
        <script type="text/javascript">

            nokia.Settings.set( "appId", "YOUR APP ID GOES HERE"); 
            nokia.Settings.set( "authenticationToken", "YOUR AUTHENTICATION TOKEN GOES HERE");

var mapContainer = document.getElementById("mapContainer");


var DefaultLatitude = 52.516237;
var DefaultLongitude = 13.377686;
var defaultZoomLevel = 16;

var mapOptions =
{
    baseMapType: nokia.maps.map.Display.NORMAL,
    center: new nokia.maps.geo.Coordinate(DefaultLatitude, DefaultLongitude),    
    zoomLevel: defaultZoomLevel,
    components: [
        new nokia.maps.map.component.ZoomBar(),
        new nokia.maps.map.component.Behavior(),
        new nokia.maps.map.component.Overview(),
        new nokia.maps.map.component.ScaleBar(),
        new nokia.maps.map.component.ContextMenu()
    ]
};

  var map = new nokia.maps.map.Display(mapContainer, mapOptions);



var str ="";
var img = "http://api.maps.nokia.com/en/playground/examples/maps/res/markerHouse.png";
var points= new Array();
var lats = new Array();
var lngs = new Array();
var times = new Array();
var num;

lats[0] = 52.516237;
lngs[0] = 13.377686;
times[0] = "brandenburg gate";
num = 1;
for(var i=0; i<num ;i++)
{
    points.push([lats[i], lngs[i]]);

    if(i==0) str = 'S';
    else if(i==num-1) str = 'E';
    else str = '';

    var marker = new nokia.maps.map.Marker(
            [lats[i], lngs[i]],
            {
                    title: str,
                    visibility: true,
                    icon: img,
                    anchor: new nokia.maps.util.Point(29, 71),
                    $html : times[i]
            });
    marker.addListener('click', function(evt){
            //$('.loc').html(times[i]); 
           alert (evt.target.$html);
    });
    map.objects.add(marker);
}

 </script>
    </body>
</html>
于 2012-10-11T09:19:13.593 回答