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 |1 Answer
Reset to default 19Ok, 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.
e.location n {latitude: 39.29711299974974, longitude: -111.57935390625, altitude: 0, altitudeReference: -1}
– RyBolt Commented Sep 6, 2016 at 15:56