【问题标题】:Can't succeed in displaying bing map from a location's paramaters and add a pushpin at the identified location无法通过位置参数成功显示 bing 地图并在识别的位置添加图钉
【发布时间】:2019-02-04 14:34:32
【问题描述】:

我希望使用位置参数在 bing 地图上显示图钉。我成功地从 BingMap Autosuggest REST API 获取这些参数。然后我希望使用这些参数(尤其是经度和纬度)在 Bing 地图上显示位置,并用图钉指示位置。

以下是我已经尝试过的:

HTML:

    <input type="text" id="longitude" value="@Model.PointLongitude" />
    <input type="text" id="latitude" value="@Model.PointLatitude" />
    <input type="text" id="address" value="@Model.AFormattedAddress" />
    <input type="text" id="locality" value="@Model.ALocality" />

    <div id="myMap" style="position:relative;width:600px;height:400px;"></div>

脚本:

    <script type='text/javascript'>
function GetMap() {
    alert("bonjour eunice");
    var longitude = document.getElementById('#longitude');
    var latitude = document.getElementById('#latitude');
    var locality = document.getElementById('#locality');
    var address = document.getElementById('#address');

    var map = new Microsoft.Maps.Map('#myMap', {
        credentials: 'Bing Map API key',
        center: new Microsoft.Maps.Location(longitude, latitude)
    });

    var center = map.getCenter();

    //Create custom Pushpin
    var pin = new Microsoft.Maps.Pushpin(center, {
        title: address,
        subTitle: locality,
        text: 'Here',
        color: 'blue'
    });

    //Add the pushpin to the map
    map.entities.push(pin);
}
    </script>
    <script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap' async defer></script>

当我执行代码时,我成功在输入字段中显示了参数,但是没有显示地图,也没有显示图钉。

请帮我指出我做错了什么。

【问题讨论】:

    标签: javascript html bing-maps


    【解决方案1】:

    问题在于 document.getElementById() 它会为您获取对象,而不是输入字段的值。

    $(window).on('load', function() {
      
          var longitude =  document.getElementById('longitude').value;
          var latitude =  document.getElementById('latitude').value;
          var locality =  document.getElementById('locality').value;
          var address =  document.getElementById('address').value;
    
          var mapCentre = new Microsoft.Maps.Location(longitude, latitude);
          var map = new Microsoft.Maps.Map('#myMap', {
            credentials: 'Bing Map API key',
            center: mapCentre
          });
    
          var center = map.getCenter();
    
          //Create custom Pushpin
          var pin = new Microsoft.Maps.Pushpin(center, {
            title: address,
            subTitle: locality,
            text: 'Here',
            color: 'blue'
          });
    
          //Add the pushpin to the map
          map.entities.push(pin);
       });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input type="text" id="longitude" value="52.029232" />
    <input type="text" id="latitude" value="5.107351" />
    <input type="text" id="address" value="Vleugelboot 48" />
    <input type="text" id="locality" value="Houten" />
    
    <div id="myMap" style="position:relative;width:600px;height:400px;"></div>
    <script src='https://www.bing.com/api/maps/mapcontrol' async defer ></script>

    【讨论】:

    • 感谢您。实际上,我没有以正确的方式获取输入字段的值。这就是我现在所做的并且它有效: var longitude = document.getElementById('longitude').value;有朝一日,这可能会对某人有所帮助
    • 我修改了答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多