最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get latlon of a mouse click with Bing Maps AJAX Control v7 - Stack Overflow

programmeradmin1浏览0评论

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?

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?

Share Improve this question edited Dec 21, 2011 at 14:03 Christofer Eliasson 33.9k7 gold badges76 silver badges103 bronze badges asked Dec 21, 2011 at 13:52 user57508user57508 1
  • for those wondering, in Bing Maps v8 , the location is hanging right off of the callback param ( e ). e.location n {latitude: 39.29711299974974, longitude: -111.57935390625, altitude: 0, altitudeReference: -1} – RyBolt Commented Sep 6, 2016 at 15:56
Add a comment  | 

1 Answer 1

Reset to default 19

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.

发布评论

评论列表(0)

  1. 暂无评论