2

我正在对将 Bing Maps 用于提议的应用程序进行一些研究,但我遇到了麻烦。一般的想法是我想显示我们在 X 距离内的各种物品的位置。起点是美国地图,我们使用用户的点击来获取纬度/经度,并使用它来选择最近的城市。我们将在那里将地图居中,然后在规定距离内为我们的每个项目加载图钉。

在构建演示的方式中,我编写了以下内容。我遇到的问题是 plotZipcode 中对 landMap.Find 的调用静默失败。没有错误消息,并且landMap.Find 之前和之后的控制台输出都按预期显示,但 plotPushpin 从未执行。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script charset="UTF-8" type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=6.2&mkt=en-us"></script>
<script type="text/javascript">
    var landMap = null;

    function getLatLong(evt) {
        var latLong = landMap.PixelToLatLong(new VEPixel(evt.mapX, evt.mapY));
        // This looks up the nearest city from the lat/long
        // and returns something like "EAGLE CREEK, IN"
        $.ajax({
            url: '/cfc/bing.cfc',
            data: {
                method: 'findCity',
                Latitude: latLong.Latitude,
                Longitude: latLong.Longitude
            },
            success: plotZipcode
        });
    }

    function plotPushpin(layer, results, places, expectMore, errorMessage) {
        console.log('Executing plotPushpin...');
        if (landMap && places && places.length >= 1) {
            var pushpin = new VEShape(VEShapeType.Pushpin, places[0].LatLong);
            pushpin.SetTitle('Available Repos Near '+places[0].Name);
            landMap.AddShape(pushpin);
        }
    }

    function plotZipcode(data, textStatus, XMLHttpRequest) {
        console.log('Executing plotZipcode...');
        if (landMap && data.length > 0) {
            console.log(data);
            landMap.Clear();
            console.log('Calling VEMap.Find...');
            landMap.Find(null, data, null, null, null, null, null, null, null, null, plotPushpin);
            //landMap.Find(null, data); // This doesn't work either.'
            console.log('Called VEMap.Find!');
        }
    }

    $(document).ready(function() {
        landMap = new VEMap('landLocation');
        landMap.LoadMap();
        landMap.ShowDisambiguationDialog(false);
        landMap.AttachEvent('onclick', getLatLong);
    });
</script>
<div id='landLocation' style="position:absolute; width:600px; height:400px;"></div>

特别令人沮丧的是,如果我使用 Firebug 手动执行以下操作,它的行为完全符合预期:

landMap.Find(null, "EAGLE CREEK, IN", null, null, null, null, null, null, null, null, plotPushpin);

任何深入了解为什么 VEMap.Find 在我的 AJAX 回调中什么都不做的任何见解都将不胜感激。

4

1 回答 1

0

问题是 Firefox 问题。由于某种原因,JQuery 文档就绪函数不喜欢在引用函数来加载地图的情况下运行。如果您将 mapload 方法放在正文 onload 事件(html 或纯 javascript - 不是 jquery)中,它将起作用。

于 2013-09-03T13:24:10.737 回答