How to get lat/lon of a mouse click with Bing Maps AJAX Control v7

我只是一个虾纸丫 提交于 2021-02-18 05:15:40

问题


I am using th newest version of the API (v7), and would like to add a pushpin on mouse-click ...

var mapSettings = {
    'credentials': 'myCredentials',
    'mapTypeId': Microsoft.Maps.MapTypeId.road,
    'enableSearchLogo': false,
    'showMapTypeSelector': false,
    'showScalebar': false
};

var $map = $('#map');
var map = new Microsoft.Maps.Map($map.get(0), mapSettings);
Microsoft.Maps.Events.addHandler(map, 'click', function (e) {
    var latitude = ?
    var longitude = ?
    var location = new Microsoft.Maps.Location(latitude, longitude);
    var pushpin = new Microsoft.Maps.Pushpin(location, {
        'draggable': true
    });
    map.entites.push(pushpin);
});

As you see, I am stuck within the click-handler: How do I get latitude & longitude of the click?


回答1:


Ok, nailed it. Here's the bit of code that you're interested in:

if (e.targetType == "map") {
  var point = new Microsoft.Maps.Point(e.getX(), e.getY());
  var loc = e.target.tryPixelToLocation(point);
  var location = new Microsoft.Maps.Location(loc.latitude, loc.longitude);
  ......
}

e.target.getLocation() only works when the target is a pushpin, infobox, etc. The click on the actual map is different.



来源:https://stackoverflow.com/questions/8590875/how-to-get-lat-lon-of-a-mouse-click-with-bing-maps-ajax-control-v7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!