【问题标题】:bing maps: how to set zoom level so pinpoint is visible to users current location必应地图:如何设置缩放级别,以便用户当前位置可以看到精确定位
【发布时间】:2013-06-05 21:56:25
【问题描述】:

我正在使用 bing maps ajax v7,为简单起见,假设我在世界各地放置了 10 个 PinPoint。我正在尝试将地图缩放到最低级别,以便最近的 PinPoint 对用户的当前位置仍然可见。如果有人能指出我正确的方向,我将不胜感激。

$(document).ready(function () {
        var windowHeight = $(window).height();
        var windowWidth = $(window).width();

        map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), {
            credentials: "myCredentials",
            backgroundColor: "#A4C4ED",
            zoom: 3,
            height: windowHeight,
            width: windowWidth
        });
        Microsoft.Maps.Events.addHandler(map, 'viewchange', hideInfoBox);
        Microsoft.Maps.Events.addHandler(map, 'click', hideInfoBox);

        //get users location and set view bound
        var geoLocationProvider = new Microsoft.Maps.GeoLocationProvider(map);
        var viewRectangle = Microsoft.Maps.LocationRect(geoLocationProvider.getCurrentPosition());
        map.setView({ bounds: viewRectangle });

        dataLayer = new Microsoft.Maps.EntityCollection();
        map.entities.push(dataLayer);

        var infoboxLayer = new Microsoft.Maps.EntityCollection();
        map.entities.push(infoboxLayer);

        //create initial infobox
        infobox = new Microsoft.Maps.Infobox(new Microsoft.Maps.Location(0, 0), {
            visible: false,
            offset: new Microsoft.Maps.Point(0, 20)
        });
        infoboxLayer.push(infobox);

        Microsoft.Maps.loadModule('Microsoft.Maps.Search', { callback: searchModuleLoaded });
    });

【问题讨论】:

    标签: javascript bing-maps


    【解决方案1】:

    我假设您是一个定位点,并且您在地图上的某个位置还有另外 10 个定位点。

    首先,您需要找到离您最近的定位点。 您可以使用此函数来预期两个包含纬度和经度的位置对象。

    oLocation1 = {latitude:0,longitude:0};
    oLocation2 = {latitude:19,longitude:23};
    
    
     function calcDistHaversine  (oLocation1, oLocation2) {
            var dLat = (oLocation2.latitude * Math.PI / 180 - oLocation1.latitude  * Math.PI / 180);//*Math.PI*180;
            var dLon = (oLocation2.longitude * Math.PI / 180 - oLocation1.longitude  * Math.PI / 180);//*Math.PI*180;
            var lat1 = oLocation1.latitude * Math.PI / 180;//*Math.PI*180;
            var lat2 = oLocation2.latitude  * Math.PI / 180;//*Math.PI*180;
    
            var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                    Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
            var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
            var distance = 6371 * c;
            return distance;
        };
    

    因此,您将获得这两个位置之间相对于地球曲率的距离。

    现在您有了自己的位置和最近的精确位置。

    让我们将它们命名为你的,他们的。

    接下来,您需要创建一个数组,其中包含转换为 microsoft location 对象的这两个位置。

      var yourLocation= new Microsoft.Maps.Location(your.latitude, your.longitude);
      var theirLocation= new Microsoft.Maps.Location(their.latitude, their.longitude);
    
      var arr = [];
      arr.push(yourLocation);
      arr.push(theirLocation);
    

    现在您可以使用 bing 地图功能,根据给定位置为您提供最佳缩放和指向。

    var bestView = Microsoft.Maps.LocationRect.fromLocations(arrLocations);
    

    然后根据我们找到的最佳视图设置地图视图。

     setTimeout((function () {
            map.setView({ bounds: bestView });
        }).bind(this), 1000);
    

    【讨论】:

    • 为什么需要setTimeout函数?我在没有超时的情况下使用它,地图不会缩放。按照此处的说明添加了超时并且工作正常
    • 所有人员都会发生同步,因此 1 秒是一个粗略的缓冲区,直到地图引擎实际绘制所有引脚为止。只有这样你才能使 setView 有边界。
    • 好的,但是没有一个事件我可以输入setView 代码,这样我就不必等待了?它似乎很脆弱
    • 你说得对,它很脆弱,但是 bing 缺少一些非常重要的事件,比如地图空闲或地图加载完成,你可以玩的唯一一组事件是“视图更改”。您可以尝试在触发“视图更改结束”后调用“setView”,但我没有时间测试我的建议。顺便说一下,如果超时的答案对您有用,请接受它。
    • 它确实对我有用,但这不是我的问题 :) 我已经投票了
    猜你喜欢
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 2017-05-02
    相关资源
    最近更新 更多